Skip to content

Commit 36980ad

Browse files
committed
automatically export attached packages
1 parent 177cdaa commit 36980ad

File tree

8 files changed

+50
-23
lines changed

8 files changed

+50
-23
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: SimDesign
22
Title: Structure for Organizing Monte Carlo Simulation Designs
3-
Version: 2.19.3
3+
Version: 2.19.4
44
Authors@R: c(person("Phil", "Chalmers", email = "[email protected]", role = c("aut", "cre"),
55
comment = c(ORCID="0000-0001-5332-2810")),
66
person("Matthew", "Sigal", role = c("ctb")),

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,5 @@ importFrom(utils,methods)
104104
importFrom(utils,object.size)
105105
importFrom(utils,packageVersion)
106106
importFrom(utils,recover)
107+
importFrom(utils,sessionInfo)
107108
importFrom(utils,tail)

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Changes in SimDesign 2.20
44

5+
- Information provided to `runSimulation(..., packages)` now automatically adds `sessionInfo()$otherPkgs`
6+
to the list. This detects and adds any explicit `library()` attachments declared before
7+
the simulation execution to be exported
8+
59
- Add `expandReplications()` function to more naturally match the `expandDesign()`
610
structure
711

R/SimDesign.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#' @importFrom progressr progressor
3636
#' @importFrom beepr beep
3737
# @importFrom robustbase glmrob
38-
#' @importFrom utils recover packageVersion head tail capture.output object.size
38+
#' @importFrom utils recover packageVersion head tail capture.output object.size sessionInfo
3939
#' @keywords package
4040
#' @references
4141
#'

R/runSimulation.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,13 @@
237237
#'
238238
#' @param packages a character vector of external packages to be used during the simulation (e.g.,
239239
#' \code{c('MASS', 'extraDistr', 'simsem')} ). Use this input when running code in
240-
#' parallel to use non-standard functions from additional packages,
241-
#' otherwise the functions must be made available by using explicit
242-
#' \code{\link{library}} or \code{\link{require}} calls within the provided simulation functions.
243-
#' Alternatively, functions can be called explicitly without attaching the package
244-
#' with the \code{::} operator
245-
#' (e.g., \code{extraDistr::rgumbel()}). Finally, to attach all previously loaded
246-
#' packages use \code{packages = .packages()}
240+
#' parallel to use non-standard functions from additional packages. Note that any previously attached
241+
#' packages explicitly loaded via \code{\link{library}} or \code{\link{require}}
242+
#' will be automatically added to this list, provided that they are visible
243+
#' in the \code{otherPkgs} element
244+
#' from \code{\link[utils]{sessionInfo}}. Alternatively, functions can be called
245+
#' explicitly without attaching the package with the \code{::} operator
246+
#' (e.g., \code{extraDistr::rgumbel()})
247247
#'
248248
#' @param beep logical; call the \code{beepr} package when the simulation is completed?
249249
#'
@@ -1260,7 +1260,7 @@ runSimulation <- function(design, replications, generate, analyse, summarise,
12601260
parallel <- FALSE
12611261
verbose <- FALSE
12621262
}
1263-
packages <- c('SimDesign', packages)
1263+
packages <- unique(c(packages, 'SimDesign', names(utils::sessionInfo()$otherPkgs)))
12641264
char_functions <- deparse(substitute(Functions[[i]]))
12651265
if(any(grepl('browser\\(', char_functions))){
12661266
if(verbose && parallel)

man/runSimulation.Rd

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/Fixed_obj_fun.Rmd

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,30 @@ The same reasoning above applies to functions defined in the R work-space as wel
8282
stopCluster(cl)
8383
```
8484

85-
# Exporting objects example in `SimDesign`
85+
# Exporting functions for parallel computing
8686

87-
In order to make objects safely visible in `SimDesign` the strategy is very simple: wrap all desired objects into a named list (or other object), and pass this to the `fixed_objects` argument. From here, elements can be indexed using the `$` operator or `with()` function, or whatever other method may be convenient. Note, however, this is only required for defined *objects* not *functions* --- `SimDesign` automatically makes user-defined functions available across all nodes.
87+
## Exporting third-party libraries
8888

89-
As an aside, an alternative approach is simply to define/source the objects within the respective `SimDesign` functions; that way they will clearly be visible at run-time. The following `fixed_objects` approach is really only useful when the defined objects contain a large amount of code.
89+
For simulations that require third-party packages to work correctly (e.g., `rgumbel()` from the `extraDistr` package) there needs to be explicit instructions to indicate which namespace these functions originate from. For parallel computing applications there are three ways to indicate the namespace of a third-party function:
90+
91+
1) Explicitly attach the package prior to executing the simulation via `library()` or `require()`. This must be done prior to using `runSimulation()`
92+
2) Explicitly pass a character vector containing the packages to be attached to `runSimulation(..., packages)` (e.g., `runSimulation(..., packages = c('extraDistr', 'copula'))`)
93+
3) Point to the function using the `::` operator (e.g., `extraDistr::rgumbel()`)
94+
95+
For performance and masking related issues it is recommended to only export functions that are actually used in the simulation (e.g., attach `dplyr` and `tidy` explicitly rather than attaching the complete `tidyverse`).
96+
Note that 2) is technically not required if 1) or 3) is used.
97+
98+
As a reminder, attaching packages can result in function masking in the R session, and therefore in situations were ambiguity exists it is recommended to use the `::` operator explicitly even if these were attached. Doing so avoids makes it clear where the function originates regardless of the order with which the packages were attached to the session.
99+
100+
## Exporting user-defined functions
101+
102+
Exporting user-defined functions explicitly is not required in `SimDesign`. Any previously written function that has been defined in the R work space prior to executing `runSimulation()` will automatically be exported (again, be cognizant of any potential function masking). Other R objects, on the other hand, are *not* explicitly exported. See the next section for further details.
103+
104+
# Exporting objects in `SimDesign`
105+
106+
In order to make objects safely visible in `SimDesign` the strategy is very simple: wrap all desired objects into a named list (or other object), and pass this to the `fixed_objects` argument. From here, elements can be indexed using the `$` operator or `with()` function, or whatever other method may be convenient. Note, however, this is only required for defined *objects* not *functions*.
107+
108+
As an aside, an alternative approach is simply to define/source the objects within the respective `SimDesign` functions; that way they will clearly be visible at run-time. The following `fixed_objects` approach is really only useful when the defined objects contain a large amount of code or information.
90109

91110
```{r}
92111
library(SimDesign)
@@ -138,4 +157,4 @@ res <- runSimulation(Design, replications, verbose=FALSE, fixed_objects=fixed_ob
138157
parallel = TRUE)
139158
```
140159

141-
Again, remember that this is only required for R **objects**, NOT for user-defined functions!
160+
Again, remember that this is only required for R **objects**, NOT for user-defined or third-party functions!

vignettes/MultipleAnalyses.Rmd

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ Finally, note that all the rules about objects and object naming from the typica
6262
The following code is [adopted from the Wiki](http://philchalmers.github.io/SimDesign/html/03-Parameter_recovery_simulation.html), and so details about the simulation should be obtained from that source.
6363

6464
```{r}
65+
# Note that all attached packages are automatically exported in SimDesign
6566
library(SimDesign)
67+
library(mirt)
68+
library(lavaan)
6669
# SimFunctions(nAnalyses = 2)
6770
6871
sample_sizes <- c(250, 500, 1000)
@@ -150,7 +153,7 @@ res <- runSimulation(Design, replications=100, verbose=FALSE, parallel=TRUE,
150153
generate=Generate,
151154
analyse=list(FIML=Analyse.FIML, DWLS=Analyse.DWLS),
152155
summarise=Summarise, filename = 'mirt_lavaan',
153-
packages=c('mirt', 'lavaan'), fixed_objects=pars)
156+
fixed_objects=pars)
154157
```
155158

156159
```{r include=FALSE}
@@ -626,7 +629,7 @@ res <- runSimulation(Design, replications=100, verbose=FALSE, parallel=TRUE,
626629
generate=Generate,
627630
analyse=list(Analyse.FIML, Analyse.DWLS),
628631
summarise=Summarise, filename = 'mirt_lavaan',
629-
packages=c('mirt', 'lavaan'), fixed_objects=pars)
632+
fixed_objects=pars)
630633
```
631634

632635
then only one analysis function will be applied at a time in the simulation experiment. Note that in this case there is no need to append 'MML' or 'DWLS' to the `results` objects as this becomes redundant with the `method` column in the `Design` object, and so the `analyse` list input is specified as an unnamed list (cf. earlier when the input was named, which appended `MML.` and `DWLS.` to the `results` output in `Summarise()`).

0 commit comments

Comments
 (0)