下面的示例代码“Code”使用包shinyStorePlus将用户 slider 从一个 session 到下一个 session 的输入保存到浏览器中。我希望用户能够通过单击“clear”actionButton()
来清除保存的输入。当“代码”中注释掉的代码取消注释时,会显示服务器部分中的明确函数,单击 actionButton()
会导致错误警告:envir$session$sendCustomMessage 中出现错误:尝试应用非功能。但是,如果我从观察者中提取 clearStore(appId = appid)
的清除数据代码并以这种方式运行代码,则它可以很好地清除保存的浏览器数据。例如,在最底部、完全在观察者之外运行“隔离清除代码”,可以像应有的那样清除浏览器数据。
我使用观察者在这里做错了什么?我尝试过使用 isolate()
、使 appid 具有反应性等,但似乎没有任何效果。
代码:
library(shiny)
library(shinyStorePlus)
ui <- fluidPage(
initStore(), br(),
sliderInput("input1",label=NULL,min=1,max=200,value=100),
actionButton("clear","Clear data")
)
server <- function(input, output, session) {
appid <- "application001"
setupStorage(
appId = appid,
inputs = list("input1")
)
# observeEvent(input$clear,{
# clearStore(appId = appid)
# })
}
shinyApp(ui, server)
隔离清算代码:
ui <- fluidPage(
initStore(),
)
server <- function(input, output, session) {
appid <- "application001"
clearStore(appId = appid)
}
shinyApp(ui, server)
最佳答案
这似乎是shinyStorePlus 代码的问题:
> clearStore
function (appId)
{
envir <- parent.frame()
envir$session$sendCustomMessage("clearStorage", appId)
}
使用parent.frame()
来获取session是不利的。
请检查以下内容:
library(shiny)
library(shinyStorePlus)
clearStore <- function(appId, session = getDefaultReactiveDomain()){
session$sendCustomMessage("clearStorage", appId)
}
ui <- fluidPage(
initStore(), br(),
sliderInput("input1",label=NULL,min=1,max=200,value=100),
actionButton("clear","Clear data")
)
server <- function(input, output, session) {
appid <- "application001"
setupStorage(
appId = appid,
inputs = list("input1")
)
observeEvent(input$clear,{
clearStore(appId = appid)
})
}
shinyApp(ui, server)
我留下了 PR here .
关于r - 为什么在使用observeEvent 和此函数时收到错误消息,该函数在未包装在观察者中时工作正常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74272837/