在RShiny中动态创建的文本框中添加值
我正在用Shiny开发一个应用程序,我陷入了对动态创建的文本框中输入的值求和的困境。我想知道如何访问在动态创建的文本框中输入的值 使用的RCode如下所示:在RShiny中动态创建的文本框中添加值,r,shiny,shiny-server,R,Shiny,Shiny Server,我正在用Shiny开发一个应用程序,我陷入了对动态创建的文本框中输入的值求和的困境。我想知道如何访问在动态创建的文本框中输入的值 使用的RCode如下所示: library(shiny) ui <- fluidPage ( fluidRow( column(3,numericInput("count", "No. of boxes",value = 4, min = 2, max = 10), actionButton("View","view") ), )
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)
库(闪亮)
uiI更改了求和函数,以及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)
require(闪亮)
ui=fluidPage(
fluidRow(
列(3,数字输入(“计数”,“框数”),值=3,最小值=2,最大值=10),操作按钮(“查看”,“查看”)
)
),
fluidRow(uiOutput(“inputGroup”)),
fluidRow(第3列,井面板(文本输出(“文本3”)))
)
#包含两个论点
完美答案!我只需将sum函数更改为sumN,它工作正常!!但如果文本框中未输入某些值,则表示不显示总和。。有人能说出解决方案吗?这与您的函数sumN
有关,而不是shinny
,看一看,我已经更新了sumN
。另外,请尽量不要将默认函数用作变量,例如sum
@PorkChop我不明白您为什么要将我重定向到此链接?在写我的评论之前,我已经接受了你的回答,现在你在编辑中使用了我的评论。@AntoinePissoot很抱歉我被一些帖子搞混了,我的错^^