rstudio / distill

Distill for R Markdown
https://rstudio.github.io/distill/
Apache License 2.0
422 stars 57 forks source link

distill not playing nice with user-produced customized html? #250

Open harrelfe opened 3 years ago

harrelfe commented 3 years ago

I have several functions in the R Hmisc package that produce html for inclusion in R markdown reports. This works fine outside of distill (see for example https://hbiostat.org/R/Hmisc/examples.html). But with distill I'm getting text appearing in the right margin after customized html is inserted, and this text is supposed to appear as normal text in the report body. Here is my test report: https://hbiostat.org/R/Hmisc/examples2.Rmd and the output: https://hbiostat.org/R/Hmisc/examples2.html

Thanks

jjallaire commented 3 years ago

Thanks for the report! I'll investigate and address in the next release.

jjallaire commented 3 years ago

I see a couple of instances of HTML ending up in the margin. For the first one it comes from this Hmisc emitted HTML:

<hr><h4>Data frame:pbc</h4>418 observations and 19 variables, maximum # NAs:136  
 <hr>

I think if you break the text into a <p> tag it will render as you expect. As it stands it's actually not valid HTML, although browsers will render it w/o apparent problem. For distill though we actually look for <p> tags and convert them into l-body divs, which is not happening here. So something like this should work:

<hr><h4>Data frame:pbc</h4>
<p>418 observations and 19 variables, maximum # NAs:136 </p> 
 <hr>
jjallaire commented 3 years ago

I should correct myself: the HTML is valid, but it's not of a form that we see generated by pandoc (which always encloses top level text in paragraphs). If it's acceptable to add the <p> that will work. Otherwise we can see if there is another way to capture that text in l-body.

jjallaire commented 3 years ago

It turns out that the distill JS framework is the code that doesn't take well to top level text nodes. There is less I can do about that. I'd make 2 suggestions here:

1) Enclose all of your custom HTML in a <div> to protect it from Distill JS (I think this would work); or

2) Add the <p> tag to your generated HTML.

I know that it seems like Distill should in theory handle arbitrary generated HTML, but the Distill framework's CSS is does impose some structural requirements that make it can't handle absolutely anything thrown at it.

I'll look into the other instance shortly.

jjallaire commented 3 years ago

For the paragraph starting "Now show almost the full raw data" that appears in the margin in your example, I actually can't get that to repro locally with the following package versions (note I am running "local" versions of distill and rmarkdown which are equivalent to current GH master):

─ Session info ─────────────────────────────────────────────────────
 setting  value                       
 version  R version 4.0.3 (2020-10-10)
 os       macOS Catalina 10.15.5      
 system   x86_64, darwin17.0          
 ui       X11                         
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/New_York            
 date     2020-12-19                  

