【问题标题】:Summing the values entered in textInput in RShiny在 RShiny 中对 textInput 中输入的值求和
【发布时间】:2017-10-11 10:03:31
【问题描述】:

我正在开发 Shiny 应用程序,但无法对动态创建的 textInput 中输入的值求和。 使用的RCode如下:

    ui <- fluidPage(
    fluidRow(
        column(3, offset = 3,wellPanel(textOutput("text2"))),
        column(3,wellPanel(textOutput("text3"))),
        column(3,wellPanel(textOutput("text4")))
      )
    )
    server <- function(input, output, session){
      observeEvent(input$view, {
        output$inputGroup = renderUI({
#code for generating textBoxes and corresponding Id's dynamically 
          input_list <- lapply(1:(nrow(df())*3), function(i) {
            inputName <- paste("id", i, sep = "")
            textInputRow<-function (inputId,value) 
            {
                        textAreaInput(inputName,"", width = "200px", height = "43px", resize = "horizontal")
            }
            column(4,
                   textInputRow(inputName, "")
            )

          })
          do.call(tagList, input_list)

        })

      })
#code for adding the values and displaying the sum
     output$text2 <- renderText({
        tot = nrow(df())*3
        sum1 = 0
        for(lim in 1:tot){
          if(lim %% 3 == 1){
            inp = paste("id",lim)
            sum1 = sum1 + input[[inp]]
          }
        }
      }) 
    }
    shinyApp(ui = ui, server = server)

输出是:

谁能帮我处理这段代码?

【问题讨论】:

  • 您面临的错误或障碍是什么。您能否修改您的代码以使其可重现?
  • 问题是文本框和相应的 InputId 是动态生成的。我想对 InputId 满足条件的文本框求和(最后一个“if”条件,其中 lim 是总数,比如 18。)并在 TextOutput 中显示总和。 @amrrs

标签: r dynamic shiny


【解决方案1】:

虽然您的问题已修改,但这里有一个可重现的代码,用于对文本框中输入的值求和:

## Only run examples in interactive R sessions
if (interactive()) {

  ui <- fluidPage(
    textInput("input1", "Input1", 1),
    textInput("input2", "Input2", 2),
    tags$h3('Result:'),
    verbatimTextOutput("value")
  )
  server <- function(input, output) {
    output$value <- renderText({ as.numeric(input$input1) + as.numeric(input$input2)})
  }
  shinyApp(ui, server)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2020-07-10
    • 2017-10-07
    • 2019-06-08
    相关资源
    最近更新 更多