在 R Shiny 中保存 ggtern 图
Save ggtern plots in R Shiny
为了将绘图保存为 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)
您可以进行如下操作:
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())
}
)
为了将绘图保存为 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)
您可以进行如下操作:
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())
}
)