簡體   English   中英

即使條件沒有改變,也會在 Shiny 中觸發 observeEvent

[英]Trigger observeEvent in Shiny even when condition hasn't changed

由於observeEvent工作方式(通常非常直觀),我在獲取我正在尋找的應用程序中的功能時遇到了一些麻煩。

我正在尋找的基本功能是用戶可以輸入幾個數字,單擊“提交”,然后彈出一個模式以獲取用戶的姓名。 之后,該應用程序會記錄名稱並對數字求和,然后清除輸入。 然后我希望用戶能夠使用相同的名稱重復該過程 - 但該應用程序目前的結構是這樣的,總和使用一個只有當名稱不同時才響應的observeEvent (即,連續使用相同的名稱兩次不起作用,盡管我希望它)。 您可以在應用程序中看到,我嘗試解決的一個解決方案是重置inputSweetAlert的輸入(使用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)

感謝您的任何幫助,您可以提供!

您可以通過 JS 重置input$name

runjs('Shiny.setInputValue("name", null, {priority: "event"});')

這是一個工作示例:

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)

有關更多信息,請參閱這篇 文章

這是一個解決方法。 這個想法包括在每次單擊按鈕時更改輸入 id。

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) {

  go <- reactive({
    input$go
  })

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

  x <- reactiveValues(val=NULL)

  observeEvent(input[[sprintf("name-%d", go())]], {
    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("mytext")
  })

  output$res1 <- renderPrint(paste("Name:", input[[sprintf("name-%d", go())]]))
  output$res2 <- renderPrint(paste("Sum:", x$val))
}

shinyApp(ui = ui, server = server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM