Reading accented filenames in R using list.files

Viewed 1380

I am reading county geojson files provided here into R Studio (R 3.1, Windows 8) for each of the states. I am using list.files() function in R.

For state PR, which has many counties with accented (Spanish) names viz. Bayamón.geo.json, Añasco.geo.json. The function list.files() returns shortened form of file names like An~asco.geo.json, Bayamo´n.geo.json.

And when in the next step I try to read the actual file using above filenames. I get an error that these files don't exist.

I was using system default encoding ISO-8859-1 and also tried changing it to UTF-8, but no luck.

Please help me solve this issue. How can I read files with accented filenames?

2 Answers

I had the same problem and I guess it happened because the default system language on my computer was different from the filenames I wanted to convert (e.g. system language = English, filename = written in french). Finally, the code below helped me to change filenames.

FILENAME_OLD is the full path for original files e.g. "C:/directory/file.wav"

FILENAME_NEW is the full path for new filenames e.g. "C:/directory/file_new.wav"

######### change filenames with non-english characters
path = "C:/directory"
setwd(path)

test_old <- Sys.glob('C:/directory/*')
test_new <- gsub("FILENAME_OLD",
                 "FILENAME_NEW", test_old)

file.rename(test_old, test_new)

Solution 1

Use Sys.glob() instead of list.files()

For your exemple, if you put USA as your working directory, you can type : Sys.glob(paths="./PR/*") to obtain a complete list, with accents, of the files in the "PR" folder.

If you want to check all files in all the working directory folder, you can type :

Sys.glob(paths=paste0(list.dirs(),"/*"))

In this code, list.dirs() is used to obtain the list of folders. paste0(list.dirs(),"/*") simply appends "/*" to every folder path, so the function Sys.glob will recursively list files in every folders and subfolders.

Solution 2

If the folders have accents, it will NOT work. Then I would recommend to use the package fs. In this package, the function dir_ls() should work. You need to install the fs package (install.packages("fs") and load it with library(fs)), then the following code should work :

dir_ls(recurse=TRUE)

The recurse=TRUE option allows you to list files in subfolders.

Documentation for the fs package :

https://cran.r-project.org/web/packages/fs/vignettes/function-comparisons.html

https://fs.r-lib.org/

Documentation on the dir_ls function : https://www.rdocumentation.org/packages/fs/versions/1.5.0/topics/dir_ls

Related