Closed elfatherbrown closed 3 years ago
BTW. This does exactly the same thing for thematic from rstudio/thematic or install.packages("thematic").
I have tested this in two boxes, both versions.
Interesting, thanks for the report. Is your package publicly available?
Could you also include sessioninfo::session_info()
?
Ahh, interesting, I can replicate with just this:
library(survMisc)
#> Loading required package: survival
thematic::thematic_on()
And it appears {survMisc}
isn't doing anything crazy in .onLoad()
or .onAttach()
π¬
Yes. Thanks for your atention. Sessioninfo at the end.
survminer does the same. I cant think off the top of my head of other packages that import ggplot. My own package is a mess, i'm learning. I hope it will be public quality some day. Thankfully I could reproduce it with something else.
Session info:
> sessioninfo::session_info()
β Session info ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
setting value
version R version 3.6.3 (2020-02-29)
os Ubuntu 20.04.2 LTS
system x86_64, linux-gnu
ui RStudio
language (EN)
collate en_US.UTF-8
ctype en_US.UTF-8
tz America/Mazatlan
date 2021-02-16
β Packages ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
package * version date lib source
abind 1.4-5 2016-07-21 [3] CRAN (R 3.5.0)
assertthat 0.2.1 2019-03-21 [3] CRAN (R 3.6.1)
backports 1.1.5 2019-10-02 [3] CRAN (R 3.6.1)
broom 0.7.4 2021-01-29 [1] CRAN (R 3.6.3)
callr 3.5.1 2020-10-13 [1] CRAN (R 3.6.3)
car 3.0-10 2020-09-29 [1] CRAN (R 3.6.3)
carData 3.0-4 2020-05-22 [1] CRAN (R 3.6.3)
cellranger 1.1.0 2016-07-27 [3] CRAN (R 3.5.0)
cli 2.3.0 2021-01-31 [1] CRAN (R 3.6.3)
clipr 0.7.0 2019-07-23 [3] CRAN (R 3.6.1)
colorspace 2.0-0 2020-11-11 [1] CRAN (R 3.6.3)
crayon 1.4.1 2021-02-08 [1] CRAN (R 3.6.3)
curl 4.3 2019-12-02 [3] CRAN (R 3.6.1)
data.table 1.12.8 2019-12-09 [3] CRAN (R 3.6.2)
DBI 1.1.0 2019-12-15 [3] CRAN (R 3.6.2)
digest 0.6.27 2020-10-24 [1] CRAN (R 3.6.3)
dplyr 1.0.4 2021-02-02 [1] CRAN (R 3.6.3)
ellipsis 0.3.1 2020-05-15 [1] CRAN (R 3.6.3)
evaluate 0.14 2019-05-28 [3] CRAN (R 3.6.1)
forcats 0.4.0 2019-02-17 [3] CRAN (R 3.6.1)
foreign 0.8-76 2020-03-03 [4] CRAN (R 3.6.3)
fs 1.3.1 2019-05-06 [3] CRAN (R 3.6.3)
generics 0.0.2 2018-11-29 [3] CRAN (R 3.5.1)
ggplot2 * 3.3.3 2020-12-30 [1] CRAN (R 3.6.3)
ggpubr * 0.4.0 2020-06-27 [1] CRAN (R 3.6.3)
ggsignif 0.6.0 2019-08-08 [1] CRAN (R 3.6.3)
glue 1.4.2 2020-08-27 [1] CRAN (R 3.6.3)
gridExtra 2.3 2017-09-09 [1] CRAN (R 3.6.3)
gtable 0.3.0 2019-03-25 [3] CRAN (R 3.6.1)
haven 2.2.0 2019-11-08 [3] CRAN (R 3.6.3)
highr 0.8 2019-03-20 [3] CRAN (R 3.6.1)
hms 0.5.3 2020-01-08 [3] CRAN (R 3.6.2)
htmltools 0.4.0 2019-10-04 [3] CRAN (R 3.6.3)
km.ci 0.5-2 2009-08-30 [1] CRAN (R 3.6.3)
KMsurv 0.1-5 2012-12-03 [1] CRAN (R 3.6.3)
knitr 1.28 2020-02-06 [3] CRAN (R 3.6.2)
lattice 0.20-41 2020-04-02 [4] CRAN (R 3.6.3)
lifecycle 1.0.0 2021-02-15 [1] CRAN (R 3.6.3)
magrittr 2.0.1 2020-11-17 [1] CRAN (R 3.6.3)
Matrix 1.3-2 2021-01-06 [4] CRAN (R 3.6.3)
munsell 0.5.0 2018-06-12 [3] CRAN (R 3.5.0)
openxlsx 4.2.3 2020-10-27 [1] CRAN (R 3.6.3)
pillar 1.4.7 2020-11-20 [1] CRAN (R 3.6.3)
pkgconfig 2.0.3 2019-09-22 [3] CRAN (R 3.6.1)
processx 3.4.5 2020-11-30 [1] CRAN (R 3.6.3)
ps 1.5.0 2020-12-05 [1] CRAN (R 3.6.3)
purrr 0.3.3 2019-10-18 [3] CRAN (R 3.6.1)
R6 2.5.0 2020-10-28 [1] CRAN (R 3.6.3)
Rcpp 1.0.3 2019-11-08 [3] CRAN (R 3.6.3)
readxl 1.3.1 2019-03-13 [3] CRAN (R 3.6.3)
reprex 1.0.0 2021-01-27 [1] CRAN (R 3.6.3)
rio 0.5.16 2018-11-26 [1] CRAN (R 3.6.3)
rlang 0.4.10 2020-12-30 [1] CRAN (R 3.6.3)
rmarkdown 2.1 2020-01-20 [3] CRAN (R 3.6.2)
rstatix 0.7.0 2021-02-13 [1] CRAN (R 3.6.3)
rstudioapi 0.13 2020-11-12 [1] CRAN (R 3.6.3)
scales 1.1.1 2020-05-11 [1] CRAN (R 3.6.3)
sessioninfo 1.1.1 2018-11-05 [3] CRAN (R 3.5.1)
stringi 1.4.6 2020-02-17 [3] CRAN (R 3.6.3)
survival 3.2-7 2020-09-28 [4] CRAN (R 3.6.3)
survminer * 0.4.8 2020-07-25 [1] CRAN (R 3.6.3)
survMisc 0.5.5 2018-07-05 [1] CRAN (R 3.6.3)
thematic * 0.1.1.9000 2021-02-16 [1] Github (rstudio/thematic@03b8c7b)
tibble 3.0.6 2021-01-29 [1] CRAN (R 3.6.3)
tidyr 1.0.2 2020-01-24 [3] CRAN (R 3.6.3)
tidyselect 1.1.0 2020-05-11 [1] CRAN (R 3.6.3)
vctrs 0.3.6 2020-12-17 [1] CRAN (R 3.6.3)
withr 2.4.1 2021-01-26 [1] CRAN (R 3.6.3)
xfun 0.12 2020-01-13 [3] CRAN (R 3.6.2)
xtable 1.8-4 2019-04-21 [3] CRAN (R 3.6.1)
yaml 2.2.1 2020-02-01 [3] CRAN (R 3.6.2)
zip 2.1.1 2020-08-27 [1] CRAN (R 3.6.3)
zoo 1.8-8 2020-05-02 [1] CRAN (R 3.6.3)
My hunch was that this is related to the way that survMisc::autoplot
works, but even after removing it, I still see this, which is super weird:
> .getNamespaceInfo(asNamespace("ggplot2"), "S3methods")[1:5, 3]
[1] "$.ggproto" "$.ggproto_parent" "$<-.uneval"
[4] "+.gg" "[.mapped_discrete"
> library(survMisc)
Loading required package: survival
Registered S3 method overwritten by 'survMisc':
method from
plot.Surv survival
survMisc0.5.1
> .getNamespaceInfo(asNamespace("ggplot2"), "S3methods")[1:5, 3]
[[1]]
[1] "$.ggproto"
[[2]]
[1] "$.ggproto_parent"
[[3]]
[1] "$<-.uneval"
[[4]]
[1] "+.gg"
[[5]]
[1] "[.mapped_discrete"
But check it out. Also the survminer package, not survMisc, shows the same problem:
library(thematic)
#> This version of thematic is designed to work with rmarkdown version 2.6.6 or higher. Consider upgrading via remotes::install_github('rstudio/rmarkdown')
thematic::thematic_on()
thematic::thematic_off()
library(survminer)
#> Loading required package: ggplot2
#> Loading required package: ggpubr
thematic::thematic_on()
#> Error in get(S3[i, 1L], mode = "function", envir = parent.frame()): invalid first argument
This is how i rewrote ggplot_build_set to debug it, but i have no idea if its meaningful (probably not):
ggplot_build_set <- function() {
browser()
if (!is_installed("ggplot2")) return(NULL)
ggplot_build_restore()
# Note that assignInNamespace() does S3 method registration, but to
# find the relevant generic, it looks in the parent.frame()...
# so this line here is prevent that from failing if ggplot2 hasn't been attached
# https://github.com/wch/r-source/blob/d0ede8/src/library/utils/R/objects.R#L472
ggplot_build <- getFromNamespace("ggplot_build", "ggplot2")
.globals$ggplot_build <- getFromNamespace("ggplot_build.ggplot", "ggplot2")
assign_in_namespace<-function (x, value, ns, pos = -1, envir = as.environment(pos))
{
browser()
nf <- sys.nframe()
if (missing(ns)) {
nm <- attr(envir, "name", exact = TRUE)
if (is.null(nm) || substr(nm, 1L, 8L) != "package:")
stop("environment specified is not a package")
ns <- asNamespace(substring(nm, 9L))
}
else ns <- asNamespace(ns)
ns_name <- getNamespaceName(ns)
if (nf > 1L) {
if (ns_name %in% tools:::.get_standard_package_names()$base)
stop("locked binding of ", sQuote(x), " cannot be changed",
domain = NA)
}
if (bindingIsLocked(x, ns)) {
in_load <- Sys.getenv("_R_NS_LOAD_")
if (nzchar(in_load)) {
if (in_load != ns_name) {
msg <- gettextf("changing locked binding for %s in %s whilst loading %s",
sQuote(x), sQuote(ns_name), sQuote(in_load))
if (!in_load %in% c("Matrix", "SparseM"))
warning(msg, call. = FALSE, domain = NA, immediate. = TRUE)
}
}
else if (nzchar(Sys.getenv("_R_WARN_ON_LOCKED_BINDINGS_"))) {
warning(gettextf("changing locked binding for %s in %s",
sQuote(x), sQuote(ns_name)), call. = FALSE, domain = NA,
immediate. = TRUE)
}
unlockBinding(x, ns)
assign(x, value, envir = ns, inherits = FALSE)
w <- options("warn")
on.exit(options(w))
options(warn = -1)
lockBinding(x, ns)
}
else {
assign(x, value, envir = ns, inherits = FALSE)
}
if (!isBaseNamespace(ns)) {
S3 <- .getNamespaceInfo(ns, "S3methods")
if (!length(S3))
return(invisible(NULL))
S3names <- S3[, 3L]
if (x %in% S3names) {
i <- match(x, S3names)
browser()
#This is where the error happens
genfun <- get(S3[i, 1L], mode = "function", envir = parent.frame())
# If you change the S3 access to: S3[i, 1L][[1]], then the get succeeds.
if (.isMethodsDispatchOn() && methods::is(genfun,
"genericFunction"))
genfun <- methods::slot(genfun, "default")@methods$ANY
defenv <- if (typeof(genfun) == "closure")
environment(genfun)
else .BaseNamespaceEnv
S3Table <- get(".__S3MethodsTable__.", envir = defenv)
remappedName <- paste(S3[i, 1L], S3[i, 2L], sep = ".")
if (exists(remappedName, envir = S3Table, inherits = FALSE))
assign(remappedName, value, S3Table)
}
}
invisible(NULL)
}
assign_in_namespace("ggplot_build.ggplot", ggthematic_build, "ggplot2")
}
Looks like the problem derives from the zoo package (a dependency of these packages):
> library(zoo); thematic::thematic_on()
Error in get(S3[i, 1L], mode = "function", envir = parent.frame()) :
invalid first argument
Turns out that library(zoo)
causes this problem since it's calling registerS3method()
in an .onLoad()
hook, which unfortunately causes assignInNamespace()
to no longer work (this seems like a bug in R itself). A minimal example:
library(ggplot2)
# works
ggplotBuild <- getFromNamespace("ggplot_build.ggplot", "ggplot2")
assignInNamespace("ggplot_build.ggplot", function(x) { warning("owned"); ggplotBuild(x) }, "ggplot2")
qplot(1:10)
# doesn't work
library(zoo)
assignInNamespace("ggplot_build.ggplot", function(x) { warning("owned"); ggplotBuild(x) }, "ggplot2")
#> Error in get(S3[i, 1L], mode = "function", envir = parent.frame()) :
#> invalid first argument
And this happens because the S3 lookup matrix turns into a list when registerS3method()
happens:
is.matrix(.getNamespaceInfo(asNamespace("ggplot2"), "S3methods"))
#> TRUE
library(zoo)
is.matrix(.getNamespaceInfo(asNamespace("ggplot2"), "S3methods"))
#> FALSE
I may submit a bug to R core for this, but in the meantime, I think I can have thematic workaround this
Tested s3methods-matrix branch. My problem has gone away. Thanks!
Turns out that
library(zoo)
causes this problem since it's callingregisterS3method()
in an.onLoad()
hook, which unfortunately causesassignInNamespace()
to no longer work (this seems like a bug in R itself). A minimal example:library(ggplot2) # works ggplotBuild <- getFromNamespace("ggplot_build.ggplot", "ggplot2") assignInNamespace("ggplot_build.ggplot", function(x) { warning("owned"); ggplotBuild(x) }, "ggplot2") qplot(1:10) # doesn't work library(zoo) assignInNamespace("ggplot_build.ggplot", function(x) { warning("owned"); ggplotBuild(x) }, "ggplot2") #> Error in get(S3[i, 1L], mode = "function", envir = parent.frame()) : #> invalid first argument
And this happens because the S3 lookup matrix turns into a list when
registerS3method()
happens:is.matrix(.getNamespaceInfo(asNamespace("ggplot2"), "S3methods")) #> TRUE library(zoo) is.matrix(.getNamespaceInfo(asNamespace("ggplot2"), "S3methods")) #> FALSE
I may submit a bug to R core for this, but in the meantime, I think I can have thematic workaround this
Hi. I still face the same problem
#> Error in get(S3[i, 1L], mode = "function", envir = parent.frame()) :
#> invalid first argument
Here is my sessionInfo
R version 4.0.4 (2021-02-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=Greek_Greece.1253
[2] LC_CTYPE=Greek_Greece.1253
[3] LC_MONETARY=Greek_Greece.1253
[4] LC_NUMERIC=C
[5] LC_TIME=Greek_Greece.1253
attached base packages:
[1] stats graphics grDevices utils datasets
[6] methods base
other attached packages:
[1] zoo_1.8-8 ggplot2_3.3.3
loaded via a namespace (and not attached):
[1] Rcpp_1.0.6 bslib_0.2.4 compiler_4.0.4
[4] pillar_1.5.1 jquerylib_0.1.3 tools_4.0.4
[7] odbc_1.3.0 digest_0.6.27 bit_4.0.4
[10] lattice_0.20-41 gtable_0.3.0 evaluate_0.14
[13] jsonlite_1.7.2 lifecycle_1.0.0 tibble_3.1.0
[16] debugme_1.1.0 pkgconfig_2.0.3 rlang_0.4.10
[19] DBI_1.1.1 rstudioapi_0.13 yaml_2.2.1
[22] xfun_0.21 withr_2.4.1 dplyr_1.0.5
[25] knitr_1.31 htmlwidgets_1.5.3 generics_0.1.0
[28] vctrs_0.3.6 sass_0.3.1 hms_1.0.0
[31] thematic_0.1.1 bit64_4.0.5 grid_4.0.4
[34] tidyselect_1.1.0 glue_1.4.2 R6_2.5.0
[37] fansi_0.4.2 rmarkdown_2.7 librarian_1.7.0
[40] farver_2.1.0 purrr_0.3.4 blob_1.2.1
[43] magrittr_2.0.1 scales_1.1.1 ellipsis_0.3.1
[46] htmltools_0.5.1.1 rsconnect_0.8.16 assertthat_0.2.1
[49] colorspace_2.0-0 labeling_0.4.2 utf8_1.1.4
[52] munsell_0.5.0 crayon_1.4.1
You need the development version remotes::install_github('rstudio/thematic')
Hi, I'm still receiving this error as well. This is my yaml and first code chunk:
---
title: "ETAN Database Exploration Tool"
output:
html_document:
theme:
bg: "#002b36"
fg: "#eee8d5"
primary: "#2aa198"
font: "auto"
# runtime: shiny
---
'''{r setup, include=FALSE}
knitr::opts_chunk$set(
cache = FALSE, cache.lazy = FALSE, autodep = TRUE, warning = FALSE,
message = FALSE, echo = TRUE, dpi = 180,
fig.width = 8, fig.height = 5, echo = FALSE
)
thematic::thematic_rmd(font = "auto")
'''
I'm receiving the following error both in isolated code chunks that generate ggplot visuals and during markdown knit.
> > sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] shiny_1.6.0 gridExtra_2.3 scales_1.1.1 ggraph_2.0.5
[5] tidygraph_1.2.0 networkD3_0.4 thematic_0.1.2.1 ggridges_0.5.3
[9] here_1.0.1 data.validator_0.1.5 skimr_2.1.2 DT_0.17
[13] lubridate_1.7.9.2 data.table_1.13.2 janitor_2.1.0 readxl_1.3.1
[17] magrittr_2.0.1 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.4
[21] purrr_0.3.4 readr_1.4.0 tidyr_1.1.3 tibble_3.1.0
[25] ggplot2_3.3.3 tidyverse_1.3.0
loaded via a namespace (and not attached):
[1] colorspace_2.0-0 ellipsis_0.3.1 rsconnect_0.8.16 rprojroot_2.0.2
[5] snakecase_0.11.0 base64enc_0.1-3 fs_1.5.0 rstudioapi_0.13
[9] farver_2.0.3 rstan_2.21.2 graphlayouts_0.7.1 ggrepel_0.9.1
[13] fansi_0.4.2 xml2_1.3.2 codetools_0.2-16 knitr_1.31
[17] polyclip_1.10-0 jsonlite_1.7.2 packrat_0.5.0 broom_0.7.5
[21] gganimate_1.0.7 dbplyr_2.1.0 ggforce_0.3.2 compiler_3.6.2
[25] httr_1.4.2 backports_1.2.1 assertthat_0.2.1 fastmap_1.0.1
[29] cli_2.3.1 later_1.1.0.1 tweenr_1.0.1 htmltools_0.5.1.1
[33] prettyunits_1.1.1 tools_3.6.2 igraph_1.2.6 gtable_0.3.0
[37] glue_1.4.2 V8_3.4.0 Rcpp_1.0.6 jquerylib_0.1.3
[41] cellranger_1.1.0 vctrs_0.3.6 crosstalk_1.1.1 xfun_0.19
[45] ps_1.3.2 rvest_0.3.6 mime_0.9 lifecycle_1.0.0
[49] MASS_7.3-51.4 hms_1.0.0 promises_1.1.1 parallel_3.6.2
[53] inline_0.3.17 yaml_2.2.1 curl_4.3 sass_0.3.1
[57] loo_2.4.1 StanHeaders_2.21.0-6 stringi_1.5.3 gifski_0.8.6
[61] pkgbuild_1.2.0 repr_1.1.3 matrixStats_0.57.0 rlang_0.4.10
[65] pkgconfig_2.0.3 evaluate_0.14 lattice_0.20-38 htmlwidgets_1.5.3
[69] labeling_0.4.2 tidyselect_1.1.0 processx_3.4.4 plyr_1.8.6
[73] R6_2.5.0 generics_0.1.0 DBI_1.1.1 pillar_1.6.0
[77] haven_2.3.1 withr_2.4.1 modelr_0.1.8 crayon_1.4.1
[81] utf8_1.1.4 rmarkdown_2.7 viridis_0.5.1 progress_1.2.2
[85] grid_3.6.2 callr_3.5.1 reprex_1.0.0 digest_0.6.27
[89] xtable_1.8-4 httpuv_1.5.4 RcppParallel_5.0.2 stats4_3.6.2
[93] munsell_0.5.0 viridisLite_0.3.0 bslib_0.2.4
@michaelgaunt404 your theme
definition is off. You probably want base_font
(not font
) set to a Google Font? https://rstudio.github.io/bslib/articles/bslib.html#custom-themes
If that doesn't fix your issues, you might need to update to a more recent version of R
The problem
I have a shiny app built as a package with golem that uses many plotting libraries including ggplot2 and as soon as I tried thematic_on or thematic_shiny I got:
"Error in get(S3[i, 1L], mode = "function", envir = parent.frame()): invalid first argument".
I digged in and discovered that the same problem that happens with my package, happens with others that include ggplot2:
Created on 2021-02-16 by the reprex package (v1.0.0)
Further digging on thematic's code
On another box I set out to debug thematic. I cloned it and this is what I did.
On this function I put a browser:
Discovered that the aforementioned "get" problem with S3 comes from assign_in_namespace("ggplot_build.ggplot", ggthematic_build, "ggplot2"). So I copied the assign_in_namespace (from assignInNamespace, as it does in the code), but set a browser on that.
What I discovered is that inside the assignInNamespace code, down by the line:
S3[i,1L] returns a list that contains the name of the function, but get wants a string. So, as you can see:
This goes away if one rewrites with:
Or at least the error does. Im not keen on hacking away at this because im pretty much a newb (i've no idea how S3 or namespaces actually work), but I think this is either a freakish mismanagement of my base packages (e.g. i carry a bug in utils::assignInNamespace), or a particular use of it that got mangled somewhere.
I LOVE the thematic idea. I want to use it! Let me know if I can help out.