2017-10-16 166 views
1

我正在Shiny中开发一个应用程序,并且我总结了在动态创建的文本框中输入的值。我想知道如何访问在动态创建的文本框中输入的值。在RShiny中添加动态创建的文本框中的值

使用的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) 

谁能帮我这个代码? 在此先感谢..

回答

3

我改变求和函数以及如何是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) 

enter image description here

+0

完美的答案!我只需将sum函数改为:sumN < - function(sum,x)sum

+0

@AntoinePissoort中加入一个条件https://stackoverflow.com/help/someone-answers –

+0

它工作正常!但如果一些值没有输入到文本框中意味着总和没有显示..任何人都可以说解决方案? –