IDL equivalent of python's dir() - idl

In python, dir(X) will list the attributes and methods of object X. Is there something similar for an IDL object?

I couldn't find a command. But in the IDE command line, typing "x." and then hovering your mouse over it will show you what attributes and functions are associated with it.

Related

Knitr - Define Latex commands that call R functions using Sexpr

I'm basically wondering how to define a new Latex command such that it allows the nesting of Sexpr and some other R function, where the Latex argument is an R object.
As fortunute happenstance, the idea somewhat is transmitted by the new command structure given below:
\newcommand{\SomeLatexCommand}[1]{\Sexpr{"#1"}}
Where fortunately the argument is indeed shown, albeit in string. With this in mind, I was hoping upon the following command:
\newcommand{\SweetLatexCommand}[1]{\Sexpr{SomeRFunction(get("#1"))}}
However, once inside nested inside an R function, #1 is not read as a placeholder for the Latex argument, but instead as an existing R variable.
Is there a way to make the last comand work? Or else, are there also other neat ways to define Latex commands which in turn can call on any R function through R objects?
Good day,
No, you can't do that. The problem is the way knitr works:
R runs the knit() function (or some other knitr function). That function looks through the source for code chunks and \Sexpr calls, executes them, and replaces them with the requested output, producing a .tex file.
Then LaTeX processes that .tex file. R is no longer involved.
Since \newcommand is a LaTeX command, it is only handled in the final stage, after all R evaluation is done.
There may be a way in knitr to specify another "macro" that works the way \Sexpr works, but I don't think there's a way to have several of them.
So what you should do is write multiple functions in R, and call those to do what you want, as \Sexpr{fn1(...)}, \Sexpr{fn2(...)}, etc.
I suppose if you were really determined, you could add an extra preprocessor stage at the beginning, that went through your Rnw file and replaced all strings that looked like \SweetLatexCommand{blah} with \Sexpr{SomeRFunction(get("blah"))} and then called knit(), but that seems like way too much work.

Is there a way to run julia script with arguments from REPL?

I can run julia script with arguments from Powershell as > julia test.jl 'a' 'b'. I can run a script from REPL with include("test.jl") but include accepts just one argument - the path to the script.
From playing around with include it seems that it runs a script as a code block with all the variables referencing the current(?) scope so if I explicitly redefine ARGS variable in REPL it catches on and displays corresponding script results:
>ARGS="c","d"
>include("test.jl") # prints its arguments in a loop
c
d
This however gives a warning for redefining ARGS and doesn't seem the intended way of doing that. Is there another way to run a script from REPL (or from another script) while stating its arguments explicitly?
You probably don't want to run a self-contained script by includeing it. There are two options:
If the script isn't in your control and calling it from the command-line is the canonical interface, just call it in a separate Julia process. run(`$JULIA_HOME/julia path/to/script.jl arg1 arg2`). See running external commands for more details.
If you have control over the script, it'd probably make more sense to split it up into two parts: a library-like file that just defines Julia functions (but doesn't run any analyses) and a command-line file that parses the arguments and calls the functions defined by the library. Both command-line interface and the second script your writing now can include the library — or better yet make the library-like file a full-fledged package.
This solution is not clean or Julia style of doing things. But if you insist:
To avoid the warning when messing with ARGS use the original ARGS but mutate its contents. Like the following:
empty!(ARGS)
push!(ARGS,"argument1")
push!(ARGS,"argument2")
include("file.jl")
And this question is also a duplicate, or related to: juliapassing-argument-to-the-includefile-jl as #AlexanderMorley pointed to.
Not sure if it helps, but it took me a while to figure this:
On your path "C:\Users\\.julia\config\" there may be a .jl file called startup.jl
The trick is that not always Julia setup will create this. So, if neither the directory nor the .jl file exists, create them.
Julia will treat this .jl file as a command list to be executed every time you run REPL. It is very handy in order to set the directory of your projects (i.e. C:\MyJuliaProject\MyJuliaScript.jl using cd("")) and frequent used libs (like using Pkg, using LinearAlgebra, etc)
I wanted to share this as I didn't find anyone explicit saying this directory might not exist in your Julia device's installation. It took me more than it should to figure this thing out.

