即使条件没有改变,也会在 Shiny 中触发observeEvent

pha*_*man 5 r shiny shinyjs shinywidgets

observeEvent由于工作方式(通常非常直观),我在获取我正在寻找的应用程序中的功能时遇到了一些麻烦。

我正在寻找的基本功能是用户可以输入几个数字,单击“提交”,然后弹出一个模式以获取用户名。之后,应用程序记录姓名并对数字求和,然后清除输入。然后我希望用户能够使用相同的名称重复该过程 - 但应用程序当前的结构是,总和使用仅observeEvent当名称不同时才响应(即,连续两次使用相同的名称不会行不通,尽管我希望如此)。inputSweetAlert您可以在应用程序中看到,我尝试的解决方案是重置(using )的输入shinyjs,但我认为它无法访问它,因为它实际上不在 UI 端。我正在使用shinyWidgets sweetAlerts,我想继续这样做。

这是一个示例应用程序:

library("shiny")
library("shinyWidgets")
library("shinyjs")

ui <- fluidPage(
  shinyjs::useShinyjs(),
  numericInput("num1", "Enter a number", value=NULL),
  numericInput("num2", "Enter another number", value=NULL),
  actionButton(inputId = "go", label = "submit"),
  verbatimTextOutput(outputId = "res1"),
  verbatimTextOutput(outputId = "res2")
)

server <- function(input, output, session) {

  observeEvent(input$go, {
    inputSweetAlert(session = session, inputId = "name", title = "What's your name?")
  })

  x <- reactiveValues(val=NULL)

  observeEvent(input$name, {
    x$val <- input$num1 + input$num2
    confirmSweetAlert(session = session, inputId = "confirmed", title = "Success!", text = "Your responses have been recorded. All is in order.", type = "success", btn_labels = c("Ok, let me continue")
    )
  })

  ## A possible approach to a solution...
  observeEvent(input$confirmed, {
    shinyjs::reset("num1") 
    shinyjs::reset("num2") 
    shinyjs::reset("name")
  })

  output$res1 <- renderPrint(paste("Name:", input$name))
  output$res2 <- renderPrint(paste("Sum:", x$val))
}

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

感谢您的任何帮助,您可以提供!

ism*_*gal 5

input$name您可以通过JS重置:

runjs('Shiny.setInputValue("name", null, {priority: "event"});')
Run Code Online (Sandbox Code Playgroud)

这是一个工作示例:

library("shiny")
library("shinyWidgets")
library("shinyjs")

ui <- fluidPage(
  shinyjs::useShinyjs(),
  numericInput("num1", "Enter a number", value = NULL),
  numericInput("num2", "Enter another number", value = NULL),
  actionButton(inputId = "go", label = "submit"),
  verbatimTextOutput(outputId = "res1"),
  verbatimTextOutput(outputId = "res2")
)

server <- function(input, output, session) {
  
  observeEvent(input$go, {
    inputSweetAlert(session = session, inputId = "name", title = "What's your name?")
    runjs('Shiny.setInputValue("name", null, {priority: "event"});')
  })
  
  x <- reactiveValues(val = NULL)
  
  observeEvent(input$name, {
    x$val <- input$num1 + input$num2
    confirmSweetAlert(session = session, inputId = "confirmed", title = "Success!", text = "Your responses have been recorded. All is in order.", type = "success", btn_labels = c("Ok, let me continue"))
  })
  
  observeEvent(input$confirmed, {
    shinyjs::reset("num1") 
    shinyjs::reset("num2") 
    shinyjs::reset("mytext")
  })
  
  output$res1 <- renderPrint(paste("Name:", input$name))
  output$res2 <- renderPrint(paste("Sum:", x$val))
}

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

欲了解更多信息,请参阅这篇文章

编辑:在使用模块的应用程序中,runjs()可以像这样调整调用以便为 id 命名空间:

runjs(paste0("Shiny.setInputValue(\"", ns("name"), "\", null, {priority: \"event\"});"))
Run Code Online (Sandbox Code Playgroud)

  • 我想他们需要使用 `{priority: "event"}`。另请参阅[此](https://github.com/rstudio/shiny/issues/928)。 (4认同)