r - 更新 shiny r 中的绘图输出

标签 r plot shiny

对于我正在开发的 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/

相关文章:

r - 为什么 data.tables mutate(row_number()) 失败?

r - 我使用R进行回归分析时下标越界错误

plot - highcharts:仅禁用图例中的单个元素,因此无法单击

css - 在 R Shiny 中更改侧边栏菜单项颜色

r - 使用renderTable()输出一个好看的矩阵

r - 将 24 小时制转换为 12 小时制

r - 将几何转换为 R 中的经度/纬度坐标

r - 在 R 中绘制具有标准差的线图

plot - 如何在图形上绘制卡方 - gnuplot

在 Shiny 应用程序中调整嵌入图像的大小