Skip to content

Commit 331b778

Browse files
committed
Last version before migrating on Rulia package
1 parent 10dd6a3 commit 331b778

File tree

5 files changed

+35
-21
lines changed

5 files changed

+35
-21
lines changed

R/jl4R.R

+9-8
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,24 @@ jl <- function(obj, ..., name_class = TRUE) {
1313

1414
jlR <- function(obj, ..., name_class = TRUE) {
1515
name <- deparse(substitute(obj))
16-
if (name_class && !(is.variable(name, parent.frame()))) {
16+
if (name_class && !is.variable(name, parent.frame())) {
1717
res <- jl_rexpr(substitute(obj), ...)
1818
if (!is.null(res)) return(toR(res))
1919
}
2020
toR(jlvalue_or_jlexception(name, jlvalue(obj, ...)))
2121
}
2222

23-
jl2 <- function(obj, ..., name_class = TRUE) {
24-
if (name_class && !(deparse(substitute(obj)) %in% ls(parent.frame()))) {
25-
return(jl_rexpr2(substitute(obj), parent.frame()))
23+
jl2 <- function(obj, ..., parent_envir = parent.frame(), name_class = TRUE) {
24+
name <- deparse(substitute(obj))
25+
if (name_class && !is.variable(name, parent_envir)) {
26+
return(jl_rexpr2(substitute(obj), parent_envir))
2627
}
27-
jlvalue(obj)
28+
jlvalue_or_jlexception(name, jlvalue(obj, ...))
2829
}
2930

30-
jl2R <- function(obj, ..., name_class = TRUE) {
31-
if (name_class && !(deparse(substitute(obj)) %in% ls(parent.frame()))) {
32-
res <- jl_rexpr2(substitute(obj), parent.frame())
31+
jl2R <- function(obj, ..., parent_envir = parent.frame(), name_class = TRUE) {
32+
if (name_class && !is.variable(name, parent_envir)) {
33+
res <- jl_rexpr2(substitute(obj), parent_envir)
3334
if (!is.null(res)) return(toR(res))
3435
}
3536
toR(jlvalue(obj, ...))

R/jlValue.R

+7-12
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ jlvalue.default <- function(expr, ...) {
55
NULL
66
}
77

8-
## Used to eval `<julia expression>` in jl function
9-
jlvalue_eval <- function(obj, ...) {
10-
if (length(obj) == 1 && is.character(obj)) {
11-
.jlvalue_eval_addclass(obj)
12-
} else {
13-
warning("Bad input for .jlvalue_eval function!")
14-
NULL
15-
}
8+
########################
9+
## eval functions
10+
## 1) jlvalue mode
11+
## IMPORTANT: the user knows that the argument is a character and the content is a valid julia code
12+
jlvalue_eval <- function(expr) {
13+
.jlvalue_eval_addclass(expr)
1614
}
1715

16+
## 2) jl mode: test on length on obj and jlexception
1817
jleval <- function(obj, ...) {
1918
if (length(obj) == 1 && is.character(obj)) {
2019
jlval <- .jlvalue_eval_addclass(obj)
@@ -25,10 +24,6 @@ jleval <- function(obj, ...) {
2524
}
2625
}
2726

28-
jlvalue_eval_unsafe <- function(expr) {
29-
.jlvalue_eval_addclass(expr)
30-
}
31-
3227
jlvalue_invisible <- function(jlval) {
3328
if(jlvalue_callR("isnothing", jlval)) {
3429
invisible(jlval)

R/jl_utils.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jlsymbol <- function(field) {
99
res
1010
}
1111

12-
jlcolon <- function() jlvalue_eval_unsafe(":")
12+
jlcolon <- function() jlvalue_eval(":")
1313

1414
jlisstructtype <- function(jlval) {
1515
jltrycall("isstructtype", jlval)

tests/testthat/test-func.R

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
test_that("jl(typeof)(1.0) %<:% Number works", {
2+
expect_jlequal(jl(typeof)(1.0) %<:% Number, "true")
3+
})
4+
5+
test_that("1.0 %isa% Number works", {
6+
expect_jlequal(1.0 %isa% Number, "true")
7+
})
8+
9+
test_that('"1.0" %isa% Number works', {
10+
expect_jlequal("1.0" %isa% Number, "false")
11+
})

tests/testthat/test-stat.R

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test_that("data.frame works", {
2+
expect_jlequal(jl(data.frame(a=1:2, b=c(TRUE,FALSE))), "2×2 DataFrame\n Row │ a b\n │ Int64 Bool\n─────┼──────────────\n 1 │ 1 true\n 2 │ 2 false")
3+
})
4+
5+
test_that("factor works", {
6+
expect_jlequal(jl(factor(c("titi","toto","titi"))),"3-element CategoricalArray{String,1,UInt32}:\n \"titi\"\n \"toto\"\n \"titi\"")
7+
})

0 commit comments

Comments
 (0)