I have this R shiny app that allows the user to input any CSV file, read some statistics on the data, then plot the data whilst choosing the x and y axis. All of this works fine, but I was wondering how I would go about adding a download button and allowing the user to save what has been plotted?
I have tried several methods but it just saves as a blank file:
library(shiny)
library(ggplot2)
library(shinythemes)
ui <- fluidPage(
navbarPage("Data Analysis Training:", id="navbar",
tabPanel("Upload Tab",
titlePanel("Upload your file here."),
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
tags$hr(),
checkboxInput("header", "Header", TRUE),
radioButtons("sep", "Separator",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = ","),
tags$hr(),
radioButtons("disp", "Display",
choices = c(Head = "head",
All = "all"),
selected = "head"),
radioButtons("quote", "Quote",
choices = c(None = "",
"Double Quote" = '"',
"Single Quote" = "'"),
selected = '"')),
mainPanel(
verbatimTextOutput("summary"),
tableOutput("contents")
))),
tabPanel("Graphing",
titlePanel("Plotting Graphs"),
sidebarLayout(
sidebarPanel( uiOutput("X_axis"),
uiOutput("Y_axis"),
),
mainPanel(
h3(textOutput("caption")),
plotOutput("plot"),
#downloadButton(
#'outputId = "downloadData",
#label = "Download")
)
)),
tabPanel(title = "Quit", value = "stop", icon = icon("circle-o-notch"))
))
server <- function(input, output, session) {
onSessionEnded(stopApp)
data <- reactive({
req(input$file1)
df <- read.csv(input$file1$datapath, header = input$header, sep = input$sep, quote = input$quote)
return(df)
})
output$contents <- renderTable({
if (input$disp == "head") {
return(head(data()))
}
else {
return(data())
}
})
output$summary <- renderPrint({
summary(data())
})
output$X_axis <- renderUI({
selectInput("variableNames_x", label = "X_axis", choices = names(data()))
})
output$Y_axis <- renderUI({
selectInput("variableNames_y", label = "Y_axis", choices = names(data()) )
})
plotteddata <- reactive({
selecteddata <- data.frame(data()[[input$variableNames_x]], data()[[input$variableNames_y]])
colnames(selecteddata) <- c("X", "Y")
return(selecteddata)
})
observe({
if (input$navbar == "stop")
stopApp()
})
output$plot <- renderPlot({
if (is.null(data)) { return(NULL)
} else {
ggplot(plotteddata(),aes(x = X,y = Y)) + geom_point(colour = 'red', pch=1) +
labs(y = input$variableNames_y,
x = input$variableNames_x,
title = "Data Analysis Plotting")
}
})
}
shinyApp(ui, server)