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 vialibuv
'suv_os_homedir
. For details (for example on how to specify the home
| directory via environment variables), see theuv_os_homedir
documentation.
Related Topics
Is the Plyr Package for R Not Available for R Version 3.0.2
Adjusting the Node Size in Igraph Using a Matrix
R: Find Missing Columns, Add to Data Frame If Missing
Pivot_Longer Multiple Variables of Different Kinds
Split Column in Data.Table to Multiple Rows
How to Run a Function Every Second
Keep All Plot Components Same Size in Ggplot2 Between Two Plots
Dplyr:How to Find the First-Non Missing String by Groups
Installing R Studio with Anaconda
Dplyr . and _No Visible Binding for Global Variable '.'_ Note in Package Check
How to Use an R Script from Github
Labelling the Plots with Images on Graph in Ggplot2
Adding All Elements of Two Lists
R - Set Execution Time Limit in Loop