ggvis图在Shiny应用程序中随机消失

3
我在Shiny中遇到了一个奇怪的问题。我的Shiny应用程序有一个ggvis图,其中包含layer_points()和几个选项来操作该图。当我运行应用程序时,有时即使我更改所有选项,一切都很好,但有时(我想没有特定的规则)图表会消失。当我更改其中一个选项时,图表会重新出现,但这并不是很好。 我研究了这个问题,但我不知道它是否是我的问题的解决方案。 当图表消失时,我的Shiny应用程序如下所示:

enter image description here

这是我的代码:

ui.R

library(ggvis)
library(markdown)
library(shiny)
library(dplyr)
library(magrittr)

shinyUI(
    fluidPage(
      h3("Title"),
      fluidRow(
        column(3,
               wellPanel(
                 radioButtons("radio",h5("Select"),choices=list("All values","Selected values"),
                              selected="All values"),

                 conditionalPanel(
                   condition = "input.radio != 'All values'",
                   checkboxGroupInput("checkGroup",label = "",
                                      choices,
                                      selected = c("AT1","AT2"))
                 ),
                 hr(),
                 radioButtons("dataset", label = h5("Drilldown"),
                              choices = list("2 Level" = "df1", "3 Level" = "df2")

                 ),
                 hr(),
                 h5("Choice"),
                 selectInput("xvar", h6(""), 
                             axis_vars_x,
                             selected = "value"),
                 selectInput("yvar", h6(""), 
                             axis_vars_y,
                             selected = "number2"),
                 hr(),
                 uiOutput("slider")
               )
        ),
        column(9,
               ggvisOutput("plot")
        )
      )

    )
  )

server.R

library(shiny)

shinyServer(function(input, output,session) {

  datasetInput <- reactive({
    switch(input$dataset,
           df2 = df2,
           df1 = df1)
  })

  axis_vara_y <- reactive({
    switch(input$yvar,
           number = 2,
           number2 = 3)
  }) 


  output$slider <- renderUI({
    sliderInput("inslider",h5(""), min   = round(min(datasetInput()[,axis_vara_y()]),0)-1, 
                max   = round(max(datasetInput()[,axis_vara_y()]),0)+1,
                value = c(round(min(datasetInput()[,axis_vara_y()]),0)-1, 
                          round(max(datasetInput()[,axis_vara_y()]),0)+1),
                step  = 0.5)
  })

  data <- reactive({
    filteredData <- datasetInput()
    axisData <- axis_vara_y()

    if(!is.null(input$inslider)){
      if(input$radio == "All values"){
        filteredData <- filteredData %>%
          filter(filteredData[,axisData] >= input$inslider[1],
                 filteredData[,axisData] <= input$inslider[2])
      }
      else {
        filteredData <- filteredData %>%
          filter(value %in% input$checkGroup,
                 filteredData[,axisData] >= input$inslider[1],
                 filteredData[,axisData] <= input$inslider[2])
      }
    }
    return(filteredData)
  })  

  data_point <- reactive({
    data() %>%
      mutate(id = row_number())

  })

  xvar <- reactive(as.symbol(input$xvar))
  yvar <- reactive(as.symbol(input$yvar))

  dotpoint_vis <- reactive({

      xvar_name <- names(axis_vars_x)[axis_vars_x == input$xvar]
      yvar_name <- names(axis_vars_y)[axis_vars_y == input$yvar]


    data_point_detail <- data_point()

    plot <- data_point_detail %>%
      ggvis(x = xvar(),y =  yvar()) %>%
      layer_points(size := 120,fill = ~value) %>%
      add_axis("x", title = xvar_name) %>%
      add_axis("y", title = yvar_name) %>%
      set_options(width = 750, height = 500, renderer = "canvas") 

  })
  dotpoint_vis %>% bind_shiny("plot")

})

global.R

choices <- list("Value1" = "AT1", "Value2" = "AT2",
                "Value3" = "AT3", "Value4" = "AT4",
                "Value5" = "AT5", "Value6" = "RT1",
                "Value7" = "AT6", "Value8" = "AT7",
                "Value9" = "AT8", "Value10" = "AT9",
                "Value11" = "AT10", "Value12" = "RT2")

levele <- c("AT1","AT2","AT3","AT4","AT5","RT1","AT6","AT7","AT8","AT9","AT10","RT2")

df1 <- data.frame(value = levele,number = seq(2,46,4), number2 = seq(2,24,2),order = 1:12) 

df2 <- data.frame(value = levele,number = rep(4:15), number2 = rep(4:9,each = 2),order = 1:12) 

df1$value <- factor(df1$value, levels = levele)
df2$value <- factor(df2$value, levels = levele)

axis_vars_y <- c("number","number2")
axis_vars_x <- c("value", "order","number","number2")

更新

我也不知道ggvis中的动画发生了什么。


@cdeterman,你知道是否有可能修复它吗? - Nicolabo
2
你更新了shiny和ggvis到新版本了吗?我更新后,问题就消失了。 - mpiktas
没错。谢谢。 - Nicolabo
1个回答

2
起初很难复现这个问题,但我发现只需在所有值和选定值之间来回点击即可复现。图表在两个单选按钮之间切换多次后会消失或重新出现,但似乎是随机的——有时需要4次点击才能使图表消失或重新出现,而有时只需要2次或其他次数的点击。
bind_shiny() 或 ggvisOutput() 中一定存在缺陷,因为以下更改可以创建一个似乎不会消失的图形:
在 ui.R 中进行以下更改:
   # ggvisOutput("plot")
   plotOutput('plot')

在 server.R 文件中进行以下更改:
plot(data_point_detail[ , c(input$xvar, input$yvar)], xlab=xvar_name, ylab=yvar_name)
#     plot <- data_point_detail %>%
#       ggvis(x = xvar(),y =  yvar()) %>%
#       layer_points(size := 120,fill = ~value) %>%
#       add_axis("x", title = xvar_name) %>%
#       add_axis("y", title = yvar_name) %>%
#       set_options(width = 750, height = 500, renderer = "canvas") 
#     plot

并且

output$plot <- renderPlot(dotpoint_vis())
# dotpoint_vis %>% bind_shiny("plot")

显然,那不是你想要的样子——它只是为了展示plotOutput()和renderPlot()的工作原理,而ggivisOutput()和bind_shiny()则无法正常工作。 - numbercruncher
我排除了add_tooltip代码,因为它对我非常重要,这就是为什么我必须使用ggvis的原因。所以这不是我的问题的解决方案。 - Nicolabo
起初我认为问题可能出在你使用dotpoint_vis的方式上。你将其定义为一个反应式,并且通常会在稍后引用结果时使用dotpoint_vis(),但是你省略了括号并说dotpoint_vis%>% bind_shiny(“plot”)。如果加上括号,就会出现错误。bind_shiny的帮助文档声称它可以接受一个反应式表达式,但为了避免这个错误,你需要将其放在observe()中,所以observe(dotpoint_vis()%>% bind_shiny(“plot”))将运行,但这并不能解决你最初的问题。我仍然认为bind_shiny()中一定有一个bug。 - numbercruncher

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接