Skip to content

Instantly share code, notes, and snippets.

@Non-Contradiction
Created March 22, 2018 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Non-Contradiction/6bd7ffcf9eb4676d5994507db21beea3 to your computer and use it in GitHub Desktop.
Save Non-Contradiction/6bd7ffcf9eb4676d5994507db21beea3 to your computer and use it in GitHub Desktop.
dll_file <- system("julia -e \'print(Libdl.dlpath(\"libjulia\"))\'", intern = TRUE)
JuliaCall:::juliacall_initialize(dll_file)
JuliaCall:::juliacall_cmd('print("1\n")')
JuliaCall:::juliacall_cmd("using RCall")
JuliaCall:::juliacall_cmd("
function transfer_list(x)
rcopy(RObject(Ptr{RCall.VecSxp}(x)))
end")
JuliaCall:::juliacall_cmd("
function error_msg(e)
m = IOBuffer()
showerror(m, e)
seek(m, 0)
readstring(m)
end")
JuliaCall:::juliacall_cmd("
function error_msg(e, bt)
m = IOBuffer()
showerror(m, e, bt)
seek(m, 0)
readstring(m)
end")
JuliaCall:::juliacall_cmd('
function Rerror(e, bt)
s1 = join(["Error happens in Julia.\n"])
s2 = error_msg(e, bt)
s = join([s1 s2])
rcall(:simpleError, s)
end')
JuliaCall:::juliacall_cmd('
function docall(call1)
try
call = transfer_list(call1)
fname = call[:fname];
named_args = call[:named_args]
unamed_args = call[:unamed_args]
need_return = call[:need_return];
show_value = call[:show_value];
if endswith(fname, ".")
fname = chop(fname);
f = eval(Main, parse(fname));
r = f.(unamed_args...);
else
f = eval(Main, parse(fname));
r = f(unamed_args...; named_args...);
end
if show_value && r != nothing
display(r)
end
# proceed(basic_display_manager)
if need_return == "R"
RObject(r).p;
elseif need_return == "Julia"
RObject(JuliaObject(r)).p
else
RObject(nothing).p;
end;
catch e
Rerror(e, catch_stacktrace()).p;
end;
end')
do_call <- function(func_name, arg_list, need_return = c("R", "Julia", "None"), show_value = FALSE){
args <- JuliaCall:::separate_arguments(arglist = arg_list)
jcall <- list(fname = func_name,
named_args = args$named,
unamed_args = args$unamed,
need_return = need_return,
show_value = show_value)
JuliaCall:::juliacall_docall(jcall)
}
JuliaCall:::juliacall_cmd("module JuliaCall docall = Main.docall end")
do_call("sqrt", list(2), need_return = "R")
## If the previous codes run withought segfault, the main function of JuliaCall should be okay
## with little or no modification of codes.
## The following ones should be okay. But it is helpful to also try these:
JuliaCall:::juliacall_cmd("using Suppressor")
JuliaCall:::juliacall_cmd("
@suppress begin
using RCall
end")
JuliaCall:::juliacall_cmd("
function exists(x)
isdefined(Symbol(x))
end")
JuliaCall:::juliacall_cmd("
function eval_string(x)
eval(Main, parse(x))
end")
JuliaCall:::juliacall_cmd("
function installed_package(pkg_name)
string(Pkg.installed(pkg_name))
end")
JuliaCall:::juliacall_cmd('
function help(fname)
string(eval_string(join(["@doc " fname])))
end')
JuliaCall:::juliacall_cmd("
function assign(name, x)
eval(Main, Expr(:(=), Symbol(name), x))
end")
JuliaCall:::juliacall_cmd("
function str_typeof(x)
string(typeof(x))
end")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment