【问题标题】:Shiny: Dynamic Number of Output Elements/Plots闪亮:输出元素/绘图的动态数量
【发布时间】:2015-10-19 15:02:30
【问题描述】:

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

查看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,但在我在选择器之间切换一两次后它总是会爆炸。帮忙?

闪亮的用户界面:

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

【问题讨论】:

    标签: r shiny googlevis


    【解决方案1】:

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

    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 绘图。

    【讨论】:

    • 哇,tagList 是我一直缺少的。非常感谢!
    • 我发现上面的链接很有帮助,+1
    【解决方案2】:

    您是否尝试过制作总共 9 (6+3) 个条件面板?如果是,您是否尝试过 3 个裸露的输出面板,其中包含条件以在图之间切换,以及 3 个额外的条件面板用于非重叠图?

    另一种方法可能是使用内部条件制作单个输出面板,然后将 3 或 6 个图堆叠成单个图 ala

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

    【讨论】:

      猜你喜欢
      • 2022-10-04
      • 2021-05-25
      • 2017-05-25
      • 2019-09-08
      • 1970-01-01
      • 2020-07-21
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多