点击“填充示例名称”时,重置后未设置新的name
值
library(shiny)
shinyApp(
ui = fluidPage(
shinyjs::useShinyjs(),
shinyjs::inlineCSS(list(.big = "font-size: 2em")),
div(id = "myapp",
h2("shinyjs demo"),
checkboxInput("big", "Bigger text", FALSE),
textInput("name", "Name", ""),
a(id = "toggleAdvanced", "Show/hide advanced info", href = "#"),
shinyjs::hidden(
div(id = "advanced",
numericInput("age", "Age", 30),
textInput("company", "Company", "")
)
),
p("Timestamp: ",
span(id = "time", date()),
a(id = "update", "Update", href = "#")
),
actionButton("submit", "Submit"),
actionButton("reset", "Reset form"),
actionButton("example", "Fill example name")
)
),
server = function(input, output, session) {
observe({
shinyjs::toggleState("submit", !is.null(input$name) && input$name != "")
})
shinyjs::onclick("toggleAdvanced",
shinyjs::toggle(id = "advanced", anim = TRUE))
shinyjs::onclick("update", shinyjs::html("time", date()))
observe({
shinyjs::toggleClass("myapp", "big", input$big)
})
observeEvent(input$submit, {
shinyjs::alert("Thank you!")
})
observeEvent(input$reset, {
shinyjs::reset("myapp")
})
observeEvent(input$example, {
shinyjs::reset("myapp")
shiny::updateTextInput(session, "name", value = "Joe Schmoe")
})
}
)
1条答案
按热度按时间6uxekuva1#
问题是,
shinyjs::reset
和updateTextInput
在同一个reactive cycle中触发。我们可以从重置中排除输入$name: