【问题标题】:To add the values in dynamically created textBox in RShiny在 R Shiny 中动态创建的文本框中添加值
【发布时间】:2017-10-16 06:20:53
【问题描述】:

我正在 Shiny 中开发一个应用程序,但我陷入了对动态创建的 textBox 中输入的值求和的过程中。我想知道如何访问在动态创建的文本框中输入的值。

使用的RCode如下:

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)

谁能帮我处理这段代码? 提前谢谢..

【问题讨论】:

    标签: r shiny shiny-server


    【解决方案1】:

    我改了求和函数,textAreaInput也是怎么生成的,看看

    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)
    

    【讨论】:

    • 完美答案!当至少缺少一个条目时,我只需将 sum 函数更改为 sumN &lt;- function(sum, x){ sum &lt;- sum(sum, x, na.rm = T) return(sum) } 以处理“NA”。如果没有值的时候“0”不方便,也可以在output$text3 中加个条件
    • 它工作正常!!但是如果一些值没有在文本框中输入意味着总和没有显示出来。。谁能说出解决方案?
    • 它与你的函数 sumN 而不是 shiny 有关,看看我已经更新了 sumN。也尽量不要使用默认函数作为变量如sum
    • @PorkChop 我不明白你为什么要把我重定向到这个链接?在写评论之前我已经接受了你的回答,现在你在编辑中使用我的评论..
    • @AntoinePissoort 对不起,我把一些帖子搞混了,我的错^^
    猜你喜欢
    • 2017-11-30
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    • 2016-12-21
    • 2022-01-11
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多