gforge / forestplot

Advanced forest plots in R using grid graphics
44 stars 15 forks source link

forestplot graph.pos incompatible with list for labeltext #30

Closed MetaEntropy closed 4 years ago

MetaEntropy commented 4 years ago

Bug description

When labeltext is a list, graph.pos cannot be set to any value above 2. That bug is due to incorrect calculation of the number of columns of the list. The following code illustrate this problem:

author=expression(paste("Smith ", italic("et al")))
pseudomat=list(Author=list(author, author), Year=list(2002,2005), N=list(50,75), Weight=list(0.40,0.60))
forestplot(labeltext = pseudomat, mean=1:2, lower=0:1, upper=4:5, graph.pos=4)

Expected behavior: display three data columns (Author, Year, N), then, the forest plot, than a fourth data column (Weight). Observed behavior: the following error is raised: "Error in forestplot.default(labeltext = pseudomat, mean = 1:2, lower = 0:1, : The graph position must be between 1 and 2. You have provided the value '4'."

Current workaround

Sets the class of pseudomat to "data.frame" in order to override the behavior of ncol().

author=expression(paste("Smith ", italic("et al")))
pseudomat=list(Author=list(author, author), Year=list(2002,2005), N=list(50,75), Weight=list(0.40,0.60))
class(pseudomat)="data.frame" # workaround (ugly hack)
forestplot(labeltext = pseudomat, mean=1:2, lower=0:1, upper=4:5, graph.pos=4)

That hack is horrendous. The pseudomat object becomes a corrupted data.frame object, hybrid between a list of list and a real data.frame, tricking forestplot for its ncol() calculation.

Bug fix

This bug can be found on line 442 to 447 of the forestplot.R file. The code checks that the graph.pos is in the correct range, but uses NCOL(labeltext) rather than nc as the number of columns.

See pull request 81dc455

Test environment

Package: forestplot Version: 1.9 Maintainer: Max Gordon max@gforge.se Built: R 4.1.0; ; 2020-07-09 04:36:23 UTC; windows

R Version: platform = x86_64-w64-mingw32 arch = x86_64 os = mingw32 system = x86_64, mingw32 status = Under development (unstable) major = 4 minor = 1.0 year = 2020 month = 07 day = 11 svn rev = 78812 language = R version.string = R Under development (unstable) (2020-07-11 r78812) nickname = Unsuffered Consequences

Windows 10 x64 (build 17134)

Locale: LC_COLLATE=French_France.1252;LC_CTYPE=French_France.1252;LC_MONETARY=French_France.1252;LC_NUMERIC=C;LC_TIME=French_France.1252

Search Path: .GlobalEnv, package:forestplot, package:checkmate, package:magrittr, package:grid, package:stats, package:graphics, package:grDevices, package:utils, package:datasets, package:methods, Autoloads, package:base

gforge commented 4 years ago

Thanks, excellent suggestion, fix and bug-report. Could you create a PR with a test case? I'll then push it to CRAN this week.