Search code examples
rshinygooglevis

My gvisBubbleChart Plot in Shiny R stops displaying when I attempt to introduce a dynamic selectInput field


I have encountered this problem while developing an app, and reproduced it here in a simplified script using Fruits df. Basically, i have selectInput box to select a Year, which is a column in Fruits. I create unique list of Years, and feed it into selectInput box. Then, ideally, i wanted my plot to display only the records for the year I selected. However, as you'll see in my code - the second you uncomment a block of 3 lines to accomplish that, - the plot stops displaying even though there doesn't seem to be any errors. Anybody knows why is this? Thanks in advance!!!

Related question - while debugging this i saw that the input$explore_year is at first "Null". I'm trying to handle this in the code but not sure why the selected="2010" doesn't take care of it automatically.

    library(shiny)
    library(googleVis)
    library(DT)
    listOfFruits <- sort(unique(Fruits$Year), decreasing = FALSE)
    ui <- fluidPage(title = "Fruits Bug Recreated",
         fluidRow(
           column(3, 
                  wellPanel(      
                     uiOutput("choose_year"),
                    br()
                   )),
           column(9, 
                  tags$hr(),
                  htmlOutput("view")
                 )),
         fluidRow(DT::dataTableOutput("tableExplore"))
         )
  server <- function(input, output) {
  output$view <- renderGvis({

#Uncomment these 3 lines to see how the plot stops displaying.
# local_exloreYear <- input$explore_year
# if (is.null(local_exloreYear)) {local_exloreYear <- "2010"}
# FruitsSubset <- subset(Fruits, Year == local_exloreYear)
#------------I wanted to use the commented line below instead of the           
#that follows
#gvisBubbleChart(FruitsSubset, idvar="Fruit",
#-------------    
gvisBubbleChart(Fruits, idvar="Fruit",                 
                xvar="Sales", yvar="Expenses",
                colorvar="Year", sizevar="Profit",
                options=list(
                  hAxis='{minValue:70, maxValue:125, title:"Sales"}',sortBubblesBySize=TRUE,
 vAxis='{title: "Expenses",minValue:60, maxValue:95}' 
 ))
 })
# Drop-down selection box for dynamic choice of minutes in the plans to compare
output$choose_year <- renderUI({
selectInput("explore_year", "Select Year", as.list(listOfFruits),selected ="2010")
  })

output$tableExplore <- DT::renderDataTable(DT::datatable({
FruitsSubset <- subset(Fruits, Fruits$Year == input$explore_year)
myTable <-FruitsSubset[,c(1,2,3,4,5,6)]
data <- myTable
data
},options = list(searching = FALSE,paging = FALSE) 
))
}

shinyApp(ui = ui, server = server)

Solution

  • Like i wrote in the comments you can solve it by make the rendering conditional on the input being non-NULL.

    output$view <- renderGvis({
      if(!is.null(input$explore_year)){
        ...
      }
    })
    

    Nevertheless, I don´t think it is really intended that you have to do that, as in other render functions it is not required e.g. in the DT::renderDataTable(), where you also use the same input (being NULL initially). Therefore, I would suggest reporting it as a bug.