闪亮:动态数量的输出元素/图表

19

我想制作一个响应式显示的功能,它会根据选择的输入值展示不同数量的图表。以mtcars数据集为例,我想让用户可以选择是按照齿轮数量还是化油器数量来绘制图表。

看一下unique(mtcars$gear),我们可以看到它有4 3 5三个可能的值,而unique(mtcars$carb)4 1 2 3 6 8六个可能的值。因此,我希望在选择“化油器数量”时生成6个单独的图表,在选择“齿轮数量”时只生成3个图表。我已经尝试使用conditionalPanel,但在切换选择器后会不可避免地崩溃。有办法解决吗?

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(...)
  ))

闪亮服务器:

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' 
    )
  })  
})
2个回答

18

灵感来自于这里,你可以这样做:

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

服务器.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
                        })  
                })
        }

})

它基本上动态创建htmlOutput图表,并在子集中有数据时绑定googleVis图表。


1
哇,tagList 就是我一直缺少的东西。非常感谢! - Serban Tanasa
2
我发现顶部链接非常有帮助,点赞+1。 - Soren Havelund Welling

0
你尝试过创建总共9个(6+3)conditionalPanel吗?如果是,则尝试使用3个裸露的output面板,其中包含条件以在图形之间切换,并使用另外3个conditionalPanel用于不重叠的图形?
另一种方法可能是创建一个带有内部条件的单个output面板,然后将您的3个或6个图形堆叠到一个单独的图形中。
if(cond1) {
   par(mfrow=c(3,1))
   plot1
   plot2
   plot3
} else {
   par(mfrow=c(3,2))
   plot1
   plot2
   plot3
   plot4
   plot5
   plot6
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接