forked from JuliaInterop/RCall.jl
/
basic.jl
105 lines (87 loc) · 2.55 KB
/
basic.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
lsv = reval("ls()")
@test length(lsv) == 0
@test isa(lsv, RObject{StrSxp})
lsd = reval("ls(\"package:datasets\")")
@test isa(lsv, RObject{StrSxp})
@test length(lsd) > 50
@test "airmiles" in rcopy(lsd)
langsexp = rlang(:solve, RObject([1 2; 0 4]))
@test length(langsexp) == 2
@test rcopy(reval(langsexp)) == [1 -0.5; 0 0.25]
@test rcopy(langsexp[1]) == :solve
langsexp[1] = RObject(:det)
langsexp[2] = RObject([1 2; 0 0])
@test rcopy(reval(langsexp))[1] == 0
globalEnv[:x] = RObject([1,2,3])
@test rcopy(globalEnv[:x]) == [1,2,3]
globalEnv[:y] = RObject([4,5,6])
@test rcopy(rcall(Symbol("+"),:x,:y)) == [5,7,9]
x = 1:10
@rput x
@rget x
@test isa(x,Vector{Int32})
@test all(x .== 1:10)
y = "foo"
@rput x y::StrSxp
@rget x y::Array{String}
@test isa(y,Vector{String})
@test y[1] == "foo"
@test sprint(io -> rprint(io,RObject([1,2,3]))) == "[1] 1 2 3\n"
@test rcopy(rcall(:besselI, 1.0, 2.0)) ≈ besseli(2.0,1.0)
@test rcopy(rcall(:besselI, 1.0, 2.0, var"expon.scaled"=true)) ≈ besselix(2.0,1.0)
@test isna(R"list(a=1, b=NA)") == [false, true]
@test isna(R"list(a=1, b=NA)", 1) == false
@test isna(R"list(a=1, b=NA)", 2) == true
# callbacks
function testfn(x,y;a=3,b=4)
[x;y;a;b]
end
r = rcall(testfn, 1, 2)
@test isa(r,RObject{IntSxp})
@test rcopy(r) == [1,2,3,4]
r = rcall(testfn, 1, 2,b=6)
@test isa(r,RObject{IntSxp})
@test rcopy(r) == [1,2,3,6]
r = rcall(:optimize,sin,[-2,0])
@test isapprox(r[:minimum][1], -pi/2, atol=eps()^0.25)
r = rcall(:optimize,sin,[0,2],maximum=true)
@test isapprox(r[:maximum][1], pi/2, atol=eps()^0.25)
nullfn() = nothing
@test isa(rcall(nullfn), RObject{NilSxp})
# graphics
RCall.rgui_init()
f = tempname()
rcall(:png,f)
rcall(:plot,1:10)
rcall(Symbol("dev.off"))
@test isfile(f)
@test !RCall.rgui_start(true)
@test_throws ErrorException RCall.rgui_start()
@test RCall.rgui_stop()
# S4 rprint
@test contains(sprint(io ->
rprint(io, reval("""
setClass("Foo", representation(x = "numeric"))
foo <- new("Foo", x = 20)
"""))), "An object of class")
# S3 rprint
@test contains(sprint(io ->
rprint(io, reval("""
print.Bar <- function(x) print("hello")
bar <- 1
class(bar) <- "Bar"
bar
"""))), "hello")
# operators
a = reval("a=c(1,2,3)")
b = reval("b=c(4,5,6)")
@test rcopy(a+b)==rcopy(R"a+b")
@test rcopy(a-b)==rcopy(R"a-b")
@test rcopy(a*b)==rcopy(R"a*b")
@test rcopy(a/b)==rcopy(R"a/b")
@test rcopy(a^b)==rcopy(R"a^b")
# misc
iris = rcopy(reval(:iris))
model = R"lm(Sepal_Length ~ Sepal_Width,data=$iris)"
@test rcopy(RCall.getclass(model)) == "lm"
@test isapprox(rcopy(R"sum($iris$Sepal_Length)"), sum(iris[:Sepal_Length]), rtol=4*eps())