We teach students to use R with RStudio. Every year, we have at least one or two students with special characters in their windows username. This leads to the situation as described here.
The errors are always a bit different. The latest student has this error message showing up in the RStudio GUI:
Error in nchar(homeDir): invalid multibyte string, element 1
The RStudio console has several warning messages:
Warning message:
In normalizePath(path.expand(path), winslash, mustWork) : path[1]="C:/Users/N�me/Documents
In theory R 4.2 should support utf8 encoding now but I think you need to change some encoding configuration on Windows for that to work, I haven't tested this myself so I can't give any specifics, hopefully someone else that has already tried would help you.
Does anyone know what we need to change to make RStudio work with special characters in windows usernames?
Enable the new UTF-8 option in Windows settings. Go to the language settings, click Administrative language settings, then Change system locale… and tick the Beta: Use Unicode UTF-8 for worldwide language support option. Then restart your computer.
Have in mind that this might have unexpected repercussions, for example, xlsxm files stored with a different encoding can't be open.
Also, this should handle the problem from the R side of things but RStudio initializes a lot of environment variables so I'm not sure if this also handles the problem from the RStudio side.
I'm not too comfortable changing a system-wide setting on our student's computers. It seems the culprit is the HOME Variable where R looks for .Renviron and .Rpofile files. I'm going to try and override the default HOME Variable by using the explanation provided in this SO answer:
The home directory is set as follows: If environment variable R_USER is set, its value is used. Otherwise if environment variable HOME is set, its value is used. After those two user-controllable settings, R tries to find system-defined home directories. It first tries to use the Windows "personal" directory (typically C:\Users\username\Documents). If that fails, if both environment variables HOMEDRIVE and HOMEPATH are set (and they normally are), the value is ${HOMEDRIVE}${HOMEPATH}. If all of these fail, the current working directory is used.
We solved it by creating a new Folder without speical characters C:\Users\safe-username and giving this folder write permission to all users. We also created a subfolder in "safe-username" named "R_libraries". Then we created a two new Environmental Variables on System level as follows:
R_USER: C:\Users\safe-username
R_LIBS_USER: C:\Users\safe-username\R_libraries
Now, R searches for .Renviron and .Rprofile in C:\Users\safe-username and installs R Libraries in C:\Users\safe-username\R_libraries. In this way, the special character issue is resolved. I'm not sure if this is the best method, I'm open to suggestions!