1

I am developing an app in Shiny and I got stuck in summing the values entered in dynamically created textBox. I want to know how to access the value entered in dynamically created textBox.

The RCode used is as follows:

library(shiny)
ui <- fluidPage (
  fluidRow(
  column(3,numericInput("count", "No. of boxes",value = 4, min = 2, max = 10),
         actionButton("View","view")

  ),
),
 fluidRow(
   uiOutput("inputGroup")
 ),
fluidRow(
  column(3,wellPanel(textOutput("text3")))
)
)


sum = 0
sumN <- function(x){
  sum <- sum + as.numeric(x)
  return(sum)
}

server <- function(input, output, session) {
  observeEvent(input$view, {
    output$inputGroup = renderUI({
      input_list <- lapply(1:(input$count), 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)

    })

  })
  getvalues <- reactive({
    tot <- input$count
    for(lim in 1:tot){
      if(lim %% 3 == 1)
        val <- reactive({sumN(as.numeric(input[[paste0("id",lim)]]))})
    }
  })

  output$text3 <- renderText({
    getvalues()
  })

  }

shinyApp(ui=ui, server = server)

Can anyone help me with this code? Thanks in advance..

Nevedha Ayyanar
  • 845
  • 9
  • 27

1 Answers1

3

I changed summing function and how are the textAreaInput are generated too, have a look

require(shiny)

ui = fluidPage(
  fluidRow(
    column(3,numericInput("count", "No. of boxes",value = 3, min = 2, max = 10),actionButton("View","view")
    )
  ),
  fluidRow(uiOutput("inputGroup")),
  fluidRow(column(3,wellPanel(textOutput("text3"))))
)

# takes in two arguments
sumN <- function(a, x){
  a <- sum(a, as.numeric(x),na.rm=T)
  return(a)
}

server <- function(input, output, session) {

  Widgets <- eventReactive(input$View,{
    input_list <- lapply(1:(input$count), 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)},ignoreInit = T)

  output$inputGroup = renderUI({Widgets()})

  getvalues <- reactive({
    val <- 0
    for(lim in 1:input$count){
      val <- sumN(val,as.numeric(input[[paste0("id",lim)]]))
    }
    val
  })

  output$text3 <- renderText({getvalues()})
}

shinyApp(ui=ui, server = server)

enter image description here

Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • Perfect answer ! I'd just change the sum function as `sumN <- function(sum, x){ sum <- sum(sum, x, na.rm = T) return(sum) }` to handle the "NA" when at least one entry is missing. If the "0" when no values is not convenient, you can also put a condition in `output$text3 ` – Antoine Pissoort Oct 16 '17 at 07:50
  • It works correctly!! But if some values are not entered in the textBox means the sum is not displayed.. Can anyone say the solution? – Nevedha Ayyanar Oct 16 '17 at 08:11
  • Its to do with your function `sumN` and not `shiny`, have a look I've updated the `sumN`. Also try not to use default functions as variables such as `sum` – Pork Chop Oct 16 '17 at 08:17
  • @PorkChop I don't understand why you are redirecting me to this link ? I've accepted your answer before writing my comment, and now you're using my comment in your edit .. – Antoine Pissoort Oct 16 '17 at 08:23
  • @AntoinePissoort sorry I got mixed up with a few posts, my bad ^^ – Pork Chop Oct 16 '17 at 09:02