格维斯图随机消失

时间:2022-08-26 23:40:06

I have a strange problem in Shiny. My shiny app has one ggvis plot with layer_points() and several options to manipulate the plot . When I run my app sometimes everything works good even if I change all options, but sometimes ( I suppose there is no specific rule) plot disappers. Plot comes back when I change one of options but it is not cool. I study this issue but I do not really know whether it is a solution for my problem. When the plot disappears my Shiny app looks like:

我有个奇怪的问题。我闪亮的应用程序有一个带有layer_points()的ggvis绘图,以及几个操作该绘图的选项。当我运行我的应用程序时,有时候即使我改变了所有的选项,所有的东西都运行得很好,但是有时候(我想没有特定的规则)情节剖析器。当我改变其中一个选项时,情节又回来了,但它并不酷。我研究这个问题,但我不知道它是否能解决我的问题。当情节消失时,我闪亮的应用程序是这样的:

格维斯图随机消失

This my code:

这我的代码:

ui.R

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

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

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")

update

更新

I also do not know what happened with animation in ggvis.

我也不知道在ggvis发生了什么。

1 个解决方案

#1


2  

The problem was difficult to reproduce at first, but I found I can reproduce it by clicking back and forth between All Values and Selected Values. The graph disappears or reappears after some number of switches between the two radio buttons, but it varies seemingly randomly -- sometimes it takes 4 clicks to make the graph disappear or reappear and other times it takes 2 clicks or some other number of clicks.

这个问题起初很难复制,但我发现我可以通过在所有值和选定值之间来回单击来复制它。在两个单选按钮之间的一些开关之后,图形会消失或重新出现,但它似乎是随机变化的——有时需要4次单击才能使图形消失或重新出现,有时需要2次单击或其他次数单击。

There must be a bug in bind_shiny() or ggvisOutput(), because the following changes do create a graphic that does not seem to disappear:

在bind_()或ggvisOutput()中必须有一个bug,因为下面的更改确实创建了一个看起来不会消失的图形:

In ui.R, make this change:

在ui。R,使这种变化:

   # ggvisOutput("plot")
   plotOutput('plot')

In server.R, make this change:

在服务器上。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

and

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

#1


2  

The problem was difficult to reproduce at first, but I found I can reproduce it by clicking back and forth between All Values and Selected Values. The graph disappears or reappears after some number of switches between the two radio buttons, but it varies seemingly randomly -- sometimes it takes 4 clicks to make the graph disappear or reappear and other times it takes 2 clicks or some other number of clicks.

这个问题起初很难复制,但我发现我可以通过在所有值和选定值之间来回单击来复制它。在两个单选按钮之间的一些开关之后,图形会消失或重新出现,但它似乎是随机变化的——有时需要4次单击才能使图形消失或重新出现,有时需要2次单击或其他次数单击。

There must be a bug in bind_shiny() or ggvisOutput(), because the following changes do create a graphic that does not seem to disappear:

在bind_()或ggvisOutput()中必须有一个bug,因为下面的更改确实创建了一个看起来不会消失的图形:

In ui.R, make this change:

在ui。R,使这种变化:

   # ggvisOutput("plot")
   plotOutput('plot')

In server.R, make this change:

在服务器上。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

and

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