shinyFiles library not working with "read.csv" - r

I am trying to play with shinyFiles library and while I figured out the UI part of the shinyFiles, I can't get the server part working. Here is my code so far
library(shiny)
library(shinyFiles)
ui <- shinyUI(pageWithSidebar(
headerPanel(
'Selections with shinyFiles',
'shinyFiles example'
),
sidebarPanel(
shinyFilesButton('file', 'File select', 'Please select a file', FALSE)
),
mainPanel(
tags$h4('The output of a file selection'),
tableOutput("contents")
)
))
server <- shinyServer(function(input, output, session) {
shinyFileChoose(input, 'file', roots=c(wd='/home/rstudio'), filetypes=c('', 'csv'))
output$contents <- renderTable({
df <- read.csv(input$file)
print(head(df))
})
})
runApp(list(
ui=ui,
server=server
))
This is the error that I am getting
Warning: Error in read.table: 'file' must be a character string or connection
I just want to make the server side to print the header of the file that I chose with shinyFilesButton. I don't understand what I am doing wrong here.

You need to use parseFilePaths https://www.rdocumentation.org/packages/shinyFiles/versions/0.3.2/topics/parseFilePaths in order to get parse the file name. Then $datapath should be used to get the file name.
library(shiny)
library(shinyFiles)
ui <- shinyUI(pageWithSidebar(
headerPanel(
'Selections with shinyFiles',
'shinyFiles example'
),
sidebarPanel(
shinyFilesButton('file', 'File select', 'Please select a file', FALSE)
),
mainPanel(
tags$h4('The output of a file selection'),
tableOutput("contents")
)
))
server <- shinyServer(function(input, output, session) {
shinyFileChoose(input, 'file', roots=c(wd='.'), filetypes=c('', 'csv'))
output$contents <- renderTable({
inFile <- parseFilePaths(roots=c(wd='.'), input$file)
if( NROW(inFile)) {
df <- read.csv(as.character(inFile$datapath))
print(head(df))
}
})
})
runApp(list(
ui=ui,
server=server
))
Note: I changed the root path in the above code to allow it to run on my system.
This is related to: Loading data files with ShinyFiles

Related

How can I provide a location in R shiny where the submitted file should be saved?

The code below takes the user's input file (filename1) and uploads it to the same directory with a new name (example: userFile_filename1) in the same directory. I'd want to upload the input file to a separate location. In R shiny, how can I set a path of my choice?
library(shiny)
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Select file to upload' )
),
mainPanel(
h4('List of uploaded files:')
,verbatimTextOutput('fileList')
)
))
)
server <- shinyServer(function(input, output) {
observe({
if (is.null(input$file1) ) { return(NULL) }
file.copy(from = input$file1$datapath, to = paste0('userFile_',input$file1$name ) )
})
output$fileList <- renderText({
input$file1
dir(pattern = 'userFile_')
})
})
shinyApp(ui, server)

How to modifiy a file in real time using shiny?

I want to create a shiny app that do two things. Frist it has to be able to upload XLS file and after the user can add it some columns (in this case, just the column "Tecnico"), but for the moment I cant be able to do that. Once the user write the value for the column and update the table, the app crashed.
library(shiny)
library(readxl)
runApp(
list(
ui = fluidPage(
titlePanel("Use readxl"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")),textInput("tecnix","Tecnico"),
actionButton("go", "update")),
mainPanel(
tableOutput('my_output_data'))
)
),
server = function(input, output){
data1 <- reactive({
inFile <- input$file1
if (is.null(inFile)){return(NULL)}
isolate({
input$file1
my_data <- read_excel(inFile$datapath)
})
my_data
})
observeEvent(input$go, {
data1()$Técnico <- input$tecnix
})
output$my_output_data <- renderTable({data1()})
}
))
Any ideas?
Thank you so much,
As suggested by #Till using reactiveValues will help solve the problem.
library(shiny)
library(readxl)
runApp(
list(
ui = fluidPage(
titlePanel("Use readxl"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")),textInput("tecnix","Tecnico"),
actionButton("go", "update")),
mainPanel(
tableOutput('my_output_data'))
)
),
server = function(input, output){
rv <- reactiveValues(my_data = NULL)
observeEvent(input$file1, {
req(input$file1$datapath)
rv$my_data <- read_excel(input$file1$datapath)
})
observeEvent(input$go, {
rv$my_data$Techni <- input$tecnix
})
output$my_output_data <- renderTable({rv$my_data})
}
))

