Skip to content

Commit 30dee31

Browse files
Improve vignette compatibility for missing OpenMx
Updated vignettes to gracefully handle cases where OpenMx or EasyMx are not installed, preventing errors on older R versions. Added conditional checks and informative messages in the vignette code. Updated cran-comments.md to document this enhancement.
1 parent c6d566d commit 30dee31

3 files changed

Lines changed: 163 additions & 114 deletions

File tree

cran-comments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# Description
33

4-
This update includes minor enhancements and bug fixes related to how string ids are handled in various functions.
4+
This update includes minor enhancements and bug fixes related to how string ids are handled in various functions. It also now allows certain vignettes to not throw an error if openmx is not installed for older R versions.
55

66
# Test Environments
77

vignettes/v1_modelingvariancecomponents.Rmd

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,18 @@ This vignette provides a detailed guide to specific functions within the `BGmisc
2424

2525
```{r setup, include=FALSE}
2626
library(BGmisc)
27-
require(EasyMx)
28-
require(OpenMx)
27+
if (!requireNamespace("EasyMx", quietly = TRUE)) {
28+
message("Please install OpenMx to run the model fitting examples.")
29+
} else {
30+
require(EasyMx)
31+
}
32+
if (!requireNamespace("OpenMx", quietly = TRUE)) {
33+
message("Please install OpenMx to run the model fitting examples.")
34+
} else {
35+
require(OpenMx)
36+
}
37+
38+
2939
```
3040

3141
Ensure that the `BGmisc` package is installed and loaded.
@@ -37,7 +47,7 @@ Ensure that the following dependencies are installed before proceeding as they p
3747
- `OpenMx`
3848

3949

40-
```{r}
50+
```r
4151
library(BGmisc)
4252
library(EasyMx)
4353
library(OpenMx)
@@ -88,11 +98,54 @@ identifyComponentModel(
8898

8999
As you can see the model is identified, now that we've added another group. Let us confirm by fitting a model. First we prepare the data.
90100

91-
```{r}
101+
```{r, include=FALSE}
102+
if (!requireNamespace("OpenMx", quietly = TRUE)) {
103+
# if OpenMx isn't available
104+
n_subjects <- 500
105+
106+
df_summary_data <- data.frame(
107+
age_mean = 34.45,
108+
age_sd = 10.12345,
109+
ht1_mean = 1.662,
110+
ht1_sd = 0.0896277,
111+
ht2_mean = 1.694,
112+
ht2_sd = 0.09903498
113+
)
114+
set.seed(12345)
115+
twinData <- data.frame(
116+
ht1 = rnorm(n_subjects, mean = df_summary_data$ht1_mean, sd = df_summary_data$ht1_sd),
117+
zyg = rep(c(1, 3), each = n_subjects / 2)
118+
)
119+
twinData$ht2 <- twinData$ht1 * ifelse(twinData$zyg == 1, 1, 0.5) +
120+
rnorm(n_subjects, mean = 0, sd = df_summary_data$ht2_sd) + .1*rnorm(n_subjects, mean = df_summary_data$ht2_mean, sd = df_summary_data$ht2_sd)
121+
twinData$ht2[twinData$zyg == 3] <- twinData$ht2[twinData$zyg == 3] + .5*rnorm(sum(twinData$zyg == 3), mean = df_summary_data$ht2_mean, sd = df_summary_data$ht2_sd)
122+
123+
} else {
124+
data(twinData, package = "OpenMx")
125+
92126
require(dplyr)
93127
# require(purrr)
94128
129+
selVars <- c("ht1", "ht2")
130+
131+
mzdzData <- subset(
132+
twinData, zyg %in% c(1, 3),
133+
c(selVars, "zyg")
134+
)
135+
136+
mzdzData$RCoef <- c(1, NA, .5)[mzdzData$zyg]
137+
138+
139+
mzData <- mzdzData %>% filter(zyg == 1)
140+
}
141+
```
142+
143+
144+
```r
145+
require(dplyr)
146+
95147
data(twinData, package = "OpenMx")
148+
96149
selVars <- c("ht1", "ht2")
97150

98151
mzdzData <- subset(
@@ -109,6 +162,9 @@ mzData <- mzdzData %>% filter(zyg == 1)
109162
Let us fit the data with MZ twins by themselves.
110163

111164
```{r}
165+
if (!requireNamespace("EasyMx", quietly = TRUE)) {
166+
print("Please install EasyMx to run the model fitting examples.")
167+
} else {
112168
run1 <- emxTwinModel(
113169
model = "Cholesky",
114170
relatedness = "RCoef",
@@ -118,11 +174,15 @@ run1 <- emxTwinModel(
118174
)
119175
120176
summary(run1)
177+
}
121178
```
122179

123180
As you can see the model was unsuccessful because it was not identified. But when we add another group, so that the model is identified, the model now fits.
124181

125182
```{r}
183+
if (!requireNamespace("EasyMx", quietly = TRUE)) {
184+
print("Please install EasyMx to run the model fitting examples.")
185+
} else {
126186
run2 <- emxTwinModel(
127187
model = "Cholesky",
128188
relatedness = "RCoef",
@@ -132,4 +192,5 @@ run2 <- emxTwinModel(
132192
)
133193
134194
summary(run2)
195+
}
135196
```

0 commit comments

Comments
 (0)