─ Packages ─────────────────────────────────────────────────────────
 package      * version  date       lib source        
 assertthat     0.2.1    2019-03-21 [1] CRAN (R 4.0.2)
 backports      1.2.0    2020-11-02 [1] CRAN (R 4.0.2)
 base64enc      0.1-3    2015-07-28 [1] CRAN (R 4.0.2)
 callr          3.5.1    2020-10-13 [1] CRAN (R 4.0.2)
 checkmate      2.0.0    2020-02-06 [1] CRAN (R 4.0.2)
 cli            2.2.0    2020-11-20 [1] CRAN (R 4.0.2)
 cluster        2.1.0    2019-06-19 [1] CRAN (R 4.0.3)
 colorspace     2.0-0    2020-11-11 [1] CRAN (R 4.0.2)
 crayon         1.3.4    2017-09-16 [1] CRAN (R 4.0.2)
 crosstalk      1.1.0.1  2020-03-13 [1] CRAN (R 4.0.2)
 data.table     1.13.2   2020-10-19 [1] CRAN (R 4.0.2)
 desc           1.2.0    2018-05-01 [1] CRAN (R 4.0.2)
 devtools       2.3.2    2020-09-18 [1] CRAN (R 4.0.2)
 digest         0.6.27   2020-10-24 [1] CRAN (R 4.0.2)
 distill        1.1.1    2020-12-19 [1] local         
 downlit        0.2.1    2020-11-04 [1] CRAN (R 4.0.2)
 dplyr          1.0.2    2020-08-18 [1] CRAN (R 4.0.2)
 ellipsis       0.3.1    2020-05-15 [1] CRAN (R 4.0.2)
 evaluate       0.14     2019-05-28 [1] CRAN (R 4.0.1)
 fansi          0.4.1    2020-01-08 [1] CRAN (R 4.0.2)
 farver         2.0.3    2020-01-16 [1] CRAN (R 4.0.2)
 foreign        0.8-80   2020-05-24 [1] CRAN (R 4.0.3)
 Formula      * 1.2-4    2020-10-16 [1] CRAN (R 4.0.2)
 fs             1.5.0    2020-07-31 [1] CRAN (R 4.0.2)
 generics       0.1.0    2020-10-31 [1] CRAN (R 4.0.2)
 ggplot2      * 3.3.2    2020-06-19 [1] CRAN (R 4.0.2)
 glue           1.4.2    2020-08-27 [1] CRAN (R 4.0.2)
 gridExtra      2.3      2017-09-09 [1] CRAN (R 4.0.2)
 gtable         0.3.0    2019-03-25 [1] CRAN (R 4.0.2)
 Hmisc        * 4.4-2    2020-11-29 [1] CRAN (R 4.0.2)
 htmlTable      2.1.0    2020-09-16 [1] CRAN (R 4.0.2)
 htmltools      0.5.0    2020-06-16 [1] CRAN (R 4.0.2)
 htmlwidgets    1.5.2    2020-10-03 [1] CRAN (R 4.0.2)
 httr           1.4.2    2020-07-20 [1] CRAN (R 4.0.2)
 jpeg           0.1-8.1  2019-10-24 [1] CRAN (R 4.0.2)
 jsonlite       1.7.1    2020-09-07 [1] CRAN (R 4.0.2)
 knitr          1.30     2020-09-22 [1] CRAN (R 4.0.2)
 labeling       0.4.2    2020-10-20 [1] CRAN (R 4.0.2)
 lattice      * 0.20-41  2020-04-02 [1] CRAN (R 4.0.3)
 latticeExtra   0.6-29   2019-12-19 [1] CRAN (R 4.0.2)
 lazyeval       0.2.2    2019-03-15 [1] CRAN (R 4.0.2)
 lifecycle      0.2.0    2020-03-06 [1] CRAN (R 4.0.2)
 magrittr       2.0.1    2020-11-17 [1] CRAN (R 4.0.2)
 Matrix         1.2-18   2019-11-27 [1] CRAN (R 4.0.3)
 memoise        1.1.0    2017-04-21 [1] CRAN (R 4.0.2)
 munsell        0.5.0    2018-06-12 [1] CRAN (R 4.0.2)
 nnet           7.3-14   2020-04-26 [1] CRAN (R 4.0.3)
 pillar         1.4.7    2020-11-20 [1] CRAN (R 4.0.2)
 pkgbuild       1.1.0    2020-07-13 [1] CRAN (R 4.0.2)
 pkgconfig      2.0.3    2019-09-22 [1] CRAN (R 4.0.2)
 pkgload        1.1.0    2020-05-29 [1] CRAN (R 4.0.2)
 plotly         4.9.2.1  2020-04-04 [1] CRAN (R 4.0.2)
 png            0.1-7    2013-12-03 [1] CRAN (R 4.0.2)
 prettyunits    1.1.1    2020-01-24 [1] CRAN (R 4.0.2)
 processx       3.4.4    2020-09-03 [1] CRAN (R 4.0.2)
 ps             1.4.0    2020-10-07 [1] CRAN (R 4.0.2)
 purrr          0.3.4    2020-04-17 [1] CRAN (R 4.0.2)
 R6             2.5.0    2020-10-28 [1] CRAN (R 4.0.2)
 RColorBrewer   1.1-2    2014-12-07 [1] CRAN (R 4.0.2)
 remotes        2.2.0    2020-07-21 [1] CRAN (R 4.0.2)
 rlang          0.4.8    2020-10-08 [1] CRAN (R 4.0.2)
 rmarkdown      2.6.0003 2020-12-19 [1] local         
 rpart          4.1-15   2019-04-12 [1] CRAN (R 4.0.3)
 rprojroot      2.0.2    2020-11-15 [1] CRAN (R 4.0.2)
 rstudioapi     0.13     2020-11-12 [1] CRAN (R 4.0.2)
 scales         1.1.1    2020-05-11 [1] CRAN (R 4.0.2)
 sessioninfo    1.1.1    2018-11-05 [1] CRAN (R 4.0.2)
 stringi        1.5.3    2020-09-09 [1] CRAN (R 4.0.2)
 stringr        1.4.0    2019-02-10 [1] CRAN (R 4.0.2)
 survival     * 3.2-7    2020-09-28 [1] CRAN (R 4.0.3)
 testthat       3.0.0    2020-10-31 [1] CRAN (R 4.0.2)
 tibble         3.0.4    2020-10-12 [1] CRAN (R 4.0.2)
 tidyr          1.1.2    2020-08-27 [1] CRAN (R 4.0.2)
 tidyselect     1.1.0    2020-05-11 [1] CRAN (R 4.0.2)
 usethis        1.6.3    2020-09-17 [1] CRAN (R 4.0.2)
 vctrs          0.3.5    2020-11-17 [1] CRAN (R 4.0.2)
 viridis        0.5.1    2018-03-29 [1] CRAN (R 4.0.2)
 viridisLite    0.3.0    2018-02-01 [1] CRAN (R 4.0.1)
 withr          2.3.0    2020-09-22 [1] CRAN (R 4.0.2)
 xfun           0.19     2020-10-30 [1] CRAN (R 4.0.2)
 yaml           2.2.1    2020-02-01 [1] CRAN (R 4.0.2)
jjallaire commented 3 years ago

There is another paragraph which is transformed into a pile of base64 gobbly gook. That also ends up in the margin but I think the real/bigger problem is that it got mangled. This was likely an Rmd chunk that was flush up against another block so we failed to parse it correctly when switching the document into visual mode.

We have fix that makes this sort of mangling impossible: https://github.com/rstudio/rstudio/pull/8626. That fix is merged into the v1.4 patch release but I'm making the case now that we need to merge it into v1.4 proper.