【问题标题】:R Shiny Dynamic InputR闪亮的动态输入
【发布时间】:2016-03-18 21:42:03
【问题描述】:

我想构建一个 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 有条件语句,但我似乎找不到一个允许我生成预先指定数量的附加字段的语句。这甚至可能吗?

【问题讨论】:

  • 您需要在服务器代码中使用renderUI。有关示例,请参阅此页面shiny.rstudio.com/articles/dynamic-ui.html
  • @warmoverflow 我已经意识到这一点,但我不明白如何使用它来动态创建预先指定数量的输入。

标签: r shiny


【解决方案1】:

查看下面的工作示例

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)

【讨论】:

猜你喜欢
  • 2016-11-21
  • 2019-06-14
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 2014-10-25
相关资源
最近更新 更多