我正在开发一个Shiny的应用程序,我总是陷入动态创建的textBox中输入的值的总和。我想知道如何访问动态创建的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)
任何人都可以帮我这个代码吗? 提前谢谢..
答案 0 :(得分: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)