这是一个 MWE:
library(shiny)
runApp(shinyApp(
ui = pageWithSidebar(
fluidRow(
column(3, wellPanel(
numericInput("numFields", "Select number of fields", 2, min = 1),
br(),
uiOutput("fields"),
br(),
actionButton("goButton", "Go!")
)),
column(3, wellPanel(
uiOutput("morefields")
)),
column(3, wellPanel(
numericInput("numFields2", "Select number of fields 2", 2, min = 1),
br(),
actionButton("goButton2", "Go2!")
))
),
server = function(input, output, session){
output$fields <- renderUI({
numFields <- as.integer(input$numFields)
lapply(1:numFields, function(i) {
textInput(paste0("field", i), paste0("Type in field ", i))
})
})
output$morefields <- renderUI({
if (input$goButton == 0) return(NULL)
isolate({
numFields <- as.integer(input$numFields)
lapply(1:numFields, function(i) {
checkboxInput(paste0("checkbox", i), paste0("Checkbox for field ",
input[[paste0("field", i)]]))
})
})
})
observeEvent(input$goButton2, {
numFields2 <- as.integer(input$numFields2)
last_field <- paste0("field", numFields2)
updateNumericInput(session, "numFields", value = numFields2)
updateTextInput(session, "field1", value = "This is the first field")
updateTextInput(session, last_field, value = "This is the last field")
})
}))
现在我执行以下一组操作:
- 启动应用程序
- 将值设置
Select number of fields 2
为例如 3 - 按下
Go2!
按钮 - 在左栏中,输入字段的数量发生了变化,但我希望第一个和最后一个字段填充文本,所以我
Go2!
再次单击按钮 - 点击
Go!
按钮,生成中间的UI。
我的目标是避免第 4 步和第 5 步,但要获得相同的结果。
我尝试使用reactiveValues
-variable 和模拟点击来解决问题(如此处建议):
library(shiny)
library(shinyjs)
jscode <- "shinyjs.click = function(id) { $('#' + id).click(); }"
runApp(shinyApp(
ui = pageWithSidebar(
useShinyjs(),
extendShinyjs(text = jscode),
fluidRow(...)),
server = function(input, output, session){
vals <- reactiveValues(update = 0)
output$fields <- renderUI({...})
output$morefields <- renderUI({...})
observeEvent(input$goButton2, {
numFields2 <- as.integer(input$numFields2)
updateNumericInput(session, "numFields", value = numFields2)
vals$update <- 1
})
observeEvent(vals$update, {
if (vals$update != 1) return(NULL)
numFields2 <- as.integer(input$numFields2)
last_field <- paste0("field", numFields2)
updateTextInput(session, "field1", value = "This is the first field")
updateTextInput(session, last_field, value = "This is the last field")
vals$update <- 2
})
observeEvent(vals$update, {
if (vals$update != 2) return(NULL)
js$click("goButton")
vals$update <- 0
})
}))
现在生成了第二个 UI,但字段仍然为空。Go2!
在所有 UI 完全更新之前,我必须单击3 次。
我还尝试在server
-part 中执行以下操作:
observeEvent(input$goButton2, {
numFields2 <- as.integer(input$numFields2)
updateNumericInput(session, "numFields", value = numFields2)
}, priority = 2)
observeEvent(vals$update, {
numFields2 <- as.integer(input$numFields2)
last_field <- paste0("field", numFields2)
updateTextInput(session, "field1", value = "This is the first field")
updateTextInput(session, last_field, value = "This is the last field")
}, priority = 1)
observeEvent(input$goButton2, {
js$click("goButton")
}, priority = 0)
事件的进程再次看起来有点不同,但仍然需要单击三次才能获得我想要的。
关于如何通过Go2!
仅单击一次按钮来实现最终结果的任何建议?