diff --git a/R/rBinomDS.R b/R/rBinomDS.R index 22bcbbf3..9ec42361 100644 --- a/R/rBinomDS.R +++ b/R/rBinomDS.R @@ -24,6 +24,7 @@ #' also returns a vector reporting the length of the pseudorandom vector #' created in each source. #' @author Paul Burton for DataSHIELD Development Team +#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands #' @export rBinomDS<-function (n, size = 1, prob = 0.5){ @@ -34,13 +35,11 @@ rBinomDS<-function (n, size = 1, prob = 0.5){ #first convert their names into the corresponding active vectors if(is.character(size)){ - command.text<-size - size<-eval(parse(text=command.text), envir = parent.frame()) + size<-.loadServersideObject(size) } if(is.character(prob)){ - command.text<-prob - prob<-eval(parse(text=command.text), envir = parent.frame()) + prob<-.loadServersideObject(prob) } stats::rbinom(n, size=size, prob=prob) diff --git a/R/rNormDS.R b/R/rNormDS.R index f50a424e..d201143a 100644 --- a/R/rNormDS.R +++ b/R/rNormDS.R @@ -29,6 +29,7 @@ #' also returns a vector reporting the length of the pseudorandom vector #' created in each source. #' @author Paul Burton for DataSHIELD Development Team +#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands #' @export rNormDS<-function (n, mean = 0, sd = 1, force.output.to.k.decimal.places=9){ @@ -39,13 +40,11 @@ rNormDS<-function (n, mean = 0, sd = 1, force.output.to.k.decimal.places=9){ #first convert their names into the corresponding active vectors if(is.character(mean)){ - command.text<-mean - mean<-eval(parse(text=command.text), envir = parent.frame()) + mean<-.loadServersideObject(mean) } if(is.character(sd)){ - command.text<-sd - sd<-eval(parse(text=command.text), envir = parent.frame()) + sd<-.loadServersideObject(sd) } random.number.vector<-stats::rnorm(n, mean=mean, sd=sd) diff --git a/R/rPoisDS.R b/R/rPoisDS.R index 3274078c..5bee2148 100644 --- a/R/rPoisDS.R +++ b/R/rPoisDS.R @@ -20,6 +20,7 @@ #' also returns a vector reporting the length of the pseudorandom vector #' created in each source. #' @author Paul Burton for DataSHIELD Development Team +#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands #' @export rPoisDS<-function (n, lambda = 1){ @@ -30,8 +31,7 @@ rPoisDS<-function (n, lambda = 1){ #first convert its name into the corresponding active vectors if(is.character(lambda)){ - command.text<-lambda - lambda<-eval(parse(text=command.text), envir = parent.frame()) + lambda<-.loadServersideObject(lambda) } stats::rpois(n, lambda=lambda) diff --git a/R/rUnifDS.R b/R/rUnifDS.R index 9f4463c6..d20d189f 100644 --- a/R/rUnifDS.R +++ b/R/rUnifDS.R @@ -29,6 +29,7 @@ #' also returns a vector reporting the length of the pseudorandom vector #' created in each source. #' @author Paul Burton for DataSHIELD Development Team +#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands #' @export rUnifDS<-function (n, min = 0, max = 1, force.output.to.k.decimal.places=9){ @@ -39,13 +40,11 @@ rUnifDS<-function (n, min = 0, max = 1, force.output.to.k.decimal.places=9){ #first convert their names into the corresponding active vectors if(is.character(min)){ - command.text<-min - min<-eval(parse(text=command.text), envir = parent.frame()) + min<-.loadServersideObject(min) } if(is.character(max)){ - command.text<-max - max<-eval(parse(text=command.text), envir = parent.frame()) + max<-.loadServersideObject(max) } random.number.vector<-stats::runif(n, min=min, max=max) diff --git a/R/sampleDS.R b/R/sampleDS.R index 1c5bd43f..213451dd 100644 --- a/R/sampleDS.R +++ b/R/sampleDS.R @@ -30,6 +30,7 @@ #' 'newobj.sample') which is written to the serverside. For further details see #' help for ds.sample and native R help for sample(). #' @author Paul Burton, for DataSHIELD Development Team, 15/4/2020 +#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands #' @export sampleDS <- function(x.transmit, size.transmit, replace.transmit=NULL, prob.transmit=NULL){ @@ -82,7 +83,7 @@ sampleDS <- function(x.transmit, size.transmit, replace.transmit=NULL, prob.tran #Activate and if they are character strings if(is.character(x.transmit)) { - x.active<-eval(parse(text=x.transmit), envir = parent.frame()) + x.active<-.loadServersideObject(x.transmit) if(is.data.frame(x.active)||is.matrix(x.active)) { @@ -152,7 +153,7 @@ sampleDS <- function(x.transmit, size.transmit, replace.transmit=NULL, prob.tran prob.active<-NULL if(is.character(prob.transmit)) { - prob.active<-eval(parse(text=prob.transmit), envir = parent.frame()) + prob.active<-.loadServersideObject(prob.transmit) } #Check size <= length(x.active) if replace.transmit==FALSE diff --git a/R/setSeedDS.R b/R/setSeedDS.R index 6515d51a..bead37b7 100644 --- a/R/setSeedDS.R +++ b/R/setSeedDS.R @@ -32,13 +32,18 @@ #' .Random.seed on each data source that is the true current state of the #' random seed in each source. #' @author Paul Burton for DataSHIELD Development Team +#' @author Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands #' @export setSeedDS<-function (seedtext=NULL, kind = NULL, normal.kind = NULL) { # Check Permissive Privacy Control Level. dsBase::checkPermissivePrivacyControlLevel(c('permissive', 'avocado')) - seed<-eval(parse(text=seedtext), envir = parent.frame()) + if(is.null(seedtext) || seedtext == "NULL"){ + seed <- NULL + } else { + seed <- as.integer(seedtext) + } set.seed(seed,kind,normal.kind) return(list(seed.as.set=.Random.seed)) } diff --git a/man/rBinomDS.Rd b/man/rBinomDS.Rd index 9a3fc25e..b8d16755 100644 --- a/man/rBinomDS.Rd +++ b/man/rBinomDS.Rd @@ -42,4 +42,6 @@ the function rbinom() in native R and its arguments are the same. } \author{ Paul Burton for DataSHIELD Development Team + +Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands } diff --git a/man/rNormDS.Rd b/man/rNormDS.Rd index ef7ea58d..f4dd1f4f 100644 --- a/man/rNormDS.Rd +++ b/man/rNormDS.Rd @@ -48,4 +48,6 @@ the function rnorm() in native R and its arguments are the same. } \author{ Paul Burton for DataSHIELD Development Team + +Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands } diff --git a/man/rPoisDS.Rd b/man/rPoisDS.Rd index 73fcdee0..9d3d2dbe 100644 --- a/man/rPoisDS.Rd +++ b/man/rPoisDS.Rd @@ -37,4 +37,6 @@ and its arguments are the same. } \author{ Paul Burton for DataSHIELD Development Team + +Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands } diff --git a/man/rUnifDS.Rd b/man/rUnifDS.Rd index c9b448ea..f3ad01c8 100644 --- a/man/rUnifDS.Rd +++ b/man/rUnifDS.Rd @@ -48,4 +48,6 @@ the function runif() in native R and its arguments are the same. } \author{ Paul Burton for DataSHIELD Development Team + +Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands } diff --git a/man/sampleDS.Rd b/man/sampleDS.Rd index c3d2ead7..aae770f8 100644 --- a/man/sampleDS.Rd +++ b/man/sampleDS.Rd @@ -54,4 +54,6 @@ help for ds.sample and native R help for sample(). } \author{ Paul Burton, for DataSHIELD Development Team, 15/4/2020 + +Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands } diff --git a/man/setSeedDS.Rd b/man/setSeedDS.Rd index 9c8c2740..6531b5f2 100644 --- a/man/setSeedDS.Rd +++ b/man/setSeedDS.Rd @@ -50,4 +50,6 @@ theoretical work with random number generators it is likely that the } \author{ Paul Burton for DataSHIELD Development Team + +Tim Cadman, Genomics Coordination Centre, UMCG, Netherlands } diff --git a/tests/testthat/test-smk-rBinomDS.R b/tests/testthat/test-smk-rBinomDS.R index 9cff07d0..a25d1a5b 100644 --- a/tests/testthat/test-smk-rBinomDS.R +++ b/tests/testthat/test-smk-rBinomDS.R @@ -15,6 +15,8 @@ # context("rBinomDS::smk::setup") +set.standard.disclosure.settings() + # # Tests # @@ -58,6 +60,14 @@ test_that("simple rBinomDS, direct", { expect_true(res[8] >= 0) }) +test_that("rBinomDS fails when size references nonexistent object", { + expect_error(rBinomDS(8, "nonexistent_obj", 0.5), "does not exist") +}) + +test_that("rBinomDS fails when prob references nonexistent object", { + expect_error(rBinomDS(8, 1, "nonexistent_obj"), "does not exist") +}) + # # Done # diff --git a/tests/testthat/test-smk-rNormDS.R b/tests/testthat/test-smk-rNormDS.R index 484ef851..0182aab6 100644 --- a/tests/testthat/test-smk-rNormDS.R +++ b/tests/testthat/test-smk-rNormDS.R @@ -15,6 +15,8 @@ # context("rNormDS::smk::setup") +set.standard.disclosure.settings() + # # Tests # @@ -58,6 +60,14 @@ test_that("simple rNormDS, direct", { expect_true(res[8] >= 0) }) +test_that("rNormDS fails when mean references nonexistent object", { + expect_error(rNormDS(8, "nonexistent_obj", 1, 9), "does not exist") +}) + +test_that("rNormDS fails when sd references nonexistent object", { + expect_error(rNormDS(8, 0, "nonexistent_obj", 9), "does not exist") +}) + # # Done # diff --git a/tests/testthat/test-smk-rPoisDS.R b/tests/testthat/test-smk-rPoisDS.R index c2cf55bf..ee9de700 100644 --- a/tests/testthat/test-smk-rPoisDS.R +++ b/tests/testthat/test-smk-rPoisDS.R @@ -15,6 +15,8 @@ # context("rPoisDS::smk::setup") +set.standard.disclosure.settings() + # # Tests # @@ -56,6 +58,10 @@ test_that("simple rPoisDS, direct", { expect_true(res[8] >= 0) }) +test_that("rPoisDS fails when lambda references nonexistent object", { + expect_error(rPoisDS(8, "nonexistent_obj"), "does not exist") +}) + # # Done # diff --git a/tests/testthat/test-smk-rUnifDS.R b/tests/testthat/test-smk-rUnifDS.R index 87c208c4..7507db16 100644 --- a/tests/testthat/test-smk-rUnifDS.R +++ b/tests/testthat/test-smk-rUnifDS.R @@ -15,6 +15,8 @@ # context("rUnifDS::smk::setup") +set.standard.disclosure.settings() + # # Tests # @@ -58,6 +60,14 @@ test_that("simple rUnifDS, direct", { expect_true(res[8] >= 0) }) +test_that("rUnifDS fails when min references nonexistent object", { + expect_error(rUnifDS(8, "nonexistent_obj", 1, 9), "does not exist") +}) + +test_that("rUnifDS fails when max references nonexistent object", { + expect_error(rUnifDS(8, 0, "nonexistent_obj", 9), "does not exist") +}) + # # Done # diff --git a/tests/testthat/test-smk-sampleDS.R b/tests/testthat/test-smk-sampleDS.R index e3927518..6fb5cdd9 100644 --- a/tests/testthat/test-smk-sampleDS.R +++ b/tests/testthat/test-smk-sampleDS.R @@ -46,6 +46,16 @@ test_that("simple sampleDS", { expect_length(res$sampling.order, 16) }) +test_that("sampleDS fails when x references nonexistent object", { + expect_error(sampleDS("nonexistent_obj", 5, FALSE, NULL), "does not exist") +}) + +test_that("sampleDS fails when prob references nonexistent object", { + x <- c(1:32) + + expect_error(sampleDS("x", 5, FALSE, "nonexistent_obj"), "does not exist") +}) + # # Done # diff --git a/tests/testthat/test-smk-setSeedDS.R b/tests/testthat/test-smk-setSeedDS.R index 0cdbf2e9..0e976bd7 100644 --- a/tests/testthat/test-smk-setSeedDS.R +++ b/tests/testthat/test-smk-setSeedDS.R @@ -15,6 +15,8 @@ # context("setSeedDS::smk::setup") +set.standard.disclosure.settings() + # # Tests # @@ -32,6 +34,14 @@ test_that("simple setSeedDS", { expect_length(res$seed.as.set, 626) }) +test_that("setSeedDS works with numeric string", { + res <- setSeedDS("42", NULL, NULL) + + expect_equal(class(res), "list") + expect_length(res, 1) + expect_length(res$seed.as.set, 626) +}) + # # Done #