嵌套的'uiOutput'调用闪亮的应用程序

时间:2015-03-04 06:02:41

标签: r shiny

最低可重复性示例:

#### 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选项来首先选择你的动物,然后我们有一个颜色&#39;下拉弹出仅使用所选动物的可能值。在一个地区&#39;下拉列表应该弹出,并且只包含基于其他两个值的可接受值。

我做了一个非常基本的版本,但我很难解决这个问题。

#### 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种可能的组合。

1 个答案:

答案 0 :(得分:0)

您可以简单地将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并渲染其他小部件。

相关问题