How to print in REPL the code of functions in Julia?

In Julia, a lot of the Base and closer related functions are also written in pure Julia, and the code is easily avaible. One can skim through the repository or the local downloaded files, and see how the function is written/implemented. But I think there is allready some built in method that does that for you, so you can write in REPL or Jupyter Notebook something like:
#code functioninquestion()
and get something like:
functioninquestion(input::Type)
some calculations
return
end
without pagingh throug the code.
I just don't remember the method or call. I have read the Reflection/Introspection section of the Manual but I cannot seem to be able to use anything there. I've tried methods, methodswith, code_lowered, expand and cannot seem to make them give what I want-
This is not currently supported but probably will be in the future.
Though this may not be what the OP is looking for, #less is very convenient to read the underlying code (so I very often use it). For example,
julia> #less 1 + 2
gives
+(x::Int, y::Int) = box(Int,add_int(unbox(Int,x),unbox(Int,y)))
which corresponds to the line given by
julia> #which 1 + 2
+(x::Int64, y::Int64) at int.jl:8
#edit functioninquestion() will open up your editor to the location of the method given.
It probably wouldn't be to hard to take the same information used by #edit and use it to open the file and skip to the method definition, and then display it directly in the REPL (or Jupyter).
EDIT: While I was answering, somebody else mentioned #less, which seems to do exactly what you want already.
There is now another tool for this, https://github.com/timholy/CodeTracking.jl. It is part of Revise.jl (and works better when also using Revise). It should work inside Jupyter and with functions defined in the REPL, unlike #edit/#less.

Can you save your session in Julia

Im very new to Julia and was trying to save my session (all the values, including functions for example) and didnt see any easy way. There seems to be a pretty complete low level write function for ints, floats, arrays, etc. But it doesnt, for example, write a DataFrames. Is there an easy way to do this or do I need to code all this from scratch? Im using V0.2.1.
Have you tried using the iJulia notebook? This might be useful for what you're describing. https://github.com/JuliaLang/IJulia.jl
You can do this with HDF5.jl. I don't know how well it works for functions, but it should work fine for data frames and any other native Julia type.
For functions you want to keep, I would probably just define them in a regular .jl file and include("def.jl") at the start of the session, for example.
Checkout out the Julia Data Format https://github.com/JuliaIO/JLD.jl
It can both save specific julia types as well as types you created yourself, and has macros to save your entire workspace at once.
I think it can be in Julia Data format (JLD).
https://github.com/JuliaIO/JLD.jl
If you have own data fromat e.g. type model
type Model
version::String
id::String
equations::Vector{Equation}
coefs::Vector{Matrix}
end
You can save it with command
using JLD
save("MODEL.jld", "modelS", model1)
and read as
pathReport = joinpath(homedir(),".julia/v0.5/foo/test")
m = JLD.load(joinpath(pathReport, "MODEL.jld"))
model2 = m["modelS"]
model2.equations[1].terms[2] == "EX_01"

Passing arguments to interactive mode

I need to understand an R script. Since I did not use R until now, I try to understand the script step by step. At the beginning of the script command line arguments (input files) are passed with commandArgs(). I know that one can access additional arguments for an R script with commandArgs().
But I just cannot figure out how to run a script with arguments in the interactive mode, so that I can print all variables used in the script later on. For example source("script.R") does not seem to take arguments.
My apologies if I am just not capable of using the right search query...
I think you're misunderstanding the use of commandArgs - it's for getting the arguments supplied when run through the command line... not the interpreter. If you just want to "supply arguments" when sourcing a file then just put those into the global namespace (just create the variables you want to use). Using source is almost just like copying the script and pasting it into the interpreter.

Resources