How to read an Excel file from a folder without specifying the filename? - r

Is there a way to read an Excel file into R directly from a folder without having to specify the filename?
I'm using the readxl library and I have only 1 file in the folder, but the file's name changes monthly.
Thanks!

The computer need to have the path anyway BUT you can get it without giving if you are absolutly sure that this is the only one file in your folder
see https://stat.ethz.ch/R-manual/R-devel/library/base/html/list.files.html
to learn more about how to open a directory and getting filename inside.
If this isn't the only file but this is the only excel file you while have to get the extension of each file and do some parsing to take a decision of wich file you want to open

As noted in other answers, this can be solved using the dir function.
Assuming the variable path contains the path of the directory in which the XLSX file is located, then the following will give you the full path to the file:
file_path = dir(path, pattern = '\\.xlsx$', full.names = TRUE)
Note that pattern uses regular expressions rather than glob format! Using the glob pattern *.xlsx might appear to work but it’s incorrect, only works by accident, and will also match other file names.

Suppose your file is located inside a folder called myFolder located in disk E:\\
library(readxl)
folderPath <- "E://myFolder"
fileName <- grep(".xlsx", dir(folderPath), value = TRUE)
filePath <- paste(folderPath, fileName, sep = "//")
df <- read_xlsx(filePath)
This code will get the name of your xlsx file inside folderPath each time you run it and then you can import it with readxl::read_xlsx. I assume there is only one xlsx file inside the folder.

Related

excel_sheets produces: Error: `path` does not exist: ‘AE800’ but rstudio has the file saved in my environment and I can open file

I am still new to R and I am not sure if I am missing something simple. I can open the excel file in R and it displays only the first tab/sheet. I know the path exists because I have the file currently on R and I copied the path right from the "copy path" option on the document. The file is also in my set working directory. Image of rstudio below. Thank you.
I suggest this workflow instead:
path <- "C:\\Users\\spice\\......." # use your real path here
shnms <- sheet_names(path)
alldata <- lapply(shnms, function(nm) read_excel(path, sheet=nm))
and you'll get a named list, each element is a worksheet in the original workbook.

Is there a way to load csv files saved in different folders with only a partial file name in R

I am trying to load multiple csv files that are each saved in different folders within my working directory in R. However I only know part of each of the file name.
For example the file in "folder1" will be named "xxx_xxx_folder1.csv", and the file in "folder2" is "xxx_xxx_folder2.csv" etc. There is only one csv in each folder.
I was wondering is there a way to load files saved in different folders with only a partial file name?
The only way I have got it to partially work so far is to have all the files in one folder
Thanks and sorry if any of this is unclear!
From your description you could use list.files with option recursive=TRUE to get a list of your csv files. You could then loop over the list to read your files:
fn <- list.files(PATH_TO_WORKING_DIRECTORY, "\\.csv$", recursive = TRUE, full.names = TRUE)
lapply(fn, read.csv)

Cannot import csv file into R

I'm trying to import the csv files from my working directory. There are 3 such files, but for some reason R insists on recognizing only one of them. I can't determine what the pattern is, and if the recognized file is moved out of the folder then nothing is recognized. Here is my code:
files = list.files(pattern="*\\.csv$")
Each of the files is for sure a csv file which I confirmed by inspecting the "Type" column in the windows folder navigator, and to be safe I also saved a copy as CSV and still had the same problem.
Is there an aspect to this I'm unaware of?
Thanks!
The issue turned out to be that the file extension for the file that worked was ".csv" and for the ones that didn't was ".CSV". I do not know how or why something like that can happen, but the pattern parameter of the list.files function is case sensitive.
Using the parameter setting ignore.case = TRUE solved this issue.

Need help manipulating directory string in a function

I'm having trouble getting the current the current directory of my R Shiny app. I use the getSrcDirectory function to get the script's current directory and then try to manually modify it into the appropriate format to pass to xlsx::loadWorkbook, in order to load a .xlsx file. Here is the pertinent code:
#get the directory of the script by creating an empty function.
src<-getSrcDirectory(function(x) {x})
wb <- loadWorkbook(file = c(gsub("/", "\\\\", c(src, "/www/NJ2012.xlsx")),
"\\www\\NJ2012.xlsx"))
I'm trying to get the file directory to look like this: C:\\Users\\misha\\Desktop\\Accessible Project\\R_Econ_App\\www\\NJ2012.xlsx.
My script is in a folder called R_Econ_App, so it should be included in the src variable. I concatenated an additional string to the src string to provide some more information about where certain files are in certain sub directories. I use gsub in an attempt to replace "/" with "\" because that's what the file directory passed into loadWorkbook has to look like.
Can you please help me figure out where my mistake is and how to fix it?
Don't bother with gsub. Just use file.path to join your directory and file path
loadWorkbook(file=file.path(src, "www", "NJ2012.xlsx"))

Retrieve input file path on Shiny R Application

I have a Shiny Application in which the user browse for a file. Once the file is selected, I want it to be read into a table. The file is xlsx format, and it has two sheets, so I am using the function readWorksheetFromFile(file path, sheet = 1) to read in this table. In Shiny when you input a file, the line of code:
input$file$datapath gives you the path of a temporary location for that file. For some reason, this path is not valid in the readWorksheetFromFile function. Is there a way to get the exact file path of a file the user selects in R?
The input$file$datapath provide a path to the temporary filename without the .xlsx extension. I think that could be the reason. You can rename it and try again.

Resources