Using WSL2 directories in R

48 Views Asked by At

I'm using Windows Subsystem Linux 2 (WSL2) in Windows 10 and it works perfectly from the Ubuntu Terminal.

I want to work some files inside the WSL2' directory (//wsl.localhost/Ubuntu/home/forever/tesis/) using WSL2 from R.

So, I tested the functionality of wsl using the shell() function in R.

shell(cmd = "cd")
# C:\Users\ivanl\Documents

it works perfect, but when I tried to access the WSL2' directories it return errors

shell(cmd = "cd //wsl.localhost/Ubuntu/home/forever/tesis/")
# El sistema no puede encontrar la ruta especificada.
# In shell(cmd = "cd //wsl.localhost/Ubuntu/home/forever/tesis/") :
#    'cd //wsl.localhost/Ubuntu/home/forever/tesis/' execution failed with error code 1


shell(cmd = "cd /mnt/c")
# El sistema no puede encontrar la ruta especificada.
# Warning message:
# In shell(cmd = "cd /mnt/c") :
#    'cd /mnt/c' execution failed with error code 1

El sistema no puede encontrar la ruta especificada means "The system can not find the specified path"

The next example doesn't return an error, but it stay processing forever, and it force me to stop it.

shell( cmd = "wsl cd /mnt/c/home/forever/tesis")

If I try to access the WSL2' directory (//wsl.localhost/Ubuntu/home/forever/tesis/) from R (not using shell()) there is no problem, I can read, modify files and all works fine.

I'm out of ideas to solve this, hope you can help me.

1

There are 1 best solutions below

0
NotTheDr01ds On

Some theory, some experience here. Taking an education guess at the first part - As it is, I believe that Windows R is calling CMD for the shell(). This is evidenced by the fact that cd is return the current directory in your "working" example. Only CMD does this - PowerShell returns a noop with just a cd.

With that in mind, the problem you are experiencing is probably because CMD does not support UNC paths (even though R itself does).

Possible solutions (untested, since I don't have R installed):

  • Map the WSL share to a drive letter. The mapping will need to go to the top-level share (e.g., \\wsl.localhost\Ubuntu) rather than the subdirectory itself. The shared drive letter will be accessible from CMD (and thus, hopefully, R).

  • Set your shell to PowerShell. From reading the shell() doc, this might look something like:

    shell(cmd = "cd //wsl.localhost/Ubuntu/home/forever/tesis/; get-childitem", shell = "pwsh.exe")
    # or
    shell(cmd = "cd //wsl.localhost/Ubuntu/home/forever/tesis/; get-childitem", shell = "powershell.exe")
    # or perhaps
    shell(cmd = "cd //wsl.localhost/Ubuntu/home/forever/tesis/; get-childitem", shell = "C:/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe")
    

The last command, where you attempt to start WSL directly, may be failing because R automatically sets the flag for the shell to be /c (assuming, again that it is CMD or Bash). The WSL command doesn't have a /c flag itself.

If you want to try to execute WSL directly, try something like:

shell(cmd = "cd /home/forever/tesis/; pwd; ls", shell = "wsl.exe", flags = "-e bash -c")
# or potentially
shell(cmd = "cd /home/forever/tesis/; pwd; ls", shell = "wsl.exe", flags = "")