-1

I like to download a file in shiny which is created by base64enc::base64decode.

What I have so far is:

library(base64enc)
library(shiny)
downloadHandler(
  filename = function()
    "test.txt",
  content = function(file) {
    base64decode(what = "VGhpcyBpcyBhIHRlc3Qu", output = file)
  }
)

and I get Warning: Error in file: argument "file" is missing, with no default

When I use base64decode without shiny, I use:

base_string <- "VGhpcyBpcyBhIHRlc3Qu"
o_file <- file("C:/User/Desktop/test.txt"), "wb")
base64decode(what = base_string, output = o_file)
close(o_file)

and everything works fine.

Is it possible to use the downloadHandler without executing the second statement first? I want to create the file just for the download.

Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
Jensxy
  • 119
  • 1
  • 14

1 Answers1

0

If we look into the documentation of ?downloadHandler we see that the content parameter requires a file path (string) of a nonexistent temp file and writes the content to that file path.

If we look into the code of base64decode:

if (is.character(output)) {
    output <- file(output, "wb")
    on.exit(close(output))
}

we see that file() is called, so that you would create/open a connection to a file already and the condition of "non-existance" of that file wouldn´t be fulfilled (my understanding).

Maybe, you could use smthg like:

write.csv(base64decode(what = base_string), file)

Full app:

library(base64enc)
library(shiny)
ui <- fluidPage(
  downloadLink("downloadData", "Download")
)

server <- function(input, output) {
  # Our dataset
  data <- mtcars

  output$downloadData <- downloadHandler(
    filename = function() {
      paste("data-", Sys.Date(), ".stackoverflow", sep="")
    },
    content = function(file) {
      base_string <- "VGhpcyBpcyBhIHRlc3Qu"
      write.table(base64decode(what = base_string), file)
    }
  )
}

shinyApp(ui, server)

Edit: Given your question in the comment. You can use write.table() for an arbitrary file type. See the edited example above to write to a file of type .stackoverflow ;).

Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
  • Thank you for your anwser. But that would only work for csv files or not? I want to do this for an arbitrary file type. – Jensxy Aug 12 '18 at 16:27
  • Hmm, then the file seems to be broken when I want to open it. – Jensxy Aug 12 '18 at 16:35
  • Cant reproduce that. I´m on windows and if i want to open it i´m asked to choose a program and can open it with the editor. Which OS are you using and what is your desired file type? – Tonio Liebrand Aug 12 '18 at 16:46
  • I am on linux (Ubuntu 16). My desired file type is pdf or docx. – Jensxy Aug 12 '18 at 16:58
  • ok, in your question above, you have `.txt`. So it´s kind of a new qurstion. To output a pdf file there are questions already: https://stackoverflow.com/questions/40420450/how-to-download-a-pdf-file-in-a-shiny-app and https://stackoverflow.com/questions/46281137/reporters-package-to-download-docx-report-from-shiny. You can combine it with base64decode of course. – Tonio Liebrand Aug 12 '18 at 17:13
  • Thank you for your effort. But I need a general approach for all file types and I don't want to save the file on the server first. – Jensxy Aug 12 '18 at 17:51