下面的示例代碼“代碼”使用包 shinyStorePlus 將用戶滑塊從一個會話輸入到下一個會話保存到瀏覽器。我希望用戶能夠通過單擊“清除”來清除保存的輸入actionButton()。當“代碼”中已注釋掉的代碼被取消注釋時,在服務器部分顯示清除功能,單擊actionButton()導致錯誤Warning: E??rror in envir$session$sendCustomMessage: attempt to apply non-function。但是,如果我拉出清晰的資料代碼clearStore(appId = appid)并以這種方式運行代碼,它可以很好地清除保存的瀏覽器資料。例如,在最底部運行“隔離清除代碼”,完全在觀察者之外,按應有的方式清除瀏覽器資料。
我在這里使用觀察者做錯了什么?我在 using 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)
uj5u.com熱心網友回復:
這似乎是 shinyStorePlus 代碼的問題:
> clearStore
function (appId)
{
envir <- parent.frame()
envir$session$sendCustomMessage("clearStorage", appId)
}
使用parent.frame()獲取會話是不利的。
請改為檢查以下內容:
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 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/524529.html
上一篇:使用資料框向函式輸入值并聚合輸出
下一篇:需要繪制堆疊等級條形圖
