Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a soft symbolic link from R on Windows

Tags:

r

symlink

I'd like to create a soft symbolic link to a file from within R on Windows (with Mklink). It fails because I cannot tell R to "run it as administrator". Is there any way I can do this?

I did manage to create hard symbolic file links, however:

path_src <- file.path(tempdir(), "test.txt")
write("Hello World!", file = path_src)
path_tgt <- file.path(tempdir(), "test_symlink.txt")
shell(sprintf("mklink /H %s %s", 
  normalizePath(path_tgt, mustWork = FALSE),
  normalizePath(path_src)
))

Note how the file at path_tgt reflects the changes made to path_src:

write("HELLO WORLD!", file = path_src, append = TRUE)

Yet, this fails:

path_tgt_2 <- file.path(tempdir(), "test_symlink_2.txt")
> shell(sprintf("mklink /D %s %s", 
  normalizePath(path_tgt_2, mustWork = FALSE),
  normalizePath(path_src)
))
Ihre Berechtigungen reichen nicht aus, um diesen Vorgang auszufhren.
Warning messages:
1: running command 'C:\Windows\system32\cmd.exe /c mklink /D C:\Users\Thyson\AppData\Local\Temp\Rtmpum73ZU\test_symlink_2.txt C:\Users\Thyson\AppData\Local\Temp\Rtmpum73ZU\test.txt' had status 1 
2: In shell(sprintf("mklink /D %s %s", normalizePath(path_tgt_2, mustWork = FALSE),  :
  'mklink /D C:\Users\Thyson\AppData\Local\Temp\Rtmpum73ZU\test_symlink_2.txt C:\Users\Thyson\AppData\Local\Temp\Rtmpum73ZU\test.txt' Ausführung mit Fehlerkode 1 fehlgeschlagen

Note

Due to a German version of Windows I can't seem to get the errors in English. The first line translates to somewhat along the lines of "You don't have enough authorization in order to carry out this process"

like image 346
Rappster Avatar asked Mar 03 '15 18:03

Rappster


People also ask

Can you create symlinks in Windows?

Windows 11, 10, 8, 7, and Vista all support symbolic links — also known as symlinks — that point to a file or folder on your system. You can create them using the Command Prompt or a third-party tool called Link Shell Extension.

How do I create a soft symbolic link?

Use the -s option to create a soft (symbolic) link. The -f option will force the command to overwrite a file that already exists. Source is the file or directory being linked to. Destination is the location to save the link – if this is left blank, the symlink is stored in the current working directory.

How do I find soft link in Windows?

In Command Prompt, run this command: dir /AL /S c:\ A list of all of the symbolic links in the c:\ directory will be returned.

Do Linux symbolic links work on Windows?

Most operating systems offer support for Symbolic Links in one way or another. Linux and Windows both provide support for generic Symbolic Links with some OS exclusive features, i.e. Windows allows for the creation of Junction points which are folder soft links with a little different working.


2 Answers

Run R as administrator. Then when you run "Mklink" from within R, you are the administrator.

Actually, you can also use the R function file.symlink to create symbolic links.

like image 51
Ven Yao Avatar answered Oct 20 '22 09:10

Ven Yao


Since 2016 (Windows 10 Insiders build 14972), you can also make symbolic links without administrator rights if you enable the developer mode (in Settings>Update and Security>For developers).

This works in the Windows console with mklink, but it doesn't seem that R's file.symlink() works (as of R 4.0.3), possibly because of the dwFlags that needs to be set.

For calling it from R, I needed to add " when the path contains spaces:

shell(sprintf('mklink "%s" "%s"', 
              normalizePath(link, mustWork = FALSE),
              normalizePath(original)
))

For directories, you can also consider the use of Sys.junction() which is somewhat equivalent to symlinks.

like image 32
Alexlok Avatar answered Oct 20 '22 10:10

Alexlok