SachaEpskamp / semPlot

Path diagrams and visual analysis of various SEM packages' output
GNU General Public License v2.0
61 stars 20 forks source link

semPaths() prints plot even when plot is assigned to an object #37

Closed fschaffner closed 2 years ago

fschaffner commented 2 years ago

Hi @SachaEpskamp, thanks for creating and maintaining this package!

I noticed that semPaths() prints the figure to the viewer even when the plot is assigned to an object, which is generally not how we would expect R functions to work. When a function is called without assigning to an object, we expect the result to be printed in the console, e.g., lm(wt ~ cyl, data = mtcars). But if we assign it to an object we don't expect the result to be printed in the console, e.g., model <- lm(wt ~ cyl, data = mtcars). This is especially problematic when using Rmarkdown, because when I assign the plot to an object, the figure still appears in the knitted HTML document, even though it shouldn't. Can this be changed?

(The reason I am asking is that I assign the figure to an object that I then post-process using other packages like semptools, and I don't want the figure to appear multiple times in my knitted Rmarkdown document.)

Rscript example: Note that the plot shouldn't be printed because I am assigning it to an object!

library("lavaan")
#> This is lavaan 0.6-10
library("semPlot")

# A silly dataset:
X <- rnorm(100)
Y <- rnorm(100)
Z <- rnorm(1) * X + rnorm(1) * Y + rnorm(1) * X * Y
DF <- data.frame(X, Y, Z)

# Regression including interaction:
res <- lm(Z ~ X * Y, data = DF)

# Path diagram:
plot <- semPaths(res) # => The figure shouldn't be printed

Created on 2022-03-04 by the reprex package (v2.0.1)

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.1.1 (2021-08-10) #> os macOS Big Sur 10.16 #> system x86_64, darwin17.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/London #> date 2022-03-04 #> pandoc 2.14.0.3 @ /Applications/RStudio.app/Contents/MacOS/pandoc/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> ! package * version date (UTC) lib source #> abind 1.4-5 2016-07-21 [2] CRAN (R 4.1.0) #> arm 1.12-2 2021-10-15 [2] CRAN (R 4.1.0) #> P assertthat 0.2.1 2019-03-21 [?] CRAN (R 4.1.0) #> P backports 1.4.1 2021-12-13 [?] CRAN (R 4.1.0) #> P base64enc 0.1-3 2015-07-28 [?] CRAN (R 4.1.0) #> P boot 1.3-28 2021-05-03 [?] CRAN (R 4.1.0) #> carData 3.0-5 2022-01-06 [2] CRAN (R 4.1.2) #> P checkmate 2.0.0 2020-02-06 [?] CRAN (R 4.1.0) #> cli 3.2.0 2022-02-14 [1] CRAN (R 4.1.2) #> P cluster 2.1.2 2021-04-17 [?] CRAN (R 4.1.1) #> coda 0.19-4 2020-09-30 [2] CRAN (R 4.1.0) #> colorspace 2.0-3 2022-02-21 [1] CRAN (R 4.1.2) #> P corpcor 1.6.10 2021-09-16 [?] CRAN (R 4.1.0) #> crayon 1.5.0 2022-02-14 [1] CRAN (R 4.1.2) #> P curl 4.3.2 2021-06-23 [?] CRAN (R 4.1.0) #> P data.table 1.14.2 2021-09-27 [?] CRAN (R 4.1.0) #> P DBI 1.1.2 2021-12-20 [?] CRAN (R 4.1.0) #> P digest 0.6.29 2021-12-01 [?] CRAN (R 4.1.0) #> P dplyr 1.0.8 2022-02-08 [?] CRAN (R 4.1.2) #> P ellipsis 0.3.2 2021-04-29 [?] CRAN (R 4.1.0) #> evaluate 0.15 2022-02-18 [1] CRAN (R 4.1.2) #> P fansi 1.0.2 2022-01-14 [?] CRAN (R 4.1.2) #> P fastmap 1.1.0 2021-01-25 [?] CRAN (R 4.1.0) #> P fdrtool 1.2.17 2021-11-13 [?] CRAN (R 4.1.0) #> P foreign 0.8-82 2022-01-13 [?] CRAN (R 4.1.2) #> P Formula 1.2-4 2020-10-16 [?] CRAN (R 4.1.0) #> P fs 1.5.2 2021-12-08 [?] CRAN (R 4.1.0) #> P generics 0.1.2 2022-01-31 [?] CRAN (R 4.1.2) #> P ggplot2 3.3.5 2021-06-25 [?] CRAN (R 4.1.0) #> P glasso 1.11 2019-10-01 [?] CRAN (R 4.1.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.1.2) #> P gridExtra 2.3 2017-09-09 [?] CRAN (R 4.1.0) #> P gtable 0.3.0 2019-03-25 [?] CRAN (R 4.1.0) #> P gtools 3.9.2 2021-06-06 [?] CRAN (R 4.1.0) #> P highr 0.9 2021-04-16 [?] CRAN (R 4.1.0) #> P Hmisc 4.6-0 2021-10-07 [?] CRAN (R 4.1.0) #> P htmlTable 2.4.0 2022-01-04 [?] CRAN (R 4.1.2) #> P htmltools 0.5.2 2021-08-25 [?] CRAN (R 4.1.0) #> P htmlwidgets 1.5.4 2021-09-08 [?] CRAN (R 4.1.0) #> P httr 1.4.2 2020-07-20 [?] CRAN (R 4.1.0) #> igraph 1.2.11 2022-01-04 [2] CRAN (R 4.1.2) #> P jpeg 0.1-9 2021-07-24 [?] CRAN (R 4.1.0) #> P knitr 1.37 2021-12-16 [?] CRAN (R 4.1.0) #> P kutils 1.70 2020-04-29 [?] CRAN (R 4.1.0) #> P lattice 0.20-45 2021-09-22 [?] CRAN (R 4.1.0) #> P latticeExtra 0.6-29 2019-12-19 [?] CRAN (R 4.1.0) #> P lavaan * 0.6-10 2022-01-25 [?] CRAN (R 4.1.2) #> P lifecycle 1.0.1 2021-09-24 [?] CRAN (R 4.1.0) #> P lisrelToR 0.1.4 2013-05-08 [?] CRAN (R 4.1.0) #> P lme4 1.1-28 2022-02-05 [?] CRAN (R 4.1.2) #> P magrittr 2.0.2 2022-01-26 [?] CRAN (R 4.1.2) #> P MASS 7.3-55 2022-01-13 [?] CRAN (R 4.1.2) #> P Matrix 1.4-0 2021-12-08 [?] CRAN (R 4.1.0) #> P mi 1.0 2015-04-16 [?] CRAN (R 4.1.0) #> P mime 0.12 2021-09-28 [?] CRAN (R 4.1.0) #> P minqa 1.2.4 2014-10-09 [?] CRAN (R 4.1.0) #> mnormt 2.0.2 2020-09-01 [2] CRAN (R 4.1.0) #> P munsell 0.5.0 2018-06-12 [?] CRAN (R 4.1.0) #> P nlme 3.1-155 2022-01-13 [?] CRAN (R 4.1.2) #> P nloptr 2.0.0 2022-01-26 [?] CRAN (R 4.1.2) #> P nnet 7.3-17 2022-01-13 [?] CRAN (R 4.1.2) #> P OpenMx 2.20.3 2022-02-01 [?] CRAN (R 4.1.2) #> openxlsx 4.2.5 2021-12-14 [2] CRAN (R 4.1.0) #> P pbapply 1.5-0 2021-09-16 [?] CRAN (R 4.1.0) #> P pbivnorm 0.6.0 2015-01-23 [?] CRAN (R 4.1.0) #> P pillar 1.7.0 2022-02-01 [?] CRAN (R 4.1.2) #> P pkgconfig 2.0.3 2019-09-22 [?] CRAN (R 4.1.0) #> P plyr 1.8.6 2020-03-03 [?] CRAN (R 4.1.0) #> P png 0.1-7 2013-12-03 [?] CRAN (R 4.1.0) #> psych 2.1.9 2021-09-22 [2] CRAN (R 4.1.0) #> P purrr 0.3.4 2020-04-17 [?] CRAN (R 4.1.0) #> P qgraph 1.9.1 2022-02-19 [?] CRAN (R 4.1.2) #> R.cache 0.15.0 2021-04-30 [2] CRAN (R 4.1.0) #> R.methodsS3 1.8.1 2020-08-26 [2] CRAN (R 4.1.0) #> R.oo 1.24.0 2020-08-26 [2] CRAN (R 4.1.0) #> R.utils 2.11.0 2021-09-26 [2] CRAN (R 4.1.0) #> P R6 2.5.1 2021-08-19 [?] CRAN (R 4.1.0) #> P RColorBrewer 1.1-2 2014-12-07 [?] CRAN (R 4.1.0) #> P Rcpp 1.0.8 2022-01-13 [?] CRAN (R 4.1.2) #> RcppParallel 5.1.5 2022-01-05 [2] CRAN (R 4.1.2) #> P reprex 2.0.1 2021-08-05 [?] CRAN (R 4.1.0) #> reshape2 1.4.4 2020-04-09 [2] CRAN (R 4.1.0) #> P rlang 1.0.1 2022-02-03 [?] CRAN (R 4.1.2) #> P rmarkdown 2.11 2021-09-14 [?] CRAN (R 4.1.0) #> P rockchalk 1.8.151 2022-02-14 [?] CRAN (R 4.1.2) #> P rpart 4.1.16 2022-01-24 [?] CRAN (R 4.1.2) #> P rstudioapi 0.13 2020-11-12 [?] CRAN (R 4.1.0) #> P scales 1.1.1 2020-05-11 [?] CRAN (R 4.1.0) #> P sem 3.1-13 2021-10-05 [?] CRAN (R 4.1.0) #> P semPlot * 1.1.4 2022-02-21 [?] CRAN (R 4.1.2) #> sessioninfo 1.2.2 2021-12-06 [2] CRAN (R 4.1.0) #> P stringi 1.7.6 2021-11-29 [?] CRAN (R 4.1.0) #> P stringr 1.4.0 2019-02-10 [?] CRAN (R 4.1.0) #> styler 1.6.2 2021-09-23 [2] CRAN (R 4.1.0) #> P survival 3.2-13 2021-08-24 [?] CRAN (R 4.1.0) #> P tibble 3.1.6 2021-11-07 [?] CRAN (R 4.1.0) #> tidyselect 1.1.2 2022-02-21 [1] CRAN (R 4.1.2) #> tmvnsim 1.0-2 2016-12-15 [2] CRAN (R 4.1.0) #> P utf8 1.2.2 2021-07-24 [?] CRAN (R 4.1.0) #> P vctrs 0.3.8 2021-04-29 [?] CRAN (R 4.1.0) #> P withr 2.4.3 2021-11-30 [?] CRAN (R 4.1.1) #> xfun 0.30 2022-03-02 [1] CRAN (R 4.1.1) #> XML 3.99-0.9 2022-02-24 [1] CRAN (R 4.1.2) #> P xml2 1.3.3 2021-11-30 [?] CRAN (R 4.1.0) #> P xtable 1.8-4 2019-04-21 [?] CRAN (R 4.1.0) #> yaml 2.3.5 2022-02-21 [1] CRAN (R 4.1.2) #> zip 2.2.0 2021-05-31 [2] CRAN (R 4.1.0) #> #> [1] /Users/florianschaffner/Documents/Studies Oxford/Confirmation of Status/Survey data analysis/BES wave 21/RA Work for Geoff/renv/library/R-4.1/x86_64-apple-darwin17.0 #> [2] /Library/Frameworks/R.framework/Versions/4.1/Resources/library #> #> P ── Loaded and on-disk path mismatch. #> #> ────────────────────────────────────────────────────────────────────────────── ```

Rmarkdown example


---
title: "Reproducible Example"
output: html_document
---

```{r}
library("lavaan")
library("semPlot")

# A silly dataset:
X <- rnorm(100)
Y <- rnorm(100)
Z <- rnorm(1) * X + rnorm(1) * Y + rnorm(1) * X * Y
DF <- data.frame(X, Y, Z)

# Regression including interaction:
res <- lm(Z ~ X * Y, data = DF) 

# Path diagram:
plot <- semPaths(res) # => The figure shouldn't be printed
sfcheung commented 2 years ago

I am not a developer of semPlot (I love using it and use it a lot in my teaching; thanks, @SachaEpskamp, for developing this package). I just want to share how I solve this problem. There may be a better solution.

You can set DoNotPlot = TRUE when calling semPlot::semPaths(). This will suppress the plot in Rmarkdown.

This is an example

---
title       : "Demo: DoNotPlot"
---

# Diagram not plotted

```{r}
library(semPlot)
X <- rnorm(100)
Y <- rnorm(100)
Z <- rnorm(1)*X + rnorm(1)*Y + rnorm(1)*X*Y
DF <- data.frame(X,Y,Z)
res <- lm(Z ~ X*Y, data = DF)
p <- semPaths(res,"est","hide", intAtSide=TRUE,
              DoNotPlot = TRUE)

Diagram plotted

plot(p)
SachaEpskamp commented 2 years ago

Hi both,

Yes indeed the argument DoNotPlot is designed for this, thank you Shu Fai!

I think it is pretty common for base R graphics to create a plot even when the result is stored in an object. For example, all these commands to that:

object <- plot(rnorm(100),rnorm(100))
object <- hist(rnorm(100))
object <- pairs(matrix(rnorm(400),100,4))

Best, sacha

fschaffner commented 2 years ago

Thanks for the explanations @sfcheung and @SachaEpskamp!