Creating a soft symbolic link from R on Windows
Asked Answered
K

2

7

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"

Kanara answered 3/3, 2015 at 18:9 Comment(3)
For translations, try: Sys.setenv(LANGUAGE='en')Hagerty
This seems post to suggest it's not really possible without add-ons.Hagerty
@Thomas: thanks for the pointer! Kind of sucks that it requires an add-on, but better than nothing ;-) PS: I do have LANGUAGE = "en" (it's my general setting in the .Renviron file for all of my project) and all my other "within R" errors are in fact in Englisch, but the shell() command seems to delegate everything to the basic OS level somehow.Kanara
C
2

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.

Carpous answered 13/10, 2015 at 17:45 Comment(3)
And how do I explicitly run R as admin? When I open R from within RStudio while being logged in as a Windows user with admin rights, it does not workKanara
Try to run RStudio as the administrator.Carpous
Even if you has logged in as a Windows user with admin rights, to run RStudio as administrator, you should right click the icon of Rstudio on the Desktop and then choose "Run as administrator".Carpous
T
1

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.

Tripartition answered 23/2, 2021 at 19:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.