JuliaInterop / RCall.jl

Call R from Julia
Other
318 stars 59 forks source link

Error from $ operator for component extraction #292

Open gtollefson opened 5 years ago

gtollefson commented 5 years ago

When I try to run the following line of R code I receive the error I've pasted below.

My reval code works when I exclude the following line which features $ operator extraction. I've tried a few syntax variation for processing in Julia which I've pasted at the very bottom. The code works in the R REPL. I'm using RCall v"0.10.6" on Julia 0.6.3.

h$x$data[[length(h$x$data)]]$marker$colorbar$ticktext <- genotypes produces

ERROR: LoadError: UndefVarError: x not defined Stacktrace: [1] genotype_heatmap2(::Array{Int64,2}, ::String, ::String, ::Array{Symbol,2}, ::Array{Any,2}, ::String) at /Users/George/projects_code/ViVa.jl/src/plot_utils.jl:66 [2] include_from_node1(::String) at ./loading.jl:576 [3] include(::String) at ./sysimg.jl:14 [4] process_options(::Base.JLOptions) at ./client.jl:305 [5] _start() at ./client.jl:371 while loading ...

I also tried:

h[["x"]][["data"]][[length(h[["x"]][["data"]])]][["marker"]][["colorbar"]][["ticktext"]] <- genotypes 

produces

ERROR: LoadError: LoadError: LoadError: UndefVarError: @x_str not defined Stacktrace: [1] include_from_node1(::String) at ./loading.jl:576 [2] include(::String) at ./sysimg.jl:14 [3] include_from_node1(::String) at ./loading.jl:576 while loading /Users/George/projects_code/ViVa.jl/src/plot_utils.jl, in expression starting on line 1 while loading /Users/George/projects_code/ViVa.jl/src/ViVa.jl, in expression starting on line 78 while loading /Users/George/projects_code/ViVa.jl/viva_cli.jl, in expression starting on line 11

h[\"x\"][\"data\"][[length(h[\"x\"][\"data\"])]][\"marker\"][\"colorbar\"][\"ticktext\"] <- genotypes produces

Warning in h["x"]["data"][[length(h["x"]["data"])]]["marker"]["colorbar"]["ticktext"] <- genotypes : number of items to replace is not a multiple of replacement length

h[[\"x[\"]][[\"]data[\"]][[length(h[[\"]x[\"]][[\"]data[\"]])]][[\"]marker[\"]][[\"]colorbar[\"]][[\"]ticktext[\"]] <- genotypes produces

ERROR: LoadError: REvalError: Error in h[["x["]][["]data["]][[length(h[["]x["]][["]data["]])]][["]marker["]][["]colorbar["]][["]ticktext["]] <- genotypes : attempt to select less than one element in integerOneIndex while loading /Users/George/projects_code/ViVa.jl/viva_cli.jl, in expression starting on line 312

Non-Contradiction commented 5 years ago

The first error is because "$x" means string interpolation in Julia, so Julia wants to find a variable with the name of x to fill into the string.

The second error is that the way the quotation mark is used let Julia think the first x is outside of the first quotation mark pairs, and it is just before another quotation mark, so Julia will treat it like x"some string". This is in fact the string macro in Julia, and Julia will try to find a macro definition like @x_str.

I think the third error is because of the misuse of [[]] and []. In R, x$a is equivalent to x[['a']], which is a single element extraction, but not x['a'], which is a subset extraction. They can have subtle but important differences when x is a list or a data.frame.

I think there are some error in the fourth try, because its intention looks different with the first three. In the error information, you can see how RCall is interpreting the command you give.

And there is a small tip I always use, since R doesn't distinguish between pairs of ' and ", you can use ' in your R command to avoid things like \".