R shiny- add download button to non-ggplot plots - r

I was wondering if there is a short solution (with a package?) to add a download button to non-ggplots in shiny, since my app produces many plots.
Thank you.

shinyscreenshot is a great package for screenshot shiny apps. You can create an image of the entire page of just specific elements like plots. The screenshotButton() function will create a button to save any element in your app.

A possible workaround is to create a function to make the plot or plots and call it inside a downloadHandler.
App
library(shiny)
mk_plot <- function(file, plots = NULL) {
jpeg(file) # open
par(mfrow=c(2,2))
plot(rnorm(30))
plot(rnorm(30))
# plot code
dev.off() # close
}
shinyApp(ui, server)
ui <- fluidPage(
downloadLink("downloadData", "Download")
)
server <- function(input, output) {
data <- mtcars
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".jpeg", sep = "")
},
content = function(file) {
mk_plot(file)
}
)
}
shinyApp(ui, server)

Related

Shiny App Downloads Button Only respond with HTML

I have met an issue when I try to implement a download button in Shiny App. Every time I run the app, it will only show me an HTML file not the actual content file. Here is my code for both the server and UI parts.
library(shiny)
library(reticulate)
shinyServer(function(input,output){
reticulate::source_python("function.py")
data_xi <- run_xi(26)
output$downloadData <- downloadHandler(
filename = function(){
paste(Sys.time(), 'site_mtx.xlsx')
},
content = function(file){
write_xlsx(data_xi, file)
}
)
})
Here is the UI:
library(shiny)
shinyUI(fluidPage(
downloadButton("downloadData", "Download Metrics Reports")
))
I just tried to use the reticulate function in my python file and save the processed dataframe to Shiny App which can be downloads, thank you very much!
I ran an example out of your code with some adjustions (unfortunately i don't have your file) and it downloads normally a xlsx file. Add the data.frame( run_xi(26))and if this is not the problem maybe the "writexl" library can be the solution.
Hope it will help.
library(shiny)
library(reticulate)
library(writexl)
if (interactive()) {
ui <-fluidPage(
downloadButton("downloadData", "Download Metrics Reports")
)
server <- function(input,output){
data_xi <- data.frame(s = c(1:3),r = c(4:6), x =c(19:21))
output$downloadData <- downloadHandler(
filename = function(){
paste(Sys.time(), 'site_mtx.xlsx')
},
content = function(file){
write_xlsx(data_xi, file)
}
)
}
shinyApp(ui, server)
}

R shiny trigger downloadHandler on page loading

I'm building an app that operates regularly when nothing is given in a url query, however if a certain string is given there it should download the file immediately. The download works fine, but when it is run at start up it return a 'download.htm' file instead of the .csv. The reproducible example is not querying the url, but triggers in an observe:
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs()
,downloadButton("downloadData", "Download")
)
server <- function(input, output) {
data <- mtcars
observe({
print("click MacClickFace")
runjs("document.getElementById('downloadData').click();")
})
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(data, file)
}
)
}
shinyApp(ui, server)
How can you trigger the download at launch or are there some security things going on here?
Probably the downloadHandler is not ready. You can use a setTimeout(..., 0):
runjs("setTimeout(function(){document.getElementById('downloadData').click();},0);")

Use custom visual in Shiny with adaptive constraints

Say I wanted to use a custom image or shapefile in an interactive environment (like R Shiny) such as this image of a paper airplane:
I would also be willing to draw the image myself in the program to allow for full control.
But the overall goal would be to allow a user to drag the edges of the image and the program could keep track of the size of the changes for each dimension (say wingspan of the paper airplane)
Would Shiny be a possibility here, or do I need to use another program? I would also want some statistics of the changes available to the user.
Does anyone have any similar examples of such a thing, or can point me in the right direction?
Like i wrote in the comment you could use the shinyjqui package and read the session info of the user.
A reproducible example can be found below:
library(shiny)
library(shinyjqui)
library(ggplot2)
server <- function(input, output, session){
global <- reactiveValues(width = 400, height = 400)
observe({
print(session)
if(!is.null(session$clientData[["output_plot1_height"]])){
global$height <- session$clientData[["output_plot1_height"]]
global$width <- session$clientData[["output_plot1_width"]]
}
})
output$plot1 <- renderImage({
outfile <- tempfile(fileext='.png')
png(outfile, width = global$width, height = global$height)
hist(rnorm(200))
dev.off()
list(src = outfile)
}, deleteFile = TRUE)
output$clientdataText <- renderText({
paste("height is ", global$height, "width is", global$width)
})
}
ui <- fluidPage(
verbatimTextOutput("clientdataText"),
jqui_resizabled(plotOutput("plot1"))
)
shinyApp(ui, server)

Shiny downloadButton and downloadLink default directory or filename

The download handler is a remarkably popular shiny tool - even for local work. However it is quite annoying when it opens up all the time in the R-Studio directory. Here is the current example program from the Shiny documantion.
library(shiny)
## Only run examples in interactive R sessions
if (interactive()) {
ui <- fluidPage(
downloadLink("downloadData", "Download")
)
server <- function(input, output) {
# Our dataset
data <- mtcars
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(data, file)
}
)
}
shinyApp(ui, server)
}
It opens like this:
A few related questions:
Is there anyway to override that directory to something more reasonable?
Can I specify the default name anywhere (the default is a bit bizarrely take from the name of the output list name).
Any insights as to why this is so would be welcome. There seems to be nothing in the docs around this.

"Download" button opens a new app window without downloading - Shiny

My "download" button does not work as expectation. It opens a new app window every time I click on it. I am wondering why it functions in this way?
download function in server.R:
output$down_load <- downloadHandler(
# specify the file name
filename = function() {
paste('cls_result_export', Sys.Data(),'.csv', sep='')
},
# Write the plot back
content = function(file){
write.csv(cls_output()$raw_data, file)
}
)
download function in ui.R:
downloadButton(outputId = "down_load", label = "Download the CLS Raw Data")
Try using an actionButton wired to an observe clause like this:
library(shiny)
ui <- fluidPage( actionButton("dodo", "Download" ) )
server <- function(input, output)
{
observe({
if (input$dodo>0){
fname <- paste0('cls_result_export', Sys.Date(),'.csv')
write.csv(mtcars,fname)
}
})
}
shinyApp(ui = ui, server = server)
Another possible way to try to fix this issue, is to include this line in your server.R script:
outputOptions(output, 'down_load', suspendWhenHidden=FALSE)

Resources