将在RShiny中的textInput中输入的值相加

时间:2017-10-11 10:03:31

标签: r dynamic shiny

我正在开发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)

输出是: Output

任何人都可以帮我这个代码吗?

1 个答案:

答案 0 :(得分:0)

在修改您的问题时,这是一个可重现的代码,用于汇总在文本框中输入的值:

## 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)
}

Output