Summing the values entered in textInput in RShiny

I am developing the Shiny app and I am unable to sum the values entered in dynamically created textInput. The RCode used is as follows:

    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)

The output is : Output

Can anyone help me with this code?

1 answer

  • answered 2017-10-11 10:03 amrrs

    While your question is modified, Here's a reproducible code for summing values entered in the textbox:

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