After several issues trying to implement a simple downloadButton of shiny app I just tried to implement an examplo that I found.
source: https://www.youtube.com/watch?v=Y5arqZ9Bp0A
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("ngear", "Select the gear number", c("1"="cyl","2"="am","3"="gear"))
),
mainPanel(
plotOutput("plot"),
downloadButton("report","download"))
)
)
server <- function(input, output) {
mtreact <- reactive({
mtcars[,c("mpg",input$ngear)]
})
output$plot <- renderPlot({
with(mtreact(),boxplot(mpg~mtreact()[,2]))
})
output$report <- downloadHandler(
filename = function(){
paste("plot","png",sep=".")
},
content = function(){
png(file)
with(mtreact(),boxplot(mpg~mtreact()[,2]))
dev.off
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
When I run this code the shiny app run properly. However, when I click on the download button a window is open to save a file named "report" with no extension and no it does not contain a plot as expected.
This is the first time that I try this functionality. Then, anyone see any mistake on the code?