我想在我的R Shiny应用程序中保存情节,但是它不起作用。我只有一个空文件,不知道如何解决。这是一个基本的闪亮结构,通过按照教程逐步进行制作,因此我认为您应该很容易理解它。 这是我的代码:
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
shinyServer <- function(input,output) {
selectDate <- reactive({
switch(input$Station,"120" = filter(s120,Time >=input$dateRangeTS[1] & Time<=input$dateRangeTS[2]),"130" = filter(s130,"215" = filter(s215,"230" = filter(s230,"330" = filter(s330,"700" = filter(s700,"710" = filter(s710,"780" = filter(s780,"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(),y = Salinite),) +
geom_point(color = "#00AFBB",y = "Salinité") +
scale_x_date(labels = date_format("%Y-%m-%d")) + ylim(27,37),"O2" = ggplot(data = selectDate(),y = `O2 (mg/L)`),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(),) +
geom_point(color = "#00AFBB",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(),) +
geom_point(color = "#00AFBB",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(),y = "O2") +
scale_x_date(labels = date_format("%Y-%m-%d")) + ylim(NA,15))
dev.off()
}
)
}
非常感谢您的帮助!