Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error putting R object back into R #405

Closed
azev77 opened this issue Feb 15, 2021 · 2 comments
Closed

Error putting R object back into R #405

azev77 opened this issue Feb 15, 2021 · 2 comments

Comments

@azev77
Copy link

azev77 commented Feb 15, 2021

I want to wrap a simple ML package: sprintr.
In R the code is straight-forward:

library(sprintr); 
set.seed(123);
m = cv.sprinter(x = X, y = y, square = FALSE, nlam = 100, lam_min_ratio = 0.01)
pred = predict(m, newdata = XH)

In Julia it works in 1-step:

X = randn(100,5)
y = 7.0 .+ X[:,1] .+ 2.0 .* X[:,2] .* X[:,3] + 3*randn(100)
using RCall

function sprintr(X, y, XH)
  @rput y X XH # put data from Julia to R.
  R"""
  library(sprintr); set.seed(123);
  m = cv.sprinter(x = X, y = y, square = FALSE, nlam = 100, lam_min_ratio = 0.01)
  pred = predict(m, newdata = XH)
  """
  return @rget pred  
end
aaaa  = sprintr(X, y, X)

It breaks in 2-steps:

function fitsprintr(X, y)
  @rput y X # put data from Julia to R.
  R"""
  library(sprintr); set.seed(123);
  m = cv.sprinter(x = X, y = y, square = FALSE, nlam = 100, lam_min_ratio = 0.01)
  """
  return @rget m  
end
function predictsprintr(m, XH)
  @rput m XH # put data from Julia to R.
  R"""
  library(sprintr); set.seed(123);
  #m = cv.sprinter(x = X, y = y, square = FALSE, nlam = 100, lam_min_ratio = 0.01)
  pred = predict(m, newdata = XH)
  """
  return @rget pred  
end




julia> ms = fitsprintr(X, y)
OrderedCollections.OrderedDict{Symbol,Any} with 12 entries:
  :n       => 100
  :p       => 5
  :a0      => 6.88335
  :compact => [0.0 1.0 0.981719; 0.0 2.0 0.801296;  ; 3.0 5.0 0.0285869; 2.0 3.0 1.46116]
  :fit     => OrderedCollections.OrderedDict{Symbol,Any}(:n=>100,:p=>5,:a0=>[6.91957, 6.91288, 6.91104, 6.91314, 6.91492, 6.91654, 6.91801, 6.91434, 6.91025, 6.90653    7.21865, 7.21867, 7.21869, 7.21871, 7.21873, 7.21874, 7.
  :fitted  => [4.00406, 5.49018, 6.72736, 8.49206, 7.45682, 2.46833, 6.28563, 5.69402, 6.96177, 7.84651    6.00993, 7.37945, 7.11813, 7.2942, 5.92529, 8.71639, 7.63545, 5.86258, 7.62806, 6.30384]
  :lambda  => [1.74206, 1.5873, 1.44629, 1.3178, 1.20073, 1.09406, 0.996869, 0.90831, 0.827618, 0.754095    0.000402438, 0.000366686, 0.000334111, 0.00030443, 0.000277385, 0.000252743, 0.00023029, 0.000209831, 0.000191191, 0.
  :cvm     => [3.90709, 3.86828, 3.81234, 3.7361, 3.64678, 3.55014, 3.46599, 3.39412, 3.33097, 3.28259    3.33651, 3.33662, 3.33672, 3.33682, 3.3369, 3.33698, 3.33705, 3.33711, 3.33717, 3.33722]
  :cvsd    => [0.425856, 0.42167, 0.417286, 0.399112, 0.384055, 0.366016, 0.353135, 0.345567, 0.341965, 0.33731    0.627155, 0.627222, 0.627282, 0.627338, 0.627388, 0.627434, 0.627476, 0.627514, 0.627549, 0.62758]
  :foldid  => [4, 4, 1, 3, 3, 1, 3, 5, 2, 3    3, 1, 2, 2, 1, 2, 1, 5, 2, 3]
          => 

julia> predictsprintr(ms, X)
ERROR: MethodError: no method matching sexpclass(::Expr)
Closest candidates are:
  sexpclass(::UInt8) at /Users/AZevelev/.julia/packages/RCall/eRsxl/src/convert/default.jl:236
  sexpclass(::Dates.Date) at /Users/AZevelev/.julia/packages/RCall/eRsxl/src/convert/default.jl:260
  sexpclass(::Dates.DateTime) at /Users/AZevelev/.julia/packages/RCall/eRsxl/src/convert/default.jl:265
  ...
Stacktrace:
 [1] sexp(::Expr) at /Users/AZevelev/.julia/packages/RCall/eRsxl/src/convert/default.jl:214
 [2] setindex!(::Ptr{VecSxp}, ::Expr, ::Int64) at /Users/AZevelev/.julia/packages/RCall/eRsxl/src/methods.jl:188
 [3] sexp(::Type{RCall.RClass{:list}}, ::OrderedCollections.OrderedDict{Symbol,Any}) at /Users/AZevelev/.julia/packages/RCall/eRsxl/src/convert/base.jl:281
 [4] sexp at /Users/AZevelev/.julia/packages/RCall/eRsxl/src/convert/default.jl:214 [inlined]
 [5] setindex!(::Ptr{VecSxp}, ::OrderedCollections.OrderedDict{Symbol,Any}, ::Int64) at /Users/AZevelev/.julia/packages/RCall/eRsxl/src/methods.jl:188
 [6] sexp(::Type{RCall.RClass{:list}}, ::OrderedCollections.OrderedDict{Symbol,Any}) at /Users/AZevelev/.julia/packages/RCall/eRsxl/src/convert/base.jl:281
 [7] sexp at /Users/AZevelev/.julia/packages/RCall/eRsxl/src/convert/default.jl:214 [inlined]
 [8] setindex!(::Ptr{EnvSxp}, ::OrderedCollections.OrderedDict{Symbol,Any}, ::Symbol) at /Users/AZevelev/.julia/packages/RCall/eRsxl/src/methods.jl:546
 [9] setindex! at /Users/AZevelev/.julia/packages/RCall/eRsxl/src/methods.jl:555 [inlined]
 [10] predictsprintr(::OrderedCollections.OrderedDict{Symbol,Any}, ::Array{Float64,2}) at ./REPL[34]:2
 [11] top-level scope at REPL[43]:1

julia> 
@azev77
Copy link
Author

azev77 commented Feb 16, 2021

The correct answer was given on Discourse. But it's very awkward:

function fitsprintr(X, y)
    @rput y X # put data from Julia to R.
    R"""
    library(sprintr);
     m <- cv.sprinter(x = X, y = y)
    """
    @rget m
    delete!(m, :call)
    delete!(m[:fit], :call)
    m
end
function predictsprintr(m, XH)
    @rput m XH # put data from Julia to R.
    R"""
    library(sprintr);
    class(m) <- 'cv.sprinter'
    pred <- predict(m, newdata = XH)
    """
    return @rget pred
end

@palday
Copy link
Collaborator

palday commented Jul 18, 2024

@azev77 I think this would be a good case for defining your own custom conversion.

@palday palday closed this as completed Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants