R闪亮的动态输入 [英] R Shiny Dynamic Input

查看:25
本文介绍了R闪亮的动态输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

查看下面的工作示例

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)

这篇关于R闪亮的动态输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