Closed durraniu closed 3 years ago
You have defined the arguments in the Julia function as positional arguments. If you use named arguments in R, they will be used as keyword arguments in Julia. But your function does not accept keyword arguments. You need to define it like said in the error message via
apply_wiedemann(; OPDVadd=2.0, FAKTORVmult=0.02,....
Notice the semicolon in front? Then Julia can find the method.
Thanks for your reply. I modified the function as follows:
apply_wiedemann_julia <- juliaEval('function apply_wiedemann_julia(;df, V_DESIRED, FAKTORVmult, BMAXmult, BNULLmult,
L, W, AXadd, BXadd, angular_vel_threshold, EXadd, OPDVadd)
## function continued as before ##')
However, running the function throws a different error:
apply_wiedemann_julia(df = sdata,
V_DESIRED = 30,
FAKTORVmult = 0.02,
BMAXmult = 0.08,
BNULLmult = 0.25,
L = unique(sdata$LV_length_m)[2],
W = unique(sdata$LV_width_m)[2],
AXadd = 5,
BXadd = 5,
angular_vel_threshold = 0.0001,
EXadd = 2,
OPDVadd = 2)
Error in handleCallbacksAndOutput() : Evaluation failed
Original error:
MethodError: no method matching getindex(::Main.RConnector.RDataFrame, ::Int64, ::String)
Closest candidates are:
getindex(::Union{Tables.AbstractColumns, Tables.AbstractRow}, ::Int64) at C:\Users\umair\.julia\packages\Tables\UxLRG\src\Tables.jl:174
getindex(::Union{Tables.AbstractColumns, Tables.AbstractRow}, !Matched::Symbol) at C:\Users\umair\.julia\packages\Tables\UxLRG\src\Tables.jl:175
Stacktrace:
[1] Main.RConnector.Fail(::String, ::MethodError) at C:\Users\umair\Documents\R\win-library\4.0\JuliaConnectoR\Julia\reading.jl:6
[2] evaluate_checked!(::Main.RConnector.Call) at C:\Users\umair\Documents\R\win-library\4.0\JuliaConnectoR\Julia\evaluating.jl:57
[3] serve_repl(::Sockets.TCPSocket) at C:\Users\umair\Documents\R\win-library\4.0\JuliaConnectoR\Julia\communicating.jl:103
[4] serve(::Int64; keeprunning::Bool, portfile::String) at C:\Users\umair\Documents\R\win-library\4.0\JuliaConnectoR\Julia\communicating.jl:73
[5] top-level
The error is thrown because you try to index an RConnector.RDataFrame
object and this is not defined.
You index the df
via df[2,"frames"]
. When you pass a data frame object to Julia, it is translated to an RConnector.RDataFrame
object. This is an object that satisfies the Julia Tables interface. It is not a DataFrame
object from the DataFrames
package. You can, however, create such an object easily by calling
df = DataFrames.DataFrame(df)
Then you can index the resulting object.
I will think about whether it would make sense to add indexing to the RConnector.RDataFrame
objects.
Thank you very much for your help! It works now.
However, strangely, it is slower than the same function in R. With juliaCall package it is faster but needs to have the dataframe assigned beforehand. I guess my best bet is to do everything in julia.
Transferring data from R to Julia needs extra time. If you assign the data frame beforehand, it will be faster later on because the data does not need to be communicated later.
juliaEval("using DataFrames")
df <- juliaCall("DataFrames.DataFrame", sdata)
apply_wiedemann_julia(df = df, ... )
For getting the best performance, the amount of data that needs to be communicated between R and Julia must be minimised. In your case, you could, e. g., read the data directly in Julia.
Thank you so much. This has been a very pleasant learning experience.
Thank you for your package. Please help with my question as follows.
Goal
I have written a function in julia that I want to now use in R. I have tested the function in julia, it works fine. But I think I am not using
JuliaConnectoR
the correct way. Please show me the correct way of using my julia function in R.Data and julia function
If you need to get the data, you can download it here.
Evaluating the function in R
You can see the method error as follows: