我有反应值,它是data.frame,并希望:
- 仅更新 1 列的反应值
- 打印仅 1 列反应值的平均值
我不知道为什么我的代码不起作用:
library(shiny)
ui <- fluidPage(actionButton("printMean", "print mean"),
actionButton("setZeroToSepal.Length", "setZeroToSepal.Length"))
dt <- iris
server <- function(input, output){
values <- reactiveValues(x = dt)
observeEvent(input$printMean, {
d <- values$x
showNotification(mean(d$Sepal.Length), type = "default")
})
observeEvent(input$setZeroToSepal.Length, {
values$x$Sepal.Length <- rep(0, nrow(values$x))
})
}
shinyApp(ui, server)
错误是:$ 中的错误:$ 运算符对原子向量无效
问题出在showNotification
上。我相信它期待一个角色,尽管它没有很好的文档记录,并且查看源代码我无法确认这一点。您可以使用
showNotification(as.character(mean(d$Sepal.Length)), type = "default")
它应该有效。