I am trying to create a forest plot but forestplot is giving me the error below. It was working before but not sure why not working now. here is an example where i create data table and then use forestplot
and dplyr
to generate a forestplot:
library(forestplot)
library(dplyr)
##Example SDAI-total cohort
base_data <- tibble(mean = c(0.92, 0.86, 0.75, 0.77, 0.60),
lower = c(0.70, 0.35, 0.39, 0.44, 0.40),
upper = c(1.19, 2.14, 1.44, 1.34, 0.90),
study = c("STUDY1", "STUDY2", "STUDY3", "STUDY4", "STUDY5"),
N = c("160", "47", "167", "38", "146"),
HR = c('0.92 (0.70-1.19)', '0.86 (0.35-2.14)', '0.75 (0.39-1.4)', '0.77 (0.44-1.34)', '0.60 (0.40-0.90)'))
summary <- tibble(mean = 0.78,
lower = 0.65,
upper = 0.95,
HR = '0.78 (0.65-0.95)',
study = "Summary")
header <- tibble(study = c("Study"),
N = c("N"),
HR = c("HR (95%CI)"),
summary = TRUE)
empty_row <- tibble(mean = NA_real_)
output <- bind_rows(header,
base_data,
empty_row,
summary)
output$summary[output$study=='Summary'] <- TRUE
output %>%
forestplot(labeltext = c(study, N, HR),
is.summary = summary,
clip = c(0.35, 2.5),
xticks=c(0.4,0.7,1,1.5,2,2.5),
hrzl_lines = gpar(col = "#444444"),
#hrzl_lines=list('2' = gpar(col='#444444')),
xlog = TRUE,
col = fpColors(box = "black",line = "black",summary = "darkred"),
boxsize = 0.25,
title='title',
xlab = ('Hazard Ratio (log scale)'),
align = 'l',
txt_gp = fpTxtGp(label=gpar(fontsize=12),summary=gpar(fontsize=12),ticks=gpar(cex=1), xlab=gpar(cex=1)))
The error I am getting:
Error: Assertion failed. One of the following must apply:
* check_matrix(mean): Must be of type 'matrix', not
* 'tbl_df/tbl/data.frame'
* check_array(mean): Must be of type 'array', not
* 'tbl_df/tbl/data.frame'
* check_integer(dim(mean)[2]): Element 1 is not <= 3
Sessioninf()
R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
locale:
[1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8
attached base packages:
[1] grid stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] dplyr_0.8.3 forestplot_1.9 checkmate_2.0.0 magrittr_1.5
loaded via a namespace (and not attached):
[1] Rcpp_1.0.3 fansi_0.4.0 crayon_1.3.4 assertthat_0.2.1
[5] R6_2.4.1 lifecycle_0.2.0 backports_1.1.5 pillar_1.4.3
[9] cli_2.0.0 rlang_0.4.5 rstudioapi_0.11 vctrs_0.2.4
[13] ellipsis_0.3.0 tools_3.6.3 glue_1.3.1 purrr_0.3.3
[17] compiler_3.6.3 pkgconfig_2.0.3 tidyselect_0.2.5 tibble_3.0.1
How can we solve this in R?
CodePudding user response:
I installed R version 4.1.1 and installed the packages required and seems to work fine. It seems to be related to error in the previous R version.
CodePudding user response:
I am unable to reproduce your error (i.e. the code works as expected on my system):
library(forestplot)
#> Loading required package: grid
#> Loading required package: magrittr
#> Loading required package: checkmate
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
##Example SDAI-total cohort
base_data <- tibble(mean = c(0.92, 0.86, 0.75, 0.77, 0.60),
lower = c(0.70, 0.35, 0.39, 0.44, 0.40),
upper = c(1.19, 2.14, 1.44, 1.34, 0.90),
study = c("STUDY1", "STUDY2", "STUDY3", "STUDY4", "STUDY5"),
N = c("160", "47", "167", "38", "146"),
HR = c('0.92 (0.70-1.19)', '0.86 (0.35-2.14)', '0.75 (0.39-1.4)', '0.77 (0.44-1.34)', '0.60 (0.40-0.90)'))
summary <- tibble(mean = 0.78,
lower = 0.65,
upper = 0.95,
HR = '0.78 (0.65-0.95)',
study = "Summary")
header <- tibble(study = c("Study"),
N = c("N"),
HR = c("HR (95%CI)"),
summary = TRUE)
empty_row <- tibble(mean = NA_real_)
output <- bind_rows(header,
base_data,
empty_row,
summary)
output$summary[output$study=='Summary'] <- TRUE
output %>%
forestplot(labeltext = c(study, N, HR),
is.summary = summary,
clip = c(0.35, 2.5),
xticks=c(0.4,0.7,1,1.5,2,2.5),
hrzl_lines = gpar(col = "#444444"),
#hrzl_lines=list('2' = gpar(col='#444444')),
xlog = TRUE,
col = fpColors(box = "black",line = "black",summary = "darkred"),
boxsize = 0.25,
title='title',
xlab = ('Hazard Ratio (log scale)'),
align = 'l',
txt_gp = fpTxtGp(label=gpar(fontsize=12),summary=gpar(fontsize=12),ticks=gpar(cex=1), xlab=gpar(cex=1)))
sessionInfo()
#> R version 4.1.1 (2021-08-10)
#> Platform: x86_64-apple-darwin17.0 (64-bit)
#> Running under: macOS Big Sur 10.16
#>
#> Matrix products: default
#> BLAS: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
#>
#> locale:
#> [1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8
#>
#> attached base packages:
#> [1] grid stats graphics grDevices utils datasets methods
#> [8] base
#>
#> other attached packages:
#> [1] dplyr_1.0.7 forestplot_2.0.1 checkmate_2.0.0 magrittr_2.0.1
#>
#> loaded via a namespace (and not attached):
#> [1] pillar_1.6.3 compiler_4.1.1 highr_0.9 R.methodsS3_1.8.1
#> [5] R.utils_2.11.0 tools_4.1.1 digest_0.6.28 evaluate_0.14
#> [9] lifecycle_1.0.1 tibble_3.1.5 R.cache_0.15.0 pkgconfig_2.0.3
#> [13] rlang_0.4.11 reprex_2.0.1 DBI_1.1.1 yaml_2.2.1
#> [17] xfun_0.26 fastmap_1.1.0 withr_2.4.2 styler_1.6.2
#> [21] stringr_1.4.0 knitr_1.36 generics_0.1.0 fs_1.5.0
#> [25] vctrs_0.3.8 tidyselect_1.1.1 glue_1.4.2 R6_2.5.1
#> [29] fansi_0.5.0 rmarkdown_2.11 tidyr_1.1.4 purrr_0.3.4
#> [33] backports_1.2.1 ellipsis_0.3.2 htmltools_0.5.2 assertthat_0.2.1
#> [37] utf8_1.2.2 stringi_1.7.5 crayon_1.4.1 R.oo_1.24.0
Created on 2021-10-11 by the reprex package (v2.0.1)
Please edit your question to include the output from the command sessionInfo()
to help with troubleshooting