strengejacke / sjstats

Effect size measures and significance tests
https://strengejacke.github.io/sjstats
189 stars 21 forks source link

Issue with colored output when knit #81

Closed jmgirard closed 4 years ago

jmgirard commented 5 years ago

I am using tidy_stan() within an R Notebook and the colored text does not seem to be escaping properly in the knitted notebook. Here is a toy example using brms:

    ---
    title: "R Notebook"
    output: html_notebook
    ---

    ```{r}
    library(sjstats)
    library(brms)
    fit <- brm(
      formula = count ~ zAge + zBase * Trt + (1 | patient),
      data = epilepsy, 
      family = poisson(), 
      prior = c(
        prior(student_t(5, 0, 10), class = b),
        prior(cauchy(0, 2), class = sd)
      ),
      refresh = 0
    )
    tidy_stan(fit)


![screenshot](https://user-images.githubusercontent.com/6829033/57881103-6b70ff00-77ee-11e9-9347-dc9d290364c5.png)
strengejacke commented 5 years ago

Does this help as workaround? (see last post) https://github.com/r-lib/crayon/issues/24

strengejacke commented 5 years ago

Or this: https://twitter.com/BrodieGaslam/status/1029059002225721344

strengejacke commented 5 years ago

maybe there is a way to wrap the print() method of tidy_stan() in a knitr-command, like knitr_print.tidy_stan(), and then skip the colored output.

(reminder: see tibble:::knit_print.trunc_mat())

jmgirard commented 5 years ago

Unfortunately, the two workarounds did not work for me.

strengejacke commented 5 years ago

Actually, I can't reproduce your issue. Are all your packages (related to knitr, tibble, ...) up to date?

But anyway, I tried something that might fix your issue, could you install from GitHub and check?

jmgirard commented 5 years ago

When using print(tidy_stan(fit)) I get the same issue with the dev version of sjstats. Everything seems to be up to date:

R version 3.6.0 (2019-04-26) 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] brms_2.8.0 Rcpp_1.0.1 sjstats_0.17.4.9000 loaded via a namespace (and not attached): [1] nlme_3.1-139 matrixStats_0.54.0 xts_0.11-2 [4] insight_0.3.0 threejs_0.3.1 rstan_2.18.2 [7] tools_3.6.0 backports_1.1.4 R6_2.4.0 [10] DT_0.6 sjlabelled_1.0.17 lazyeval_0.2.2 [13] colorspace_1.4-1 prettyunits_1.0.2 processx_3.3.1 [16] tidyselect_0.2.5 gridExtra_2.3 Brobdingnag_1.2-6 [19] emmeans_1.3.4 compiler_3.6.0 cli_1.1.0 [22] performance_0.1.0.9000 shinyjs_1.0 colourpicker_1.0 [25] bayestestR_0.1.5 scales_1.0.0 dygraphs_1.1.1.6 [28] mvtnorm_1.0-10 callr_3.2.0 ggridges_0.5.1 [31] StanHeaders_2.18.1 stringr_1.4.0 digest_0.6.18 [34] minqa_1.2.4 rmarkdown_1.12 base64enc_0.1-3 [37] pkgconfig_2.0.2 htmltools_0.3.6 lme4_1.1-21 [40] htmlwidgets_1.3 rlang_0.3.4 rstudioapi_0.10 [43] shiny_1.3.2 generics_0.0.2 jsonlite_1.6 [46] zoo_1.8-5 crosstalk_1.0.0 gtools_3.8.1 [49] dplyr_0.8.1 inline_0.3.15 magrittr_1.5 [52] loo_2.1.0 bayesplot_1.6.0 Matrix_1.2-17 [55] munsell_0.5.0 abind_1.4-5 yaml_2.2.0 [58] stringi_1.4.3 MASS_7.3-51.4 pkgbuild_1.0.3 [61] plyr_1.8.4 grid_3.6.0 parallel_3.6.0 [64] promises_1.0.1 sjmisc_2.7.9 forcats_0.4.0 [67] crayon_1.3.4 miniUI_0.1.1.1 lattice_0.20-38 [70] haven_2.1.0 splines_3.6.0 hms_0.4.2 [73] ps_1.3.0 knitr_1.22 pillar_1.4.0 [76] igraph_1.2.4.1 boot_1.3-22 markdown_0.9 [79] estimability_1.3 shinystan_2.5.0 reshape2_1.4.3 [82] stats4_3.6.0 rstantools_1.5.1 glue_1.3.1 [85] evaluate_0.13 packrat_0.5.0 modelr_0.1.4 [88] nloptr_1.2.1 httpuv_1.5.1 gtable_0.3.0 [91] purrr_0.3.2 tidyr_0.8.3 assertthat_0.2.1 [94] ggplot2_3.1.1 xfun_0.7 mime_0.6 [97] xtable_1.8-4 broom_0.5.2 coda_0.19-2 [100] later_0.8.0 rsconnect_0.8.13 tibble_2.1.1 [103] shinythemes_1.1.2 bridgesampling_0.6-0
jmgirard commented 5 years ago

This seems to be getting closer. No color but also an error message:

library(sjstats)
library(brms)
fit <- brm(
  formula = count ~ zAge + zBase * Trt + (1 | patient),
  data = epilepsy, 
  family = poisson(), 
  prior = c(
    prior(student_t(5, 0, 10), class = b),
    prior(cauchy(0, 2), class = sd)
  ),
  refresh = 0,
  file = "test"
)
sjstats:::knit_print.tidy_stan(tidy_stan(fit))

# Summary Statistics of Stan-Model

Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
  Consider 'structure(list(), *)' instead.Error in enc2utf8(x) : argument is not a character vector
strengejacke commented 5 years ago

You should run the inital code, just tidy_stan().

jmgirard commented 5 years ago

No change - it doesn't seem to find the knit_print function properly and is still blue in console and unescaped in HTML.

screenshot2

strengejacke commented 5 years ago

The preview doesn't seem to work, I guess, but what if you knit the HTML file?

jmgirard commented 5 years ago

The HTML file when using output: html_notebook is still wrong. However, if I change to output: html_document I get the following:

## 
## # Summary Statistics of Stan-Model
## 
##             estimate std.error ci.lvl     HDI(89%) ratio rhat mcse
##  Intercept      1.77      0.12     89 [ 1.58 1.96]  0.16 1.00 0.00
##  zAge           0.09      0.09     89 [-0.04 0.23]  0.16 1.00 0.00
##  zBase          0.71      0.12     89 [ 0.52 0.89]  0.18 1.01 0.00
##  Trt1          -0.27      0.16     89 [-0.52 0.01]  0.17 1.00 0.01
##  zBase.Trt1     0.06      0.16     89 [-0.20 0.30]  0.19 1.00 0.01
## Warning in structure(x, class = "knit_asis", knit_meta = meta, knit_cacheable = cacheable): Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
##   Consider 'structure(list(), *)' instead.

So this looks good (excepting the warning).

strengejacke commented 4 years ago

strange, I just copied your initial code and got this:

Unbenannt

jmgirard commented 4 years ago

I am getting this now: image

jmgirard commented 4 years ago

As a temporary work-around, this works decently:

image

strengejacke commented 4 years ago

Closing, as I'm not sure if this is actually sjstats related, or how to fix this.