对于我正在开发的 Shiny 应用程序,给定一个数据文件,我需要在应用程序的起始页上显示一个条形图(我正在使用 ggplot2)。因此,当用户运行应用程序并上传文件时,他们会立即在主面板中看到条形图。
应用程序启动后,用户应该能够单击操作按钮来引导数据并将误差线添加到同一个条形图中。我不想一开始就添加错误栏,因为我的用户可能对错误栏不感兴趣,并且通过引导添加错误栏会让他们等待。所以,只有当他们想看到错误栏并且愿意等待 Bootstrap 结束时,我才必须添加错误栏。
我尝试了很多方法来更新剧情,但都没有成功。我当然可以用错误栏渲染另一个图,但这不是我想要做的理想情况。有没有办法更新地 block 或向其添加特征?
最佳答案
我认为您总是必须通过 renderPlot
来更新绘图输出(除非您将绘图保存为图像,并在不可见的范围内将其加载到绘图之上div 或类似的东西)。您可以做的是将绘图的图层分别保存为两部分,一部分带有条形图,另一部分带有误差线,然后在需要时将误差线放在顶部。
data(iris)
require(shiny)
sepal.length <- sapply(unique(iris$Species),function(s){ iris[which(iris$Species==s),"Sepal.Length"] })
data <- data.frame("mean"=apply(sepal.length,2,mean),"sd"=apply(sepal.length,2,sd),"species"=unique(iris$Species))
server <- shinyServer(function(input, output, session) {
# Save plot in reactive
plot.dat <- reactiveValues(main=NULL, layer1=NULL)
plot.dat$main <- ggplot(data=data,aes(x=species,y=mean,fill=species)) +
geom_bar(stat="identity")
observe({
print("render")
output$plot <- renderPlot({ plot.dat$main + plot.dat$layer1 })
})
observeEvent(input$add_bars,{
# Calculate standard deviation
plot.dat$layer1 <- geom_errorbar(aes(ymin=mean-sd,ymax=mean+sd))
})
})
ui <- shinyUI(fluidPage(
plotOutput("plot"),
actionButton("add_bars","Add error bars")
))
shinyApp(ui = ui, server = server)
关于r - 更新 shiny r 中的绘图输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33312817/