修改R SHINY中的动态输入码 [英] Ammending dynamic input code in R Shiny

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

问题描述

因此,我在几天前问了以下问题,R Shiny Dynamic Input,虽然这个问题的答案是正确的,但我现在想详细说明一些,因为我无法编辑为回答我的新问题而给出的代码。因此,最初我希望能够要求用户指定一个数字,比如k,然后动态生成k个字段供用户填写。现在,给定的代码假设输出是一个数字,但是,我希望用户能够在1,.,k字段中的每个字段中指定一个包含5个值的向量。由于在指定k之后,输入将是长度为5的数值的k个向量,因此我希望能够将这些值存储在k乘以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)

推荐答案

您只需做几处更改:

  1. mainPanel(textOutput("inputValues"))更改为mainPanel(tableOutput("inputValues"))(这不是必需的,它只是以表格/矩阵格式显示值,以便您可以看到它们)
  2. numericInput(inputName, inputName, 1)更改为textInput(inputName, inputName, "1 2 3 4 5")
  3. output$inputValues <- 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就是您想要的:a 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)

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

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