Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

r - Shiny: Dynamic Number of Output Elements/Plots

I want to make a reactive display, that displays a different number of plots depending on which value of the input selector is chosen. In the case of the mtcars dataset, let's say I want to let the user choose beetween cutting by Nr. of Gears or Nr. of Carburatos for the plots to be produced.

Looking at unique(mtcars$gear) we see it has 4 3 5 so 3 possible values, while unique(mtcars$carb) has 4 1 2 3 6 8 so 6 possible values. I therefore want to produce 6 separate plots when Nr. of Carburators is selected and only 3 plots when Nr. of Gears is selected. I've played with conditionalPanel but it invariably blows up after I switch between selectors once or twice. Help?

Shiny UI:

library(shiny)
library(googleVis)

shinyUI(bootstrapPage(
    selectInput(inputId = "choosevar",
              label = "Choose Cut Variable:",
              choices = c("Nr. of Gears"="gear",
                          "Nr. of Carburators"="carb")),
    htmlOutput('mydisplay')  ##Obviously I'll want more than one of these... 
#   conditionalPanel(...)
  ))

Shiny Server:

shinyServer(function(input, output) {
   #Toy output example for one out of 3 unique gear values:
    output$mydisplay <- renderGvis({
    gvisColumnChart(    
    mtcars[mtcars$gear==4,], xvar='hp', yvar='mpg' 
    )
  })  
})
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Inspired from this, you could do:

ui.R

shinyUI(pageWithSidebar(            
        headerPanel("Dynamic number of plots"),            
        sidebarPanel(
                selectInput(inputId = "choosevar",
                            label = "Choose Cut Variable:",
                            choices = c("Nr. of Gears"="gear", "Nr. of Carburators"="carb"))
        ),            
        mainPanel(
                # This is the dynamic UI for the plots
                uiOutput("plots")
        )
))

server.R

library(googleVis)
shinyServer(function(input, output) {
        #dynamically create the right number of htmlOutput
        output$plots <- renderUI({
                plot_output_list <- lapply(unique(mtcars[,input$choosevar]), function(i) {
                        plotname <- paste0("plot", i)
                        htmlOutput(plotname)
                })

                tagList(plot_output_list)
        }) 

        # Call renderPlot for each one. Plots are only actually generated when they
        # are visible on the web page. 


        for (i in 1:max(unique(mtcars[,"gear"]),unique(mtcars[,"carb"]))) {
                local({
                        my_i <- i
                        plotname <- paste0("plot", my_i)

                        output[[plotname]] <- renderGvis({
                                data <- mtcars[mtcars[,input$choosevar]==my_i,]
                                if(dim(data)[1]>0){
                                gvisColumnChart(    
                                        data, xvar='hp', yvar='mpg' 
                                )}
                                else NULL
                        })  
                })
        }

})

It basically creates htmlOutput plots dynamically and binds the googleVis plots when there is data in the subset.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...