To add the values in dynamically created textBox in RShiny

為{幸葍}努か 提交于 2019-12-11 05:26:19

问题


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:


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)



来源:https://stackoverflow.com/questions/46764036/to-add-the-values-in-dynamically-created-textbox-in-rshiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!