Display a selected image from upload in Shiny UI

I want to be able to upload multiple images with file input and display the single image selected in the UI
ui.R
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file","Upload the file", multiple = TRUE), # fileinput() function is used to get the file upload contorl option
uiOutput("selectfile")
),
mainPanel(
uiOutput('images')
)
)
)
server.R
server <- function(input,output) {
## Side bar select input widget coming through renderUI()
# Following code displays the select input widget with the list of file loaded by the user
output$selectfile <- renderUI({
if(is.null(input$file)) {return()}
list(hr(),
helpText("Select the files for which you need to see data and summary stats"),
selectInput("Select", "Select", choices=input$file$name)
)
})
output$images <- renderImage({
if(is.null(input$file)) {return(NULL)}
for (i in 1:nrow(input$file))
{
if(input$file$name[i] == input$Select){
list(src=input$file$datapath[i],
alt= "error")
print(input$file$name[i])
print(input$file$datapath[i])
}
}
})
}
With this solution, the prints of the datapath and the name shows me the right answer but i keep getting the same error after trying to render the image: "Warning: Error in basename: a character vector argument expected".
Here is a solution using base64 encoding.
library(shiny)
library(base64enc)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file", "Upload the file", multiple = TRUE),
uiOutput("selectfile")
),
mainPanel(
uiOutput('image')
)
)
)
server <- function(input,output) {
output$selectfile <- renderUI({
req(input$file)
list(hr(),
helpText("Select the files for which you need to see data and summary stats"),
selectInput("Select", "Select", choices=input$file$name)
)
})
output$image <- renderUI({
req(input$Select)
i <- which(input$file$name == input$Select)
if(length(i)){
base64 <- dataURI(file = input$file$datapath[i], mime = input$file$type[i])
tags$img(src = base64, alt= "error")
}
})
}
shinyApp(ui, server)

upload and view a pdf in R shiny

I have a simple shiny app in R for reading a PDF file from the user and display it. I can't seem to get it to work. On the shiny server in the www directory I see a 1 KB file with the name "myreport.pdf" that just has the first character
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Testing File upload"),
sidebarLayout(
sidebarPanel(
fileInput('file_input', 'upload file ( . pdf format only)', accept = c('.pdf'))
),
mainPanel(
uiOutput("pdfview")
)
)
))
server <- shinyServer(function(input, output) {
observe({
req(input$file_input)
test_file <- readBin(input$file_input$datapath, what="character")
writeBin(test_file, "www/myreport.pdf")
})
output$pdfview <- renderUI({
tags$iframe(style="height:600px; width:100%", src="myreport.pdf")
})
})
shinyApp(ui = ui, server = server)
I think the issue is with the binary reading and writing. Instead trying to copy the files using file.copy seems to work. Also I've taken the iframe inside observeEvent for the iframe to update every time the pdf is uploaded in the same session.
Updated Code:
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Testing File upload"),
sidebarLayout(
sidebarPanel(
fileInput('file_input', 'upload file ( . pdf format only)', accept = c('.pdf'))
),
mainPanel(
uiOutput("pdfview")
)
)
))
server <- shinyServer(function(input, output) {
observe({
req(input$file_input)
#test_file <- readBin(input$file_input$datapath, what="raw")
#writeBin(test_file, "myreport.pdf")
#cat(input$file_input$datapath)
file.copy(input$file_input$datapath,"www", overwrite = T)
output$pdfview <- renderUI({
tags$iframe(style="height:600px; width:100%", src="0.pdf")
})
})
})
shinyApp(ui = ui, server = server)

Get the name of uploaded file as a variable in Shiny

I am creating a Shiny App where one of the sections of a Venn Diagram will be named after the uploaded file(done by user). For example, if someone uploads a file ClientXYZ.csv, one section of the Venn diagram will be named "ClientXYZ"
Is it possible to do this in Shiny?
Its not clear without a reproducible example, but you can grab the name of the file with input and name.
library(shiny)
ui <- fluidPage(
titlePanel("Grabbing my file name"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Select your file',
accept = c(
'text/csv',
'text/comma-separated-values',
'.csv'
)
)
),
mainPanel(
textOutput("myFileName")
)
)
)
server <- function(input, output) {
file_name <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
return (stringi::stri_extract_first(str = inFile$name, regex = ".*(?=\\.)"))
})
output$myFileName <- renderText({ file_name() })
}
# Run the application
shinyApp(ui = ui, server = server)

Resources