I want to download my plot into png or pdf formate .
here code for UI:
library(shiny)
shinyUI(fluidPage(
titlePanel(title=h3("Iris Dataset Shiny App",align="center")),
sidebarLayout(
sidebarPanel(
selectInput("x_axis","Select Variable",
choices = c("Sepal.Length"=1,"Sepal.Width"=2,
"Petal.Length"=3,"Petal.Width"=4)),
br(),
selectInput("y_axis","Select Variable",
choices = c("Sepal.Length"=1,"Sepal.Width"=2,
"Petal.Length"=3,"Petal.Width"=4)),
br(),
radioButtons("fileType","Choose file type",
choices = c("png","pdf"),selected = "png")
),
mainPanel(
plotOutput("plot"),
downloadButton(outputId="downloadBtn",label="Download the file")
)
)
))
Here code for server:
library(shiny)
shinyServer(
function(input,output){
x <- reactive({
iris[,as.numeric(input$x_axis)]
})
y <- reactive({
iris[,as.numeric(input$y_axis)]
})
output$plot <- renderPlot({
plot(x(),y())
})
output$downloadBtn <- downloadHandler(
filename = function(){
paste("iris",input$fileType,sep = ".")
},
content = function(file){
if(input$fileType=="png")
png(file)
else
pdf(file)
plot(x(),y())
dev.off()
}
)
}
)