To add the values in dynamically created textBox i

2019-07-27 02:03发布

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

1条回答
姐就是有狂的资本
2楼-- · 2019-07-27 02:34

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

查看更多
登录 后发表回答