[英]Problem when saving plot from reactive inputs in R shiny
I want to save plots in my R Shiny app but it doesn't work.我想在我的 R Shiny 应用程序中保存绘图,但它不起作用。 I only have an empty file and i don't know how to fix it.我只有一个空文件,我不知道如何修复它。 This is a basic shiny construction, made step by step by following tutorials, so i think it should be easy for you to understand it.这是一个基本的 shiny 构造,按照教程一步一步制作,所以我想你应该很容易理解它。 Here is my code:这是我的代码:
ui.R ''' ui.R '''
ui <- navbarPage(
navbarMenu("Times series",
tabPanel("Abiotiques",
selectInput("Time_Series", p(strong("Which parameters do you want
to plot as time serie?")),
choices = list("Temperature", "Salinity", "O2")),
selectInput("Station", p(strong("Which station do you want to
plot as time serie?")),
choices = list("120","130","215","230","330",
"700","710","780","ZG02")),
mainPanel(plotOutput("TS", height = 550),
downloadButton("foo", "Download plot")))
)
''' '''
server.R server.R
shinyServer <- function(input, output) {
selectDate <- reactive({
switch(input$Station,
"120" = filter(s120, Time >=input$dateRangeTS[1] & Time<=input$dateRangeTS[2]),
"130" = filter(s130, Time >=input$dateRangeTS[1] & Time<=input$dateRangeTS[2]),
"215" = filter(s215, Time >=input$dateRangeTS[1] & Time<=input$dateRangeTS[2]),
"230" = filter(s230, Time >=input$dateRangeTS[1] & Time<=input$dateRangeTS[2]),
"330" = filter(s330, Time >=input$dateRangeTS[1] & Time<=input$dateRangeTS[2]),
"700" = filter(s700, Time >=input$dateRangeTS[1] & Time<=input$dateRangeTS[2]),
"710" = filter(s710, Time >=input$dateRangeTS[1] & Time<=input$dateRangeTS[2]),
"780" = filter(s780, Time >=input$dateRangeTS[1] & Time<=input$dateRangeTS[2]),
"ZG02" = filter(sZG02, Time >=input$dateRangeTS[1] & Time<=input$dateRangeTS[2]))
})
output$TS <- renderPlot({ y <- switch(input$Time_Series, "Temperature" = ggplot(data = selectDate(), aes(x = Time, y = Temperature), ) + geom_point(color = "#00AFBB", size = 2) + labs(x = "Time", y = "Temperature") + scale_x_date(labels = date_format("%Y-%m-%d")) + ylim(0, 25),
"Salinity" = ggplot(data = selectDate(), aes(x = Time, y = Salinite), ) +
geom_point(color = "#00AFBB", size = 2) + labs(x = "Time", y = "Salinité") +
scale_x_date(labels = date_format("%Y-%m-%d")) + ylim(27, 37),
"O2" = ggplot(data = selectDate(), aes(x = Time, y = `O2 (mg/L)`), ) +
geom_point(color = "#00AFBB", size = 2) + labs(x = "Time", y = "O2") +
scale_x_date(labels = date_format("%Y-%m-%d")) + ylim(NA, 15))
plot(y)
})
output$foo <- downloadHandler(
filename = function() {
paste("test", "png", sep=".")
},
content = function(file) {
if(input$Time_Series == "Temperature")
png(file)
print(ggplot(data = selectDate(), aes(x = Time, y = Temperature), ) +
geom_point(color = "#00AFBB", size = 2) + labs(x = "Time", y = "Temperature") +
scale_x_date(labels = date_format("%Y-%m-%d")) + ylim(0, 25))
if(input$Time_Series == "Salinity")
png(file)
print(ggplot(data = selectDate(), aes(x = Time, y = Salinite), ) +
geom_point(color = "#00AFBB", size = 2) + labs(x = "Time", y = "Salinité") +
scale_x_date(labels = date_format("%Y-%m-%d")) + ylim(27, 37))
if(input$Time_Series == "O2")
png(file)
print(ggplot(data = selectDate(), aes(x = Time, y = `O2 (mg/L)`), ) +
geom_point(color = "#00AFBB", size = 2) + labs(x = "Time", y = "O2") +
scale_x_date(labels = date_format("%Y-%m-%d")) + ylim(NA, 15))
dev.off()
}
)
}
Thanks a lot for your help!非常感谢你的帮助!
You need brackets after the if
statements because right now you're only doing png()
in the ifs. if
语句后需要括号,因为现在您只在 ifs 中执行png()
。 Also, try ggsave()
:另外,尝试ggsave()
:
output$foo <- downloadHandler(
filename = function() {
paste("test", "png", sep=".")
},
contentType = 'image/png',
content = function(file) {
if(input$Time_Series == "Temperature") {
p <- ggplot(data = selectDate(), aes(x = Time, y = Temperature), ) +
geom_point(color = "#00AFBB", size = 2) +
labs(x = "Time", y = "Temperature") +
scale_x_date(labels = date_format("%Y-%m-%d")) +
ylim(0, 25)
} else if (input$Time_Series == "Salinity") {
p <- ggplot(data = selectDate(), aes(x = Time, y = Salinite), ) +
geom_point(color = "#00AFBB", size = 2) +
labs(x = "Time", y = "Salinité") +
scale_x_date(labels = date_format("%Y-%m-%d")) +
ylim(27, 37)
} else if (input$Time_Series == "O2") {
p <- ggplot(data = selectDate(), aes(x = Time, y = `O2 (mg/L)`), ) +
geom_point(color = "#00AFBB", size = 2) + labs(x = "Time", y = "O2") +
scale_x_date(labels = date_format("%Y-%m-%d")) +
ylim(NA, 15)
}
if (exists('p')) {
# ggsave() guesses it's png from the file extension
ggsave(file, p)
}
}
)
Do your plot in a reactive conductor, in this way you won't have to duplicate the code to save it:在电抗导体中执行 plot,这样您就不必复制代码来保存它:
myplot <- reactive({
switch(input$Time_Series,
"Temperature" = ggplot(data = selectDate(), aes(x = Time, y = Temperature), ) + geom_point(color = "#00AFBB", size = 2) + labs(x = "Time", y = "Temperature") + scale_x_date(labels = date_format("%Y-%m-%d")) + ylim(0, 25),
"Salinity" = ggplot(data = selectDate(), aes(x = Time, y = Salinite), ) +
geom_point(color = "#00AFBB", size = 2) + labs(x = "Time", y = "Salinité") +
scale_x_date(labels = date_format("%Y-%m-%d")) + ylim(27, 37),
"O2" = ggplot(data = selectDate(), aes(x = Time, y = `O2 (mg/L)`), ) +
geom_point(color = "#00AFBB", size = 2) + labs(x = "Time", y = "O2") +
scale_x_date(labels = date_format("%Y-%m-%d")) + ylim(NA, 15))
})
output$TS <- renderPlot({myplot()})
And use ggsave
:并使用ggsave
:
output$foo <- downloadHandler(
filename = function() {
paste("test", "png", sep=".")
},
content = function(file) {
ggsave(file, myplot())
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.