【问题标题】:Ammending dynamic input code in R Shiny在 R Shiny 中修改动态输入代码
【发布时间】:2016-03-22 07:51:47
【问题描述】:

所以几天前我问了以下问题,R Shiny Dynamic Input,虽然给出的问题的答案是正确的,但我现在想详细说明一下,因为我无法编辑给出的代码来回答我的新问题。所以最初我希望能够让用户指定一个数字,比如 k,然后动态生成 k 个字段供用户填写。现在,给出的代码假定输出是数字,但是,我希望用户能够在 1,...,k 字段中的每个字段中指定一个包含 5 个值的向量。由于在指定 k 之后,输入将是长度为 5 的数值的 k 个向量,因此我希望能够将这些值存储在 k x 5 矩阵中。这样我以后可以使用这些值进行数据操作。如果有帮助,这里是原始答案中的代码:

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)

编辑

这里是更新的代码,但仍然不能完全工作:

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(tableOutput("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$inputValues <- renderTable({
  all <- paste(lapply(1:input$numInputs, function(i) {
    inputName <- paste("input", i, sep = "")
    input[[inputName]]
  }))
  matrix <- as.matrix(all, ncol=5)
  as.data.frame(matrix)
})

  })

  # 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 shiny


    【解决方案1】:

    您只需要进行一些更改:

    1. mainPanel(textOutput("inputValues")) 更改为mainPanel(tableOutput("inputValues"))(这不是必需的,它只是以表格/矩阵格式显示值,以便您查看)
    2. numericInput(inputName, inputName, 1) 更改为textInput(inputName, inputName, "1 2 3 4 5")
    3. output$inputValues &lt;- renderText({...... 更改为

      output$inputValues <- renderTable({
        all <- paste(lapply(1:input$numInputs, function(i) {
          inputName <- paste("input", i, sep = "")
          input[[inputName]]
        }))
        matrix = as.matrix(read.table(text=all))
        data.frame(matrix)
      })
      

    matrix 是您想要的:k x 5 矩阵。

    请注意,我没有进行任何输入验证。假设用户将在每个输入中输入 5 个数字,以空格分隔。如果他们不这样做,输出可能是错误的,或者你会看到一个错误。您可能需要在此处实施一些输入检查,以确保它是 5 个数字而不是其他任何数字。

    完整代码

    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(tableOutput("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 = "")
            textInput(inputName, inputName, "1 2 3 4 5")
          })
          do.call(tagList, input_list)
        })
      })
    
      # this is just a demo to display all the input values
      output$inputValues <- renderTable({
        all <- paste(lapply(1:input$numInputs, function(i) {
          inputName <- paste("input", i, sep = "")
          input[[inputName]]
        }))
        matrix = as.matrix(read.table(text=all))
        data.frame(matrix)
      })
    
    })
    
    # Run the application
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 感谢您的更新。但是,当我运行代码时,它不会动态填充任何字段。
    • 太棒了,非常感谢!是否可以用逗号而不是空格分隔输入?我试过sep=",",但似乎没有用。
    • 最后一个问题,但是当我这样做时,矩阵只有 3 行(假设用户指定了 4)
    • 这是因为read.csv 假定默认情况下有一个标头。使用matrix = as.matrix(read.csv(text=all,header=F))
    猜你喜欢
    • 2018-11-28
    • 2017-05-17
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 2016-10-11
    相关资源
    最近更新 更多