0

为了将绘图保存为 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)
4

1 回答 1

0

您可以进行如下操作:

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())
  }
)
于 2021-09-06T07:30:30.683 回答