Is there any equivalent to R's rm(list=ls()) (which removes every object in the global environment) in Julia?
It is currently not possible. See Delete full workspace or one variable in julia for the explanation of the current status of the issue (in short - the currently recommended practice is to wrap the code you use in a module and Julia will allow you to replace this module).
Related
In R you can search the documentation by typing a question mark ? or a double question mark ??. How do you search the manual for strings in the Julia REPL?
>?first
No documentation for ‘first’ in specified packages and libraries:
you could try ‘??first’
>??first
In the R console a browser window opens up:
In RStudio the page is opened within the IDE.
The help() function and ? help operator in R provide access to the
documentation pages for R functions, data sets, and other objects,
both for packages in the standard R distribution and for contributed
packages.
The help() function and ? operator are useful only if you already know the name of the function that you wish to use. Other ways to search include apropos and ??
The apropos() function searches for objects, including functions,
directly accessible in the current R session that have names that
include a specified character string.
The ?? operator is a synonym for help.search(). The help.search()
function scans the documentation for packages installed in your
library. The argument to help.search() is a character string or regular expression.
P.S. I intend to answer my own question.
Julia has similar interactive utilties. Julia's main search utility for docstrings is named apropos.
To search the documentation for information about "first" in Julia, you have apropos("first") or equivalently ?"first". Thus ?"first" is roughly equivalent to R's ??.
To search for functions and methods, you can type a singe question mark ?, just as with R. In the Julia REPL, as you type the question mark, the prompt changes to a question mark. If you type "first" it searches through strings, while if you type first without quote-marks, you get a search over variables exported by the modules currently loaded.
Illustration:
help?>"first"
# or equivalently:
julia>apropos("first")
help?>first
If you search for a string, case is ignored. If you want to search within the DataFrames module, type using DataFrames before searching.
This also works in Visual Studio Code and Atom:
I need to know how to create a library in Julia and where I must keep it in order to call it later. I come from C and matlab, it seems there is no documentation about pratical programming in Julia.
Thanks
If you are new to Julia, you will find it helpful to realize that Julia has two mechanisms for loading code. Stating you "need to know how to create a library in Julia" would imply you most likely will want to create a Julia module docs and possibly a packagedocs. But the first method listed below may also be useful to you.
The two methods to load code in Julia are:
1. Code inclusion via the include("file_path_relative_to_call_or_pwd.jl")docs
The expression include("source.jl") causes the contents of the file source.jl to be evaluated in the global scope of the module where the include call occurs.
Regarding where the "source.jl" file is searched for:
The included path, source.jl, is interpreted relative to the file where the include call occurs. This makes it simple to relocate a subtree of source files. In the REPL, included paths are interpreted relative to the current working directory, pwd().
Including a file is an easy way to pull code from one file into another one. However, the variables, functions, etc. defined in the included file become part of the current namespace. On the other hand, a module provides its own distinct namespace.
2. Package loading via import X or using Xdocs
The import mechanism allows you to load a package—i.e. an independent, reusable collection of Julia code, wrapped in a module—and makes the resulting module available by the name X inside of the importing module.
Regarding the difference between these two methods of code loading:
Code inclusion is quite straightforward: it simply parses and evaluates a source file in the context of the caller. Package loading is built on top of code inclusion and is quite a bit more complex.
Regarding where Julia searches for module files, see docs summary:
The global variable LOAD_PATH contains the directories Julia searches for modules when calling require. It can be extended using push!:
push!(LOAD_PATH, "/Path/To/My/Module/")
Putting this statement in the file ~/.julia/config/startup.jl will extend LOAD_PATH on every Julia startup. Alternatively, the module load path can be extended by defining the environment variable JULIA_LOAD_PATH.
For one of the simplest examples of a Julia module, see Example.jl
module Example
export hello, domath
hello(who::String) = "Hello, $who"
domath(x::Number) = x + 5
end
and for the Example package, see here.
Side Note There is also a planned (future) library capability similar to what you may have used with other languages. See docs:
Library (future work): a compiled binary dependency (not written in Julia) packaged to be used by a Julia project. These are currently typically built in- place by a deps/build.jl script in a project’s source tree, but in the future we plan to make libraries first-class entities directly installed and upgraded by the package manager.
When I develop a R package I endup doing the following thing:
load the latest build
use it
realize there is a bug in say function 'f_bug'
try to debug
I would be easy if I could just 're-source' f_bug and that the newly sourced version would be chosen (I'd rebuild the package clean latter).
But I cannot do that, it looks like the package::f_bug is always "chosen" by default when called within another package function.
Can I do such a thing ?
You can't use RStudio's convenient graphical breakpoints, but you can do the same thing using trace:
trace(package::f_bug, browser, at = insertion_point)
Here insertion_point refers not to line numbers but to a vector of substeps. From ?trace:
look at ‘as.list(body(f))’ to get the numbers
associated with the steps in function ‘f’.)
Another option might be to use utils::setBreakpoint which takes a file name and line number as arguments. See the help file for details.
I'm writing some fairly involved R code spread across multiple files and collected together into a package. A problem I've run into on occasion is that I will define a utility function in one file that has the same name as another utility function defined in another file. One of the two definitions gets replaced, leading to unintended behavior. Is there any sort of tool to check for this kind of accidental redefinition? Something that would check that no two top-level assignments foo <- ... in the package assign to the same name?
As pointed out in the comments, the right way to do this is to use packages. Packages give functions their own namespaces automatically, plus they make it very easy to reuse and share code. If you're using RStudio, you can create one with very little effort from the New Project menu.
However, if you can't use packages or namespaces for some reason, there's still a way to do what you want: you can lock a variable (including a function) so that it's not possible to overwrite it.
> pin <- 11
> lockBinding("pin", .GlobalEnv)
> pin <- 12
Error: cannot change value of locked binding for 'pin'
See Binding and Environment Locking for details.
Am using EMACS/ESS as editor for R.
I find it helpful to refer to a function defined outside of base with it's relevant namespace; as well as being good practice in general, it seems to be necessary when running R CMD check on a package.
I really like autocomplete in EMACS and am wondering if there's a way to extend the functionality to include namespace when autocomplete-ing the name of a function.
For example (in R):
library(stats)
Then in ESS when I start typing dn the autocomplete dnorm appears (greyed out) and I can complete it by pressing TAB.
What would be better is to complete as stats::dnorm or even stats:::dnorm so that I don't need to manually check whether the function I'm using is in base. (For a relatively new user, memorizing the names of all functions in base may be a lot to ask).
Details:
EMACS: 2012-06-10 on MARVINGNU Emacs 24.1.1 (i386-mingw-nt6.1.7601)
ESS version 12.04-4
Icicles (default install c. Oct 2012). Not sure how to find version info. for this.
If this doesn't already exist, any pointers would be welcome. Note this is closely related but if the answer is already there then I'm not quite getting it...