2015-07-28 15 views
5

Desidero creare un display reattivo, che visualizzi un numero diverso di grafici a seconda del valore del selettore di ingresso scelto. Nel caso del set di dati mtcars, diciamo che voglio lasciare che l'utente scelga tra tagliare da Nr. di Gears o Nr. di Carburatos per le trame da produrre.Lucido: numero dinamico di elementi/grafici di output

Guardando unique(mtcars$gear) vediamo che ha 4 3 5 valori così 3 possibili, mentre unique(mtcars$carb) ha 4 1 2 3 6 8 valori così 6 possibili. Pertanto, desidero produrre 6 grafici separati quando è selezionato Nr. of Carburators e solo 3 grafici se è selezionato Nr. of Gears. Ho giocato con conditionalPanel ma invariabilmente esplode dopo che ho cambiato i selettori una o due volte. Aiuto?

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' 
    ) 
    }) 
}) 

risposta

5

Ispirato da this, si potrebbe fare:

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

}) 

In pratica crea dinamicamente i diagrammi htmlOutput e associa i grafici googleVis quando ci sono dati nel sottoinsieme.

+1

Wow, la tagList è quello che mi mancava da sempre. Grazie mille! –

0

Hai provato per un totale di 9 (6 + 3) conditionalPanels? In caso affermativo, avete provato 3 pannelli di output nudi con condizionali al loro interno per passare da un grafico all'altro e 3 ulteriori riquadrati condizionali per i grafici non sovrapposti?

Un altro modo potrebbe essere quello di fare un unico pannello di uscita con un condizionale interna, e quindi impilare le 3 o 6 lotti in un unico appezzamento ala

if(cond1) { 
    par(mfrow=c(3,1)) 
    plot1 
    plot2 
    plot3 
} else { 
    par(mfrow=c(3,2)) 
    plot1 
    plot2 
    plot3 
    plot4 
    plot5 
    plot6 
}