Getting file path from Shiny UI (Not just directory) using browse button without uploading the file
This functionality is available in the shinyFiles
package. Have a look at this minimal example:
library(shiny)
library(shinyFiles)
ui <- fluidPage(
shinyFilesButton("Btn_GetFile", "Choose a file" ,
title = "Please select a file:", multiple = FALSE,
buttonType = "default", class = NULL),
textOutput("txt_file")
)
server <- function(input,output,session){
volumes = getVolumes()
observe({
shinyFileChoose(input, "Btn_GetFile", roots = volumes, session = session)
if(!is.null(input$Btn_GetFile)){
# browser()
file_selected<-parseFilePaths(volumes, input$Btn_GetFile)
output$txt_file <- renderText(as.character(file_selected$datapath))
}
})
}
shinyApp(ui = ui, server = server)
Specify destination folder for download from an R Shiny app
You're passing reactive(file <- input$file1)
as the contentType
argument to downloadHandler()
, which can't be good. Also, you're not writing anything to the file
given as an argument to the content
function.
Remove the reactive(file <- input$file1)
line, and specify output_file = file
in rmarkdown::render()
, and your download should work.
As discussed in the comments, you won't be able to have control over the download path though -- that's something the user's web browser and their settings there will decide.
Here's a somewhat more minimal app with a functioning file download, for reference:
library(shiny)
ui <- fluidPage(
sliderInput("value", "Some value", 1, 5, 2),
downloadButton("report", "Generate report")
)
server <- function(input, output) {
output$report <- downloadHandler(
filename = "wordreport.doc",
content = function(file) {
params <- list(value = input$value)
rmarkdown::render(
system.file("examples/knitr-minimal.Rmd", package = "knitr"),
output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
}
shinyApp(ui, server)
Is there an equivalent to the Shiny fileInput function for saving outputs?
You can use downloadButton
in ui
and downloadHandler
in server
as follows:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV file to upload", accept = ".csv"),
checkboxInput("header", "Header", TRUE),
downloadButton("download")
),
mainPanel(
tableOutput("contents"),
)
)
)
server <- function(input, output) {
output$contents <- renderTable({
file <- input$file1
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "csv", "Please upload a csv file"))
read.csv(file$datapath, header = input$header)
})
output$download <- downloadHandler(
filename = function() {
paste0(input$file1, ".csv")
},
content = function(file) {
write.csv(contents(), file)
}
)
}
shinyApp(ui, server)
Related Topics
Extracting Orthogonal Polynomial Coefficients from R's Poly() Function
Navlistpanel: Make Tabs Sequentially Active in Shiny App
Pivot_Wider, Count Number of Occurrences
Check to See If a Value Is Within a Range
Changing Multiple Column Values Given a Condition in Dplyr
R Shiny - Uioutput Not Rendering Inside Menuitem
How to Sort Data by Column in Descending Order in R
Nested Facet Plot with Ggplot2
Flip Facet Label and X Axis with Ggplot2
Generate All Combinations, of All Lengths, in R, from a Vector
Intersecting Points and Polygons in R
Change Color Median Line Ggplot Geom_Boxplot()
Remove Duplicate Rows from Xts Object
Installing Rcppeigen on Amazon Ec2