我使用observeEvent插入一个带有insertUI
的UI(在下面的示例中,它添加了一个额外的数字输入框(,并使用observe Event通过removeUI
删除这些UI。问题是通过这些UI创建的数据没有清除或重置。在下面的示例中,您可以在单独的数字输入框中输入数字,然后按Go!
将它们相加。删除元素后,它应该清除我们的数据并重新求和,但它会保留最后添加的数字。
例如,如果第一个框中有1,并且又添加了两个输入框,都有1,则按Go!
将它们正确地相加为3。但是,删除两个文本框并按Go!
将保持3,而此时它应该只显示1。
我确信我错过了一些简单的东西。我的代表如下:
library(shiny)
ui <- fluidPage(
# Application title
titlePanel("example"),
# button input
sidebarLayout(
sidebarPanel(
numericInput(
"number",
"A Number 1-10",
1,
min = 1,
max = 10,
step = NA,
width = NULL),
#add/remove buttons
fluidRow(column(
6,
actionButton('insertBtn', 'Add Number'),
actionButton('removeBtn', 'Remove Number'),
tags$div(id = 'placeholder')
)),
# go button
fluidRow(column(
6, actionButton(inputId = "go", label = "Go!"),
))
),
# output
mainPanel(
htmlOutput("total")
)
)
)
server <- function(input, output) {
# add text boxes when clicked
inserted <- c()
observeEvent(input$insertBtn, {
btn <- input$insertBtn
id <- paste0('number', btn)
insertUI(
selector = '#placeholder',
## wrap element in a div with id for ease of removal
ui = tags$div(
fluidRow(
numericInput(
id,
"A Number 1-10",
1,
min = 1,
max = 10,
step = NA,
width = NULL)
),
id = id)
)
inserted <<- c(id, inserted)
})
# remove text boxes
observeEvent(input$removeBtn, {
removeUI(
## pass in appropriate div id
selector = paste0('#', inserted[length(inserted)])
)
inserted <<- inserted[-length(inserted)]
})
# add numbers in boxes
final_number_text <- eventReactive(input$go, {
btn <- input$insertBtn
if(btn > 0){
total <- input$number
for(i in 1:btn){
total<- total+ (input[[paste0("number", i)]])
}
} else {
total <- input$number
}
HTML(paste(total))
})
output$total <- renderText({
final_number_text()
})
}
# Run the application
shinyApp(ui = ui, server = server)
当长度为0 时,我们可能需要无错误的seq_along
for(i in seq_along(inserted)) {...}
请注意,删除数字时,不会减少input$insertBtn
的值。因此,您希望只计算inserted
中的元素。因此,更换
for(i in 1:btn){...}
带有
lapply(inserted, function(par) {total <<- total + input[[par]]})
全代码
library(shiny)
ui <- fluidPage(
# Application title
titlePanel("example"),
# button input
sidebarLayout(
sidebarPanel(
numericInput(
"number",
"A Number 1-10",
1,
min = 1,
max = 10,
step = NA,
width = NULL),
#add/remove buttons
fluidRow(column(
6,
actionButton('insertBtn', 'Add Number'),
actionButton('removeBtn', 'Remove Number'),
tags$div(id = 'placeholder')
)),
# go button
fluidRow(column(
6, actionButton(inputId = "go", label = "Go!"),
))
),
# output
mainPanel(
htmlOutput("total")
)
)
)
server <- function(input, output, session) {
# add text boxes when clicked
inserted <- c()
observeEvent(input$insertBtn, {
btn <- input$insertBtn
id <- paste0('number', btn)
insertUI(
selector = '#placeholder',
## wrap element in a div with id for ease of removal
ui = tags$div(
fluidRow(
numericInput(
id,
"A Number 1-10",
1,
min = 1,
max = 10,
step = NA,
width = NULL)
),
id = id)
)
# inserted <<- c(id, inserted) ## removes first one first
inserted <<- c(inserted, id) ## removes last one first
})
# remove text boxes
observeEvent(input$removeBtn, {
removeUI(
## pass in appropriate div id
selector = paste0('#', inserted[length(inserted)])
)
inserted <<- inserted[-length(inserted)]
})
# add numbers in boxes
final_number_text <- eventReactive(input$go, {
total <- input$number
if(length(inserted) > 0){
lapply(inserted, function(par) {total <<- total + input[[par]]})
}
HTML(paste(total))
})
output$total <- renderText({
final_number_text()
})
}
# Run the application
shinyApp(ui = ui, server = server)