Akshay Bhonde
Akshay Bhonde

Reputation: 21

Unable to render googlevis charts twice through R shiny dynamic UI

Have this piece of code. I cannot get it to plot the chart twice. Try input values for textbox in this sequence:

  1. 1, 2 and 6
  2. 1, 6 and 2

For example 1, it plots the chart only once. For example 2, it plots only twice (because they are different charts) Any tips how I could get this to work as many times as the user wants?

ui <- fluidPage(   
tagList( textInput(inputId = "textbox", label = NULL,value = ""),actionButton("go", "Go"),uiOutput("ui")))

server <- function(input, output) {     

observeEvent(input$go, {
output$plot1 <- renderGvis({
  df <- data.frame(country=c("US", "GB", "BR"),
                   val1=c(10,33,44),
                   val2=c(23,122,342))

  Sys.sleep(0.3)
  gvisBarChart(df, xvar="country", yvar=c("val1", "val2"),
               options=list(isStacked=TRUE, height = 300, width = 400))

})

output$plot2 <- renderGvis({
  df <- data.frame(country=c("IND", "RUS", "BR"),
                   val1=c(10,3333,244),
                   val2=c(2344,122,342))

  Sys.sleep(0.3)
  gvisBarChart(df, xvar="country", yvar=c("val1", "val2"),
               options=list(isStacked=TRUE, height = 300, width = 400))
  })
      output$ui <- renderUI({

        if(isolate(as.numeric(input$textbox)) %in% c(1,2,3)){
          box(title = "ABC", width = 10, height = 550, htmlOutput("plot1",height = 500))    
        }else{
          box(title = "DEF", width = 4, height = 550, htmlOutput("plot2",height = 500))    

        }


      })
    })  
  }

Upvotes: 0

Views: 160

Answers (1)

Akshay Bhonde
Akshay Bhonde

Reputation: 21

Thanks to some help from an expert I was able to understand why that happened. The issue is with the html trying to figure out which chart is being rendered. It crops up only when we have dynamic UI and in the case where the same googlevis chart is being called upon. For some reason it does not know which chart is being referred and so does not populate. I'm certainly not very savvy with HTML and my statements may not be conclusive. Nevertheless, it works by adding a chart id while rendering the chart.

Pasting the code below, hoping that it helps someone in the future too.

server <- function(input, output) {     

observeEvent(input$go, {
output$plot1 <- renderGvis({
df <- data.frame(country=c("US", "GB", "BR"),
               val1=c(10,33,44),
               val2=c(23,122,342))

Sys.sleep(0.3)
gvisBarChart(df, xvar="country",chartid = "plot1",
yvar=c("val1", "val2"),
           options=list(isStacked=TRUE, height = 300, width = 400))

})

output$plot2 <- renderGvis({
df <- data.frame(country=c("IND", "RUS", "BR"),
               val1=c(10,3333,244),
               val2=c(2344,122,342))

Sys.sleep(0.3)
gvisBarChart(df, xvar="country", chartid = "plot2",
             yvar=c("val1", "val2"),
             options=list(isStacked=TRUE, height = 300, width = 400))
})
  output$ui <- renderUI({

    if(isolate(as.numeric(input$textbox)) %in% c(1,2,3)){
      box(title = "ABC", width = 10, height = 550, htmlOutput("plot1",height = 500))    
    }else{
      box(title = "DEF", width = 4, height = 550, htmlOutput("plot2",height = 500))    

    }


  })
})  
}

Although I have solved the issue for googlevis charts, I'm still struggling to find out how to do the same with maps from the leaflet package. Any pointers are highly welcome. Thanks.

Upvotes: 1

Related Questions