首页 文章

用户指定填充

提问于
浏览
0

我正在使用ggvis并在UI端使用selectInput进行以下代码,允许用户选择哪个变量指示填充和形状(inputId分别是填充和形状) . 我希望绘图能够在用户选择时具有恒定的填充或形状 . 这段代码的else部分正是我想要的,但当我在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")

2 回答

  • 0

    正如我在评论中提到的,您可以使用 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
        }
    
    })
    
  • 1

    现在代码可以使用来自@aosmith的输入,因为我想要它是否被抑制 . 但是,当我这样做时,填充和形状的图例重叠,因为这篇文章解决了 .

    legends on ggvis graph are overlaping when using tooltip

    解决方法是添加一个图例,如果选择了常量数据可视化选项,则会使图形消失 . 我将发布一个新问题,尝试解决此问题 .

    更新:下面的答案解决了原始问题,但@ 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")
    

相关问题