【问题标题】:Save ggtern plots in R Shiny在 R Shiny 中保存 ggtern 图
【发布时间】:2021-11-02 05:42:45
【问题描述】:

为了将绘图保存为 png 或 svg 在服务器中添加什么?

ggsave 可以与 ggtern 一起使用吗? (这是对三元图的 ggplot 的扩展)

这是我在 Shiny 中尝试做的最小可重现示例:

library(shiny)
library(ggtern)
library(tidyverse)



ui <- fluidPage(

    downloadButton("dwnld", label = "Save plot"),
    plotOutput("ternary")
)


server <- function(input, output) {
    # ternary plot via ggtern
    output$ternary <- renderPlot({
        data <- tibble(x = 0.2, y = 0.3, z = 0.5)
        plot <- ggtern(data, aes(x = x, y = y, z = z)) + geom_point(size = 8)
        print(plot)
        
    })
    
    # download the plot
    #????????
}

 
shinyApp(ui = ui, server = server)

【问题讨论】:

  • 什么是plotInput
  • 请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example
  • 对不起,我将修复代码以使其可重现。谢谢
  • 我认为ggsave() 应该可以工作。我无法测试这段代码,因为shinyApp 只是挂在我身上(我不知道那是因为你遗漏了什么还是因为我在一个有趣的环境中工作(Emacs,而不是 RStudio,R 的开发版本,Linux ...) -- 我不经常做闪亮的东西。
  • @BenBolker 这很奇怪,它在我的机器上运行。我试过使用ggsave(),但没有用。我实际上不确定在服务器端的 downloadHandler 对象中放入什么。

标签: r plot shiny save ggtern


【解决方案1】:

您可以按照以下方式进行:

myPlot <- reactive({
  data <- tibble(x = 0.2, y = 0.3, z = 0.5)
  ggtern(data, aes(x = x, y = y, z = z)) + geom_point(size = 8)
})

output[["ternary"]] <- renderPlot({
  myPlot()
})

output[["dwnld"]] <- downloadHandler(
  filename = "myPlot.png",
  content = function(file){
    ggsave(file, myPlot())
  }
)

【讨论】:

  • 谢谢!!这适用于最小的示例。但是,它不适用于我的实际应用程序。这是因为我在 renderPlot 中有“数据”对象和 ggtern,而不是在单独的反应对象中。我正在使用单独的反应对象从 csv 文件中选择变量 (x,y,z)。 ggtern 是否必须在反应式表达式中?
猜你喜欢
  • 1970-01-01
  • 2021-05-07
  • 2014-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
  • 2020-07-05
  • 2016-05-24
相关资源
最近更新 更多