[英]Nested 'uiOutput' calls in a shiny application
最小可重復的例子:
#### 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種可能的組合。
您可以簡單地將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並渲染其他小部件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.