Sebastian Hesse
Sebastian Hesse

Reputation: 545

Use macs alias folders in R

I am playing around with my data storage structure and thought it a great idea to have one folder with original data and distribute it from there to r project folders via an alias folder. But it seems R doest understand alias folders as it shows it as an object but but a folder. Is there a nice way to solve this or should I simply use my mail folder as a working directory of each project and get the scripts via subfolder structures?

Google seems oblivious to this question so I thought maybe someone here can help :)

Thanks! Sebastian

Upvotes: 1

Views: 528

Answers (1)

hrbrmstr
hrbrmstr

Reputation: 78822

You should consider using cross-platform symbolic links vs the macOS native alias files created via Finder operations. Go to a Terminal/iTerm prompt and do:

$ man ln

to learn more.

However, you can work with macOS alias files from R. The version 3 binary alias file format is still kinda opaque and I didn't feel like digging into binary formats on a rainy November afternoon but you can use the mactheknife package to accomplish this via applescript:

library(mactheknife) # github or gitlab hrbrmstr/mactheknife

resolve_alias <- function(path) {
  mactheknife::applescript(sprintf('
set myPosix to POSIX file "%s"
tell application "Finder"
   set myPath to original item of item myPosix
end tell
set myPOSIXPath to POSIX path of (myPath as text)
return myPOSIXPath
', path.expand(path)))
}

(resolve_alias("~/Desktop/data-alias"))
## [1] "/Users/bob/data/"

If you can't install that package for some reason (it has some heavy dependencies for some folks), here's the source code for the applescript() function:

applescript <- function (script_source, extra_args = c(), params = c()) {
  script_source <- paste0(script_source, collapse = "\n")
  tf <- tempfile(fileext = ".applescript")
  on.exit(unlink(tf), add = TRUE)
  cat(script_source, file = tf)
  osascript <- Sys.which("osascript")
  args <- c(extra_args, tf, params)
  res <- system2(command = osascript, args = args, stdout = TRUE)
  invisible(res)
}

which is all base R with no dependencies (outside of it being macOS-specific :-)

UPDATE

I added a resolve_alias() function to the mactheknife package so now you can just do:

mactheknife::resolve_alias("path-to-alias-file")

if you can install mactheknife.

Upvotes: 2

Related Questions