我想构建一个R闪亮的应用程序,它具有动态输入,要求用户输入数字,然后根据该输入生成4个输入字段。这就是我的想法。
library(shiny)
# Define UI for random distribution application
shinyUI(fluidPage(
# Application title
titlePanel("Calcs"),
# Sidebar with controls to select the random distribution type
# and number of observations to generate. Note the use of the
# br() element to introduce extra vertical spacing
sidebarLayout(
sidebarPanel(
numericInput("dorr", "How many inputs do you want",4),
i = i+1
while(input$dorr<i){
numericInput("S", "Number of simulations to run:", 10)
i=i+1
}
numericInput("S", "Number of simulations to run:", 10),
actionButton(inputId = "submit_loc",label = "Run the Simulation")
),
mainPanel(
)
)))
我知道上面的代码不起作用,但这是我的理由。我知道Shiny有条件语句,但我似乎找不到允许我生成预先指定数量的附加字段的语句。这甚至可能吗?
答案 0 :(得分:4)
请参阅下面的工作示例
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
numericInput("numInputs", "How many inputs do you want", 4),
# place to hold dynamic inputs
uiOutput("inputGroup")
),
# this is just a demo to show the input values
mainPanel(textOutput("inputValues"))
)
))
# Define server logic required to draw a histogram
server <- shinyServer(function(input, output) {
# observe changes in "numInputs", and create corresponding number of inputs
observeEvent(input$numInputs, {
output$inputGroup = renderUI({
input_list <- lapply(1:input$numInputs, function(i) {
# for each dynamically generated input, give a different name
inputName <- paste("input", i, sep = "")
numericInput(inputName, inputName, 1)
})
do.call(tagList, input_list)
})
})
# this is just a demo to display all the input values
output$inputValues <- renderText({
paste(lapply(1:input$numInputs, function(i) {
inputName <- paste("input", i, sep = "")
input[[inputName]]
}))
})
})
# Run the application
shinyApp(ui = ui, server = server)