how to make r read files from directories containing non-English characters

Viewed 1652

There are two folders on my desktop and they include the same files. It seems, directories (paths) containing non-English characters don't work in my current windows environment.

> setwd("C:/Users/my_user/Desktop/folder_name_including_only_english_characters")
> list.files()
[1] "test.csv"    "test.jpg"    "test.pdf"    "Test.R"      "test.sql"    "test.xlsx"
> setwd("C:/Users/my_user/Desktop/folder_name_including_non_english_characters_like_ıüö")
> list.files()
character(0)

I am not sure whether this is something related with the settings of R or windows 10.

I don't have such an issue with my other computer.

UPDATE:

During my recent tries, I have noticed that, although the list.files() function fails, R can still read files from the directories containing non-English characters using the built in read.csv2 function. However, when I tried to read xlsx files, for example:

openxlsx::read.xlsx(paste0("C:/Users/my_user/Desktop/folder_name_including_non_English_characters_like_ıüö","/test.xlsx"))

it gives the error below. (btw, openxlsx::read.xlsx() function reads the files from directories containing only English characters successfully.

Error in file(con, "r") : invalid 'description' argument
In addition: Warning message:
In unzip(xlsxFile, exdir = xmlDir) : error 1 in extracting from zip file

I suspect the reason behind both the fail of the function list.files() and the error on using openxlsx::read.xlsx() function may be the same.

My question is: what setting(s) should I change to make R successfully recognize the directories containing non-English characters?

2 Answers

After banging my heads for hours, Sys.glob(*.*) is the only possible option when list.files fail.

> Sys.setlocale(locale = 'Turkish_Turkey.1254')
[1] "LC_COLLATE=Turkish_Turkey.1254;LC_CTYPE=Turkish_Turkey.1254;LC_MONETARY=Turkish_Turkey.1254;LC_NUMERIC=C;LC_TIME=Turkish_Turkey.1254"
> setwd('C:/Users/SA31/non_english_characters_like_ıüö')
> list.files()
character(0)
> Sys.glob('*.*')
[1] "New Text Document.txt"

Updated code without setwd:

> Sys.glob('C:/Users/SA31/non_english_characters_like_ıüö/*')
[1] "C:/Users/SA31/non_english_characters_like_ıüö/New Text Document.txt"
[2] "C:/Users/SA31/non_english_characters_like_ıüö/New folder"

Changing the system locale for non-unicode programs to Turkish fixed the issue. "All Control Panel Items" > "Region and Language" > "Admimistrative" > "Change System Locale"

Related