JuliaInterop / JuliaCall

Embed Julia in R
https://non-contradiction.github.io/JuliaCall/index.html
Other
267 stars 36 forks source link

How can Julia read variables inside R function? #101

Open changy12 opened 5 years ago

changy12 commented 5 years ago

Dear "JuliaCall" developer:

I run the following code in my Rstudio: install.packages("JuliaCall")

devtools::install_github("Non-Contradiction/JuliaCall") # get the development version of JuliaCall library(JuliaCall) julia=julia_setup(JULIA_HOME = "/Applications/Julia-1.1.app/Contents/Resources/julia/bin") f=function(k){ julia_command("@rget k;") return(k) } f(3)

The error comes:

Error: Error happens in Julia. BoundsError Stacktrace: [1] getindex at /Users/chenziyi/.julia/packages/RCall/ffM0W/src/methods.jl:520 [inlined] [2] getindex(::Ptr{EnvSxp}, ::Symbol) at /Users/chenziyi/.julia/packages/RCall/ffM0W/src/methods.jl:523 [3] getindex(::RObject{EnvSxp}, ::Symbol) at /Users/chenziyi/.julia/packages/RCall/ffM0W/src/methods.jl:524 [4] top-level scope at none:0 [5] eval(::Module, ::Any) at ./boot.jl:328 [6] eval_string(::String) at /Library/Frameworks/R.framework/Versions/3.5/Resources/library/JuliaCall/julia/setup.jl:195 [7] docall(::Ptr{Nothing}) at /Library/Frameworks/R.framework/Versions/3.5/Resources/library/JuliaCall/julia/setup.jl:168

I found "@ rget" can read the global variables (defined before entering R functions) into Julia, but cannot read the local variables defined after entering R functions. How can I read local variables?

Thank you.

devtools::session_info() for my Rstudio Session info -------------------------------------------------------------------------------------------------- setting value
version R version 3.5.1 (2018-07-02) system x86_64, darwin15.6.0
ui RStudio (1.1.456)
language (EN)
collate en_US.UTF-8
tz America/New_York
date 2019-04-14

Packages ------------------------------------------------------------------------------------------------------ package version date source
base
3.5.1 2018-07-05 local
coda 0.19-1 2016-12-08 CRAN (R 3.5.0)
compiler 3.5.1 2018-07-05 local
curl 3.2 2018-03-28 CRAN (R 3.5.0)
datasets
3.5.1 2018-07-05 local
devtools 1.13.6 2018-06-27 CRAN (R 3.5.0)
digest 0.6.18 2018-10-10 cran (@0.6.18)
git2r 0.23.0 2018-07-17 CRAN (R 3.5.0)
graphics 3.5.1 2018-07-05 local
grDevices
3.5.1 2018-07-05 local
grid 3.5.1 2018-07-05 local
httr 1.3.1 2017-08-20 CRAN (R 3.5.0)
JuliaCall 0.16.5 2019-04-12 Github (Non-Contradiction/JuliaCall@09c0d96) knitr 1.22 2019-03-08 cran (@1.22)
LaplacesDemon 16.1.1 2018-06-30 CRAN (R 3.5.0)
lattice 0.20-35 2017-03-25 CRAN (R 3.5.1)
memoise 1.1.0 2017-04-21 CRAN (R 3.5.0)
methods
3.5.1 2018-07-05 local
mvtnorm 1.0-8 2018-05-31 CRAN (R 3.5.0)
parallel 3.5.1 2018-07-05 local
R6 2.2.2 2017-06-17 CRAN (R 3.5.0)
Rcpp 1.0.1 2019-03-17 cran (@1.0.1)
RcppZiggurat 0.1.5 2018-06-10 CRAN (R 3.5.0)
Rfast 1.9.1 2018-07-20 CRAN (R 3.5.0)
stats
3.5.1 2018-07-05 local
tools 3.5.1 2018-07-05 local
utils * 3.5.1 2018-07-05 local
withr 2.1.2 2018-03-15 CRAN (R 3.5.0)
xfun 0.6 2019-04-02 cran (@0.6)
yaml 2.2.0 2018-07-25 CRAN (R 3.5.0)

Non-Contradiction commented 5 years ago

Thanks for the feedback! Currently, there is no easy way to get local variables in R directly. However, there are some possible ways to achieve that effect. Maybe you can try julia_assign, which can create a global variable in Julia from an R variable. Or you can create a Julia command string with the R variable and then evaluate the command string in Julia. A more complicated but performant way is to write the Julia functions you need, and then call it with some R variables by julia_call. I hope these methods work for you.

changy12 commented 5 years ago

The following R code perfectly, directly and easily transfers the local variable "var_R" in R into the Julia variable called "var_julia"

julia_assign("var_julia", var_R)

Thank you quite much!