用户指定的填充

User specified fill

我正在使用 ggvis 并且在 UI 侧有一个 select 输入的以下代码允许用户 select 哪个变量指示填充和形状(inputId 是填充和形状)。如果用户选择,我希望情节能够具有恒定的填充或形状。此代码的 else 部分完全按照我的要求工作,但是当我 select if 语句中的选项时,应用程序崩溃并出现以下错误:

Error in eval: could not find function ":="

我知道我的语法是正确的,因为如果我抑制图例和 if/else 语句并将填充指定为常量 (fill := "black") 它会按照我想要的方式工作。

如有任何帮助,我们将不胜感激。

vis <- reactive({
fillvar <- prop("fill", as.symbol(input$fill))
shapevar <- prop("shape", as.symbol(input$shape))
filteredData() %>%
ggvis(x = xvar, y = yvar) %>%
  layer_points(size.hover := 200,
               fillOpacity:= 0.5, fillOpacity.hover := 1,

               # Allows for points to be consistent if the user desires

               if (input$fill == "All Points Black") {
                 fill := "black"}
               else {
                 fill = fillvar}
               ,

               if (input$shape == "All Points Circles") {
                 shape := "circle"}
               else {
                 shape = shapevar}
              ,

               key := ~ID
  ) %>%

  # Adds legends to the Plot in designated locations
  add_legend("fill", title = as.character(fillvar)) %>%
  add_legend("shape", title = as.character(shapevar), properties = legend_props(legend = list(y=300))) %>%

  # Adds the previously defined tool_tip my_tooltip
  add_tooltip(my_tooltip, "hover") %>%

  # Specifies the size of the plot
  set_options(width = 800, height = 400, duration = 0)
})

#Actually plots the data
vis %>% bind_shiny("plot1")

如果图例被抑制,代码现在可以使用@aosmith 的输入,就像我希望的那样。但是,当我这样做时,填充和形状的图例会重叠,因为 post 地址。

解决方法是添加一个图例,如果选择了常量数据可视化选项,该图例就会消失。我会 post 一个新问题来尝试解决这个问题。

更新:下面的答案解决了最初的问题,但@aosmith 的答案解决了在更正第一个问题后出现的第二个问题。

我的代码更正了原始问题,但包含重叠的图例(用@aosmith 的回答更正)如下。

vis <- reactive({

  # Allows for points to be consistent if the user desires
  if (input$fill == "All Points Black") {
    fillvar = "black"}
  else {
    fillvar <- as.symbol(input$fill)}

  if (input$shape == "All Points Circles") {
    shapevar = "circle"}
  else {
    shapevar <- as.symbol(input$shape)}

#Plot Data with Visualization Customization
xvar <- prop("x", as.symbol(input$x))
yvar <- prop("y", as.symbol(input$y))

filteredData() %>%
  ggvis(x = xvar, y = yvar) %>%
  layer_points(size.hover := 200,
               fillOpacity:= 0.5, fillOpacity.hover := 1,
               prop("fill", fillvar),
               prop("shape", shapevar),
               key := ~Shot_ID
  ) %>%

  # Adds the previously defined tool_tip my_tooltip
  add_tooltip(my_tooltip, "hover") %>%

  # Specifies the size of the plot
  set_options(width = 800, height = 450, duration = 0)
})

#Actually plots the data
vis %>% bind_shiny("plot1")

正如我在评论中提到的,您可以使用 if 语句为 prop 创建一个变量。这允许您通过直接在 prop.

中使用常量或变量来绕过 := 的问题

您会自动获得图例。当您有两个图例(这将导致重叠)时要控制位置,您可以命名您的 ggvis 图。这允许您参考向图形添加元素,以便仅在基于逻辑和您的 shapevarfillvar 值添加第二个图例时才向下移动第二个图例。

这是反应函数的代码。

vis <- reactive({
    fillvar = "black"
    if(input$fill != "All Points Black") {
        fillvar = as.name(input$fill)
    }

    shapevar = "circle"
    if(input$shape != "All Points Circles") {
        shapevar = as.name(input$shape)
    }

    p1 = filteredData() %>%
        ggvis(x = xvar, y = yvar) %>%
        layer_points(size.hover := 200,
                   fillOpacity:= 0.5, fillOpacity.hover := 1,
                   prop("fill", fillvar),
                   prop("shape", shapevar),
                   key := ~ID
        ) %>%

        # Adds the previously defined tool_tip my_tooltip
        add_tooltip(my_tooltip, "hover") %>%

        # Specifies the size of the plot
        set_options(width = 800, height = 400, duration = 0)


    # Control addition of second legend using if() on p1 object
    if(fillvar != "black" & shapevar != "circle") {
        p1 %>% add_legend("shape", properties = legend_props(legend = list(y=300)))
    }
    else {
        p1
    }

})