R 动态输入

R 动态输入,r,shiny,R,Shiny,我想构建一个Rshinny应用程序,它有一个动态输入,要求用户输入一个数字,然后根据该输入生成另外4个输入字段。这就是我的想法 library(shiny) # Define UI for random distribution application shinyUI(fluidPage( # Application title titlePanel("Calcs"), # Sidebar with controls to select the random distribu

我想构建一个Rshinny应用程序,它有一个动态输入,要求用户输入一个数字,然后根据该输入生成另外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(

  )
)))
库(闪亮)
#定义随机分布应用程序的UI
shinyUI(fluidPage)(
#申请名称
titlePanel(“Calcs”),
#带有控件的侧栏,用于选择随机分布类型
#以及要生成的观察值的数量。请注意
#br()元素引入额外的垂直间距
侧边栏布局(
侧栏面板(
数字输入(“dorr”,“您需要多少输入”,4),
i=i+1

while(输入$dorr参见下面的工作示例

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)
库(闪亮)

您需要在服务器代码中使用
renderUI
。请参阅本页了解一些信息examples@warmoverflow我已经意识到了这一点,但我不知道如何使用它来动态创建预先指定数量的输入。我根据您的答案提出了一个新问题,也许您可以看看: