...
Hello!
I am getting stuck with the 'downloadHandler()' function.
The script below is a ShinyApp small test where
- FIRST PRIORITY
I would like to
- download a formatted text in a word document (.docx) without losing the text style/layout used.
- download a table in different file format such as .txt (comma delimited or tab delimited file), .pdf, .word , excel file.
- download a plot in a .pdf, .word, .png file.
The script I wrote was able to display all the format options to choose and the the download button but when I select each of the formats and I click download, It opens a 'Save File' window with a 'DownloadData' name in the File name tab without the user's selected extension. Moreover nothing happens after clicking save the file named DownloadData.
I am developing the ShinyApp using RStudio on Windows but once it is done, It will run on a Linux system too.
Is the ShinyApp script written like below for downloading reports interchangeable between Windows and Linux?
Many thanks in advance for all precious help!!
# Load library
library(shiny)
library(rmarkdown)
# Create a data-table to use as input in the ShinyApp
dat <- data.frame("NGS" = c("WES", "TDS"), "MEITL" = c(6,1), "EATL" = c(3, 2), "IPTCL" = c(4, 3), stringsAsFactors = FALSE)
rownames(dat) <- dat[,1]
dat[,1] <- NULL
# Building ShinyApp TEST
# UI section
ui <- fluidPage(
titlePanel(p(h1("Test app"),"by", strong("Ter"))),
theme = shinythemes::shinytheme("slate"),
sidebarLayout(
sidebarPanel(
selectInput('Tumor','Choose tumor type', choices = c('MEITL','EATL','IPTCL')),
selectInput('Data','Select data',choices = c('WES','TDS')),
radioButtons('format', 'Document format:', c('PDF', 'HTML', 'Word', "csv", "tsv"), inline = TRUE),
downloadButton("DownloadData", "Download")
),
mainPanel(
tabsetPanel(
tabPanel("Nf of samples",h3(textOutput('Nb_sample'))),
tabPanel("Table", br(), tableOutput('table'))
)
)
)
)
# SERVER section
server = function(input, output, session) {
output$Nb_sample <- renderText({
paste("The number of", input$Tumor, "with", input$Data, "analysis are", dat[rownames(dat) == input$Data, colnames(dat) == input$Tumor])
})
output$table <- renderTable({
dat
}, rownames = TRUE)
# Download files
output$DownloadData <- downloadHandler(
filename= function(){
paste('My-report', sep = ".", switch(
input$format, PDF = 'pdf', HTML = 'html', Word = 'docx', csv = 'txt', tsv = 'txt'
))
},
content = function(file){
# download the table 'dat' in comma OR tab delimited format
# following the example in 'https://github.com/rstudio/shiny-examples/blob/master/039-download-file/server.R'
# Write to a file specified by the 'file' argument
write.table(dat, file, sep = switch(input$format, "csv" = ",",
"tsv" = "\t"),
row.names = TRUE)
# download the formatted text 'Nb_sample' in word OR pdf
# OR HTML format
rmarkdown::render(Nb_sample,
output_format = switch(input$format,
PDF = pdf_document(), HTML = html_document(),
Word = word_document()),
output_file = file)
})
}
shinyApp(ui = ui, server = server)