Setting Working Directory: Julia Versus R

Setting working directory: Julia versus R

The idiom is just different as you can see from the source. If you invoke cd() without arguments, it defaults to the home directory. The function homedir() can be used to prepend the home directory.

julia> homedir()
"/Users/jeffw"

julia> cd("/")

julia> pwd()
"/"

julia> cd()

julia> pwd()
"/Users/jeffw"

Combining things

julia> cd("$(homedir())/Desktop")

julia> pwd()
"/Users/jeffw/Desktop"

Analog of r-here or py-here for Julia

Based on the description, it sounds like DrWatson.jl does what you're looking for. From the website:

[DrWatson] is a Julia package created to help people increase the consistency of their scientific projects, navigate them and share them faster and easier, manage scripts, existing simulations as well as project source code. DrWatson helps establishing reproducibility, and in general it makes managing a scientific project a simple job.

Like the description implies, it's more ambitious than here seems to be, having functionality to also manage data, simulation runs, etc. But they're optional, and you can use only the directory handling part if you need.

The Navigating a Project describes the projectdir function which works similar to here. projectdir("foo", "bar") resolves to foo/bar under the current project's root directory, just like with here.

There's also datadir(), srcdir(), and others to directly handle common subdirectories under a project for eg. datadir("foo", "test.jld2") resolves to data/foo/test.jld2 under the project's root directory.

In R, do an operation temporarily using a setting such as working directory

There is an idiom for this in some of R's base plotting functions

op <- par(no.readonly = TRUE)

# par(blah = stuff)
# plot(stuff)

par(op)

that is so unbelievably crude as to be fully portable to options() and setwd().

Fortunately it's also easy to implement a crude wrapper:

with_dir <- function(dir, expr) {
old_wd <- getwd()
setwd(dir)
result <- evalq(expr)
setwd(old_wd)
result
}

I'm no wizard with nonstandard evaluation so evalq could be unstable somehow. More on NSE in an old write-up by Lumley and also in Wickham's Advanced R, but it's dense stuff and I haven't wrapped my head around it all yet.

edit: as per Ben Bolker's comment, it's probably better to use on.exit for this:

with_dir <- function(dir, expr) {
old_wd <- getwd()
on.exit(setwd(old_wd))
setwd(dir)
evalq(expr)
}

From the R docs:

on.exit records the expression given as its argument as needing to be executed when the current function exits (either naturally or as the result of an error). This is useful for resetting graphical parameters or performing other cleanup actions.

How does julia know what path separator and root directory to use?

It uses the Sys.isunix and Sys.iswindows functions in order to conditionally define the correct path_separator_re variables, etc.

  • https://github.com/JuliaLang/julia/blob/5c3f58039525972b24930f356821af8299f70a26/base/path.jl#L19-L41

if Sys.isunix()
# ...
const path_separator_re = r"/+"
# ...

splitdrive(path::String) = ("",path)
elseif Sys.iswindows()
# ...
const path_separator_re = r"[/\\]+"
# ...

function splitdrive(path::String)
m = match(r"^([^\\]+:|\\\\[^\\]+\\[^\\]+|\\\\\?\\UNC\\[^\\]+\\[^\\]+|\\\\\?\\[^\\]+:|)(.*)$", path)
String(m.captures[1]), String(m.captures[2])
end
else
error("path primitives for this OS need to be defined")
end

For the root directory, check out the homedir function, which uses libuv to determine it.

  • https://github.com/JuliaLang/julia/blob/5c3f58039525972b24930f356821af8299f70a26/base/path.jl#L52-L77

help?> homedir
search: homedir

homedir() -> AbstractString
Return the current user's home directory.

| Note
|
| homedir determines the home directory via libuv's uv_os_homedir. For details (for example on how to specify the home
| directory via environment variables), see the uv_os_homedir documentation.



Related Topics



Leave a reply



Submit