R闪亮仪表板中的动态重复条件面板 [英] Dynamic repeating conditionalPanel in R shiny dashboard
问题描述
我正在尝试创建一个动态的条件面板.所以我的条件如下:
I am trying to create a dynamic conditional panel. So my conditions are as follows:
在用户界面中输入:
selectInput('inpt','Input Number', seq(1,50,1), selectize = FALSE)
我的条件面板UI输入是:
conditionalPanel(
"input.inpt == 2",
box(
selectInput("id1", "Select number",
seq(1, 24, 1), selected = 1),
selectInput("id2", "Select number",
seq(1, 24, 1), selected = 1),
width = 2,
status = "primary"
)
),
conditionalPanel(
"input.inpt == 3",
box(
selectInput("id1", "Select number",
seq(1, 24, 1), selected = 1),
selectInput("id2", "Select number",
seq(1, 24, 1), selected = 1),
selectInput("id3", "Select number",
seq(1, 24, 1), selected = 1),
width = 2,
status = "primary"
)
因此,此代码非常有效!但是问题是它是硬编码的.
So this code is working great! But the problem is it is hardcoded.
正如您可以签入 selectInput 一样,我总共有50个输入.
As you can check in selectInput, I have total of 50 inputs.
我有完全相同数量的新selectInput面板,具体取决于输入.
I have the exactly same number of new selectInput panels depending on the input.
示例:如果选择3,则我将有3个selectInput,分别为id1,id2,id3(如上面的代码所示).如果选择18,我将有18个selectInput,分别为id1,id2,...,id18.如果选择了'n',我将拥有'n'具有id1,id2,... idn的selectInput.
Example: If 3 is selected, I will have 3 selectInput with id1, id2, id3 (as shown in code above). If 18 is selected, I will have 18 selectInput with id1, id2, ..., id18. If 'n' is selected, I will have 'n' selectInput with id1, id2,... idn.
我不想写这么多代码.
P.S .:所有更改都在box()中进行.我不想创建多个框.
P.S.: All changes are made in just box(). I don't want to create multiple boxes.
那么如何概括这个呢?如果我有"n"个输入,如何重复这种方法?
So how to generalised this? If I have 'n' number of inputs how to repeat this approach?
推荐答案
您可以使用 renderUI
而不是 conditionalPanel
:
output$selectors <- renderUI({
n <- input$inpt
selectors <- lapply(1:n, function(i){
selectInput(paste0("id",i), "Select number", seq(1,24), selected = 1)
})
do.call(function(...){
box(..., width = 2, status = "primary")
}, selectors)
})
和 uiOutput("selectors")
在您的Shiny UI中.
and uiOutput("selectors")
in your Shiny UI.
小例子:
library(shiny)
library(shinydashboard)
ui <- fluidPage(
br(),
selectInput("inpt", "Input Number", seq(1,50), selectize = FALSE),
br(),
uiOutput("selectors")
)
server <- function(input, output, session){
output[["selectors"]] <- renderUI({
n <- input[["inpt"]]
selectors <- lapply(1:n, function(i){
selectInput(paste0("id",i), "Select number", seq(1,24), selected = 1)
})
do.call(function(...){
box(..., width = 2, status = "primary")
}, selectors)
})
}
shinyApp(ui, server)
这篇关于R闪亮仪表板中的动态重复条件面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!