diff --git a/R/accessing_mutating.R b/R/accessing_mutating.R index 7f1ff77..d3a7947 100644 --- a/R/accessing_mutating.R +++ b/R/accessing_mutating.R @@ -33,56 +33,48 @@ #' the length of the replacement must match the number of elements to replace. #' @name AccessMutate.JuliaProxy #' @examples -#' if (juliaSetupOk()) { +#' \dontrun{#' +#' # (Mutable) struct +#' juliaEval("mutable struct MyStruct +#' x::Int +#' end") #' -#' # (Mutable) struct -#' juliaEval("mutable struct MyStruct -#' x::Int -#' end") +#' MyStruct <- juliaFun("MyStruct") +#' s <- MyStruct(1L) +#' s$x +#' s$x <- 2 +#' s[["x"]] #' -#' MyStruct <- juliaFun("MyStruct") -#' s <- MyStruct(1L) -#' s$x -#' s$x <- 2 -#' s[["x"]] +#' # Array +#' x <- juliaCall("map", MyStruct, c(1L, 2L, 3L)) +#' x +#' length(x) +#' x[[1]] +#' x[[1]]$x +#' x[[1]] <- MyStruct(2L) +#' x[2:3] +#' x[2:3] <- MyStruct(2L) +#' x #' -#' # Array -#' x <- juliaCall("map", MyStruct, c(1L, 2L, 3L)) -#' x -#' length(x) -#' x[[1]] -#' x[[1]]$x -#' x[[1]] <- MyStruct(2L) -#' x[2:3] -#' x[2:3] <- MyStruct(2L) -#' x +#' # Tuple +#' x <- juliaEval("(1, 2, 3)") +#' x[[1]] +#' x[1:2] +#' length(x) #' -#' # Tuple -#' x <- juliaEval("(1, 2, 3)") -#' x[[1]] -#' x[1:2] -#' length(x) +#' # NamedTuple +#' x <- juliaEval("(a=1, b=2)") +#' x$a #' -#' # NamedTuple -#' x <- juliaEval("(a=1, b=2)") -#' x$a +#' # Dictionary +#' strDict <- juliaEval('Dict("hi" => 1, "hello" => 2)') +#' strDict +#' strDict$hi +#' strDict$hi <- 0 +#' strDict[["hi"]] <- 2 +#' strDict["howdy", "greetings"] <- c(2, 3) +#' strDict["hi", "howdy"] #' -#' # Dictionary -#' strDict <- juliaEval('Dict("hi" => 1, "hello" => 2)') -#' strDict -#' strDict$hi -#' strDict$hi <- 0 -#' strDict[["hi"]] <- 2 -#' strDict["howdy", "greetings"] <- c(2, 3) -#' strDict["hi", "howdy"] -#' -#' } -#' -#' \dontshow{ -#' if (exists("strDict")) { -#' rm(x, s, strDict) -#' } -#' stopJulia() #' } NULL diff --git a/man/AccessMutate.JuliaProxy.Rd b/man/AccessMutate.JuliaProxy.Rd index 09e43dd..96fca30 100644 --- a/man/AccessMutate.JuliaProxy.Rd +++ b/man/AccessMutate.JuliaProxy.Rd @@ -73,55 +73,47 @@ The dimensions of proxy objects for Julia \code{AbstractArray}s and \code{Tuple} can be queried via \code{length} and \code{dim}. } \examples{ -if (juliaSetupOk()) { - - # (Mutable) struct - juliaEval("mutable struct MyStruct - x::Int - end") - - MyStruct <- juliaFun("MyStruct") - s <- MyStruct(1L) - s$x - s$x <- 2 - s[["x"]] - - # Array - x <- juliaCall("map", MyStruct, c(1L, 2L, 3L)) - x - length(x) - x[[1]] - x[[1]]$x - x[[1]] <- MyStruct(2L) - x[2:3] - x[2:3] <- MyStruct(2L) - x - - # Tuple - x <- juliaEval("(1, 2, 3)") - x[[1]] - x[1:2] - length(x) - - # NamedTuple - x <- juliaEval("(a=1, b=2)") - x$a - - # Dictionary - strDict <- juliaEval('Dict("hi" => 1, "hello" => 2)') - strDict - strDict$hi - strDict$hi <- 0 - strDict[["hi"]] <- 2 - strDict["howdy", "greetings"] <- c(2, 3) - strDict["hi", "howdy"] +\dontrun{#' +# (Mutable) struct +juliaEval("mutable struct MyStruct + x::Int + end") + +MyStruct <- juliaFun("MyStruct") +s <- MyStruct(1L) +s$x +s$x <- 2 +s[["x"]] + +# Array +x <- juliaCall("map", MyStruct, c(1L, 2L, 3L)) +x +length(x) +x[[1]] +x[[1]]$x +x[[1]] <- MyStruct(2L) +x[2:3] +x[2:3] <- MyStruct(2L) +x + +# Tuple +x <- juliaEval("(1, 2, 3)") +x[[1]] +x[1:2] +length(x) + +# NamedTuple +x <- juliaEval("(a=1, b=2)") +x$a + +# Dictionary +strDict <- juliaEval('Dict("hi" => 1, "hello" => 2)') +strDict +strDict$hi +strDict$hi <- 0 +strDict[["hi"]] <- 2 +strDict["howdy", "greetings"] <- c(2, 3) +strDict["hi", "howdy"] } - -\dontshow{ -if (exists("strDict")) { - rm(x, s, strDict) -} -stopJulia() -} } diff --git a/tests/testthat/test_examples_notrun.R b/tests/testthat/test_examples_notrun.R new file mode 100644 index 0000000..f77513f --- /dev/null +++ b/tests/testthat/test_examples_notrun.R @@ -0,0 +1,44 @@ +test_that("Example for AccessMutate.JuliaProxy works", { + # (Mutable) struct + juliaEval("mutable struct MyStruct + x::Int + end") + + MyStruct <- juliaFun("MyStruct") + s <- MyStruct(1L) + s$x + s$x <- 2 + expect_equal(s[["x"]], 2) + + + # Array + x <- juliaCall("map", MyStruct, c(1L, 2L, 3L)) + x + expect_equal(length(x), 3) + x[[1]] + x[[1]]$x + x[[1]] <- MyStruct(2L) + x[2:3] + x[2:3] <- MyStruct(2L) + expect_equal(juliaLet("[myx.x for myx in x]", x = x), c(2,2,2)) + + # Tuple + x <- juliaEval("(1, 2, 3)") + expect_equal(x[[1]], 1) + expect_s3_class(x[1:2], "JuliaProxy") + expect_equal(length(x), 3) + + # NamedTuple + x <- juliaEval("(a=1, b=2)") + expect_equal(x$a, 1) + + # Dictionary + strDict <- juliaEval('Dict("hi" => 1, "hello" => 2)') + strDict + expect_equal(strDict$hi, 1) + strDict$hi <- 0 + strDict[["hi"]] <- 2 + strDict["howdy", "greetings"] <- c(2, 3) + expect_equal(strDict["hi", "howdy"], list(2,2)) + +})