【发布时间】:2020-01-16 16:35:06
【问题描述】:
由于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)
感谢您提供的任何帮助!
【问题讨论】:
-
您好,您使用的是哪个版本的
shinyWidgets? -
@Victorp,我在 0.4.9。
-
谢谢,它对我有用,很奇怪,当你第二次使用
inputSweetAlert时,值设置为NULL。尽管如此,我已经在上一个开发版 shinyWidgegts 中实施了@ismirsehregal 建议 -
嗯...奇怪的是它现在也适用于我,尽管它以前显然没有这种行为。无论哪种方式,感谢您更明确地包含该行为!
标签: r shiny shinyjs shinywidgets