闪亮:输出元素/图的动态数量 [英] Shiny: Dynamic Number of Output Elements/Plots

查看:68
本文介绍了闪亮:输出元素/图的动态数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个反应式显示,根据选择的输入选择器的值,显示不同数量的图.在mtcars数据集的情况下,假设我要让用户选择按Nr切割.齿轮或Nr.的Carburatos来制作地块.

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.

unique(mtcars$gear),我们看到它有4 3 5,所以有3个可能的值,而unique(mtcars$carb)4 1 2 3 6 8,所以有6个可能的值.因此,我想在选择Nr. of Carburators时生成6个单独的图,而在选择Nr. of Gears时仅生成3个图.我玩过conditionalPanel,但是在选择器之间切换一次或两次后,它总是会炸毁.帮助吗?

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?

闪亮的用户界面:

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

推荐答案

灵感来自 ,您可以这样做:

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

})

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

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

这篇关于闪亮:输出元素/图的动态数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