You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/Fixed_obj_fun.Rmd
+23-4Lines changed: 23 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -82,11 +82,30 @@ The same reasoning above applies to functions defined in the R work-space as wel
82
82
stopCluster(cl)
83
83
```
84
84
85
-
# Exporting objects example in `SimDesign`
85
+
# Exporting functions for parallel computing
86
86
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
88
88
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.
90
109
91
110
```{r}
92
111
library(SimDesign)
@@ -138,4 +157,4 @@ res <- runSimulation(Design, replications, verbose=FALSE, fixed_objects=fixed_ob
138
157
parallel = TRUE)
139
158
```
140
159
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!
Copy file name to clipboardExpand all lines: vignettes/MultipleAnalyses.Rmd
+5-2Lines changed: 5 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -62,7 +62,10 @@ Finally, note that all the rules about objects and object naming from the typica
62
62
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.
63
63
64
64
```{r}
65
+
# Note that all attached packages are automatically exported in SimDesign
65
66
library(SimDesign)
67
+
library(mirt)
68
+
library(lavaan)
66
69
# SimFunctions(nAnalyses = 2)
67
70
68
71
sample_sizes <- c(250, 500, 1000)
@@ -150,7 +153,7 @@ res <- runSimulation(Design, replications=100, verbose=FALSE, parallel=TRUE,
@@ -626,7 +629,7 @@ res <- runSimulation(Design, replications=100, verbose=FALSE, parallel=TRUE,
626
629
generate=Generate,
627
630
analyse=list(Analyse.FIML, Analyse.DWLS),
628
631
summarise=Summarise, filename = 'mirt_lavaan',
629
-
packages=c('mirt', 'lavaan'), fixed_objects=pars)
632
+
fixed_objects=pars)
630
633
```
631
634
632
635
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