【问题标题】:Nested 'uiOutput' calls in a shiny application在闪亮的应用程序中嵌套“uiOutput”调用
【发布时间】:2015-05-05 00:54:55
【问题描述】:

最小可重现示例:

#### global.R
combinations <- data.frame(animal = c(rep('cat', 2),
                                  rep('dog', 3),
                                  rep('horse', 2)),
                       color = c('blue', 
                                 'black', 
                                 'red', 
                                 'red', 
                                 'black',
                                 'red',
                                 'green'),
                       region = c('west',
                                  'west',
                                  'east',
                                  'west',
                                  'east',
                                  'west',
                                  'east'))

基本上,我想在闪亮的应用程序上提供一个 UI 选项,以首先选择您的动物,然后弹出一个“颜色”下拉菜单,仅使用所选动物的可能值。那么应该会弹出一个“区域”下拉菜单,并且仅包含基于其他两个可接受的值。

我做了一个非常基本的版本,但在如何嵌套时遇到了麻烦。

#### ui.R
library(shiny)

shinyUI(fluidPage(

  # Sidebar with dropdown for animal
  sidebarLayout(
    sidebarPanel(
      selectInput("animal",
                  label = "Choose your animal:",
                 choices = unique(combinations$animal)) ,

      wellPanel(uiOutput("ui_1")),

    )
  )
))

有没有办法避免手动输入所有可能的组合并将其包装在“uiOutput”调用中?我使用的真实文件有大约 1200 种可能的组合。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以简单地将 uiOutput 嵌套在 shinyUI 中,并使用 shinyServer 中的 renderUI 创建适当的小部件。

    我在这里报告你想要做的一个例子。

    ui <- fluidPage(
    
      # Sidebar with dropdown for animal
      sidebarLayout(
        sidebarPanel(
          selectInput("animal",
                      label = "Choose your animal:",
                      choices = unique(combinations$animal)),
          uiOutput("animal_color")
    
    
    
        ),
        wellPanel(uiOutput("ui_1"))
      )
    )
    
    server <- function(input, output, session){
      output$animal_color <- renderUI({
        sel_animal <- input$animal
        col_choices <- combinations$color[which(combinations$animal==input$animal)]
        return(selectInput(inputId="color", label = "choose your color", choices = col_choices))
      })
    }
    

    使用与示例相同的数据框:

    combinations <- data.frame(animal = c(rep('cat', 2),
                                      rep('dog', 3),
                                      rep('horse', 2)),
                           color = c('blue', 
                                     'black', 
                                     'red', 
                                     'red', 
                                     'black',
                                     'red',
                                     'green'),
                           region = c('west',
                                      'west',
                                      'east',
                                      'west',
                                      'east',
                                      'west',
                                      'east'))
    

    您可以在 uiOutput("animal_color") 旁边添加另一个 uiOutput 并渲染其他小部件。

    【讨论】:

      猜你喜欢
      • 2013-03-06
      • 2021-11-21
      • 2016-01-06
      • 1970-01-01
      • 2016-08-15
      • 2015-08-03
      • 2014-03-26
      • 2013-07-08
      相关资源
      最近更新 更多