JuliaInterop / RCall.jl

Call R from Julia
Other
318 stars 59 forks source link

"is.na() applied to non-(list or vector) of type 'language'" when passing a Julia's boolean variable to R's smooth.spline #499

Open szcf-weiya opened 10 months ago

szcf-weiya commented 10 months ago

I encounter the problem when passing a boolean variable to smooth.spline.

julia> a = true

julia> x = rand(100);

julia> y = x;

julia> R"smooth.spline($x, $y, cv=$a)"
RObject{VecSxp}
Call:
smooth.spline(x = `#JL`$x, y = `#JL`$y, cv = `#JL`$a)

Smoothing Parameter  spar= 0.2607728  lambda= 7.905905e-08 (18 iterations)
Equivalent Degrees of Freedom (Df): 43.86631
Penalized Criterion (RSS): 1.62104e-30
Error showing value of type RObject{VecSxp}:
ERROR: REvalError: Warning in is.na(cv) :
  is.na() applied to non-(list or vector) of type 'language'
Warning in if (!is.na(cv)) cat(if (cv) "PRESS(l.o.o. CV): " else "GCV: ",  :
  the condition has length > 1 and only the first element will be used
Warning in if (cv) "PRESS(l.o.o. CV): " else "GCV: " :
  the condition has length > 1 and only the first element will be used
Error in if (cv) "PRESS(l.o.o. CV): " else "GCV: " : 
  argument is not interpretable as logical

It works well if with R's own boolean variable TRUE

julia> R"smooth.spline($x, $y, cv=TRUE)"
RObject{VecSxp}
Call:
smooth.spline(x = `#JL`$x, y = `#JL`$y, cv = TRUE)

Smoothing Parameter  spar= 0.2607728  lambda= 7.905905e-08 (18 iterations)
Equivalent Degrees of Freedom (Df): 43.86631
Penalized Criterion (RSS): 1.62104e-30
PRESS(l.o.o. CV): 1.721696

The error message indicates that it failed at the print.smooth.spline step (https://github.com/wch/r-source/blob/1af6b4aa417e106d63687992e229f461d45a30d9/src/library/stats/R/smspline.R#L319-L321)

image

The fitting procedure is OK. And if I append ; to hide the message, no error is thrown,

julia> R"smooth.spline($x, $y, cv=$a)";

I'm quite curious about why the error is triggered. How should we fix it?

I am using Julia 1.8.1 + RCall v0.13.14 (R3.6.3)

palday commented 9 months ago

Can you try using the most recent version of RCall (0.13.17)? I don't get an error message with RCall 0.13.17 and R 4.2. I'm guessing it's easier to upgrade RCall than R and the relevant part of the R printing source hasn't changed:

    if (!is.na(cv <- x$cv)) 
        cat(if (cv) 
            "PRESS(l.o.o. CV): "
        else "GCV: ", format(x$cv.crit, digits = digits), "\n", 
            sep = "")
szcf-weiya commented 9 months ago

I tried

Both throw the same error message