googleVis icon indicating copy to clipboard operation
googleVis copied to clipboard

renderGvis not working with renderUI and plotOutput ( while creating multiple google charts )

Open darkwish121 opened this issue 10 years ago • 4 comments

I am creating an app in which multiple charts will dynamically build using R Shiny on base of CSV folder, selected using shinyFiles. I am facing problem with combination of renderGvis, renderUI and plotOutput.

CASE 1:

renderUI creates 5 divs using plotOutput. When trying to create charts with renderGvis, it DIDN'T CREATE charts and gives error in browser's console "TypeError: invalid 'in' operand a"

CASE 2:

renderUI creates 5 divs using htmlOutput. When trying to create charts with renderGvis, it CREATE charts but as soon as I click to choose a new folder it gives following error in browser's console "uncaught exception: Duplicate binding for ID plot1". Here problem is, it re-try to create 5 div which are already exists with same ids.

Here is code.

UR.r

 library(shiny)
 library(shinyFiles)
 shinyUI(fluidPage(

 tags$head(
 tags$style(HTML("
            .shiny-output-error-validation {
            color: red;
            }
            .shiny-output-error-missing-folder {
            color: red;
            }
            "))
    ),
   titlePanel("Example"),
  sidebarPanel(
  shinyDirButton('directory', 'Folder select', 'Please select a folder'),
  tags$p(),
  tags$p('Select a folder.'),
  tags$hr()
 ),
mainPanel(
    tags$h3('Overall Insights'),
    textOutput('allStats'),
    tags$h3('Columns Charts'),
    uiOutput('columnCharts')
 )
 ))

Server.r

 library(shiny)
 library(shinyFiles)
 library(googleVis)

 shinyServer(function(input, output, session) {
 volumes <- getVolumes()
 shinyDirChoose(input, 
         'directory', 
         roots=volumes, 
         session=session, 
         restrictions=system.file(package='base'))

  dataFrame <- reactive({

validate (
   need(input$directory != "", "<< Please select any folder"),
   errorClass = "missing-folder"
) 
  text <- "dummy output"
 })

output$allStats <- renderText({

 dummyVar <- dataFrame()

 output$columnCharts <- renderUI({

  plot_output_list <- lapply(1:5, function(i) {
    plotname <- paste0("plot", i)
    htmlOutput(plotname)
 })

 do.call(tagList, plot_output_list)
 })

 for (i in 1:5) {
 local({

  my_i <- i
  plotname <- paste0("plot", my_i)

  output[[plotname]] <- renderGvis({

  data <- cars
  # plot(data)
  if(nrow(data)!= 0){

    chart <- gvisBarChart(
      data,
      options = list(
        gvis.editor = "Edit",
        legend = "none",
        backgroundColor = "transparent"
      )
    )
    return(chart)
  }
  })
})
 }
 txt <- "Google Charts"
    })

  })

darkwish121 avatar Mar 24 '16 10:03 darkwish121

I am sorry, but I don't know how to help you here. Perhaps, stackoverflow is a better place for this kind of question, as it touches on shiny, shinyFiles as well.

mages avatar Mar 25 '16 17:03 mages

I have also posted this issue in stackoverflow here is provided link. Can you please give me favor with install this script on your local and test in above conditions. Thanks

http://stackoverflow.com/questions/36197354/rendergvis-not-working-with-renderui-and-plotoutput-while-creating-multiple-go

darkwish121 avatar Mar 27 '16 01:03 darkwish121

here is Gist

You can run it by shiny::runGist('f87cb4cefec777d7ef5733b1ebe7a830')

darkwish121 avatar Mar 30 '16 11:03 darkwish121

I believe this can be close. The gist is working.

screen shot 2017-05-12 at 10 46 30 am

tiagochst avatar May 12 '17 17:05 tiagochst