Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data.frames/tibbles not output as paged in RMarkdown after upgrading to R 3.5.0 #1331

Closed
agranholm opened this issue May 1, 2018 · 8 comments
Assignees
Labels
bug an unexpected problem or unintended behavior

Comments

@agranholm
Copy link

agranholm commented May 1, 2018

Since upgrading to R 3.5.0, there appears to be an issue with inline display and rendering to html of data.frames/tibbles when using R Notebooks - they are not rendered as paged tables, unless it is a grouped tibble (and for now, grouping a tibble by any variable before displaying seems to solve the problem).

Reproducible example including the results of the rendering (and written how it appears inline in RStudio):

https://rpubs.com/Granholm/384010

This happens only in R Notebooks, and adding df_print: paged to the header does not solve it:

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

When knitting a regular RMarkdown document to html with the header shown below, all the three examples in the above link are rendered as paged tables and are all displayed correctly inline in RStudio (inline, the data.frame and tibble are displayed as in the console and the grouped tibble is displayed as a paged table).

---
title: "R Notebook"
output:
  html_document:
    df_print: paged
---

This appears to be present in both the most recent version of the rmarkdown package from CRAN and also the dev version from GitHub.

More people have been mentioning the issue in the RStudio Community forums:
https://community.rstudio.com/t/issues-with-r-markdown-notebook-in-r-3-5-0-not-displaying-tibble-df-nicely-inline/7651

https://community.rstudio.com/t/kable-results-not-displaying-inline/7828/4

I don't know if this is mostly an issue related to rmarkdown or knitr, so feel free to redirect me and close this issue if better suited elsewhere.

@rich-iannone
Copy link
Member

@yihui I can repro this using R 3.5.0 and rmarkdown 1.9.9+. With this document:

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

```{r packages, message=FALSE, warning=FALSE, include=FALSE}
library(dplyr)
```

```{r mtcars_paged}
mtcars[1:3, ]
```

```{r mtcars_grouped_nonpaged, paged.print=FALSE}
mtcars[1:3, ] %>% group_by(cyl)
```

the code chunk with paged printing is fine. The last code chunk (with grouping) yields a warning:

Warning in rm(list = "print.knitr_kable", envir = as.environment("tools:rstudio"),  :
  object 'print.knitr_kable' not found

When previewing the notebook as HTML, the output for the last chunk is additionally garbled:

�[38;5;246m# A tibble: 3 x 11�[39m
�[38;5;246m# Groups:   cyl [2]�[39m
    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
�[38;5;250m*�[39m �[3m�[38;5;246m<dbl>�[39m�[23m �[3m�[38;5;246m<dbl>�[39m�[23m �[3m�[38;5;246m<dbl>�[39m�[23m �[3m�[38;5;246m<dbl>�[39m�[23m �[3m�[38;5;246m<dbl>�[39m�[23m �[3m�[38;5;246m<dbl>�[39m�[23m �[3m�[38;5;246m<dbl>�[39m�[23m �[3m�[38;5;246m<dbl>�[39m�[23m �[3m�[38;5;246m<dbl>�[39m�[23m �[3m�[38;5;246m<dbl>�[39m�[23m �[3m�[38;5;246m<dbl>�[39m�[23m
�[38;5;250m1�[39m  21       6   160   110  3.9   2.62  16.5     0     1     4     4
�[38;5;250m2�[39m  21       6   160   110  3.9   2.88  17.0     0     1     4     4
�[38;5;250m3�[39m  22.8     4   108    93  3.85  2.32  18.6     1     1     4     1

This has to to with the print.knitr_kable method, so, does this problem originate in knitr?

@rich-iannone rich-iannone self-assigned this May 2, 2018
@rich-iannone rich-iannone added bug an unexpected problem or unintended behavior Difficulty: Intermediate labels May 2, 2018
@kevinushey
Copy link
Contributor

This might be a bad interaction between R 3.5 and RStudio -- I can't reproduce with R 3.4 and identical versions of rmarkdown. I'll update here if we learn more and determine that the fix needs to come in on the IDE side.

@kevinushey
Copy link
Contributor

Something appears to be going awry with S3 dispatch in the Notebook context. One can see the issue by attempting to debug the print call in a Notebook chunk, using R 3.5.0 with RStudio v1.2:

```{r}
debugonce(print)
as_tibble(mtcars)
```

When running this, I see:

> debugonce(print)
> as_tibble(mtcars)
debugging in: function (x, ...) 
UseMethod("print")(x)
debug: UseMethod("print")
Browse[2]> class(x)
[1] "tbl_df"     "tbl"        "data.frame"
Browse[2]> print.tbl_df
function (x, ...) 
{
    o <- overrideMap(x, options)
    if (!is.null(o)) {
        overridePrint(o$x, o$options, o$className, o$nRow, o$nCol)
    }
}
<bytecode: 0x7fad5be20ec0>
<environment: 0x7fad44a37498>

So it appears like the S3 override we've registered should be in scope for this print call. However, this is not the case -- the S3 method registered in tibble is called instead.

Browse[2]> s
debugging in: print.tbl_df(x)
debug: {
    cat_line(format(x, ..., n = n, width = width, n_extra = n_extra))
    invisible(x)
}

The NEWS file of R 3.5.0 has this:

  • S3 method lookup now searches the namespace registry after the top level environment of the calling environment.

So it seems like R has explicitly switched up the mechanism used for S3 dispatch, and this has broken the way we override the S3 methods when injecting our own printers. We'll have to think about how to accommodate this change in behavior.

@kevinushey
Copy link
Contributor

I've re-filed this over at rstudio/rstudio#2748.

@yihui
Copy link
Member

yihui commented May 17, 2018

@agranholm Could you test the current latest version of RStudio (1.1.453)?

@agranholm
Copy link
Author

I have now tested with R v. 3.5.0, RStudio v. 1.1.453, all packages including RMarkdown and knitr updated to the latest available on CRAN on macOS.

The following works perfect - all are rendered as paged tables inline and in the html files.

---
title: "R Notebook"
output: html_notebook
---
mtcars
library(tibble)
as_tibble(mtcars)
library(dplyr)
mtcars %>% group_by(cyl)

Thanks for the heads up and for the attention to this issue!

@yihui yihui closed this as completed May 17, 2018
@rbotafogo
Copy link

I'm using knitr in GraalVM. It works fine, but when printing tibbles I see the same error as reported here. Can you tell me what was the fix so I can try to replicate this in GraalVM?

@github-actions
Copy link

github-actions bot commented Nov 3, 2020

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

5 participants