Different print method dispatched when running in console vs R markdown cell?

Viewed 39

When I run the following code in the console vs in the R markdown document, I see that the different methods are being dispatched. Is this a bug, or does the way that the code is run affect method dispatch?

library(sloop)
library(tidyverse)

df <- tibble(x=rnorm(5),
               y=rnorm(5))

sloop::s3_dispatch(print(df))

Running the code in the R Markdown code cell (in RStudio) Running the code in RStudio code cell

Running the code in the console Running the code in console

1 Answers

RStudio is overriding print.tbl_df in the notebook. Code here.

In an R Notebook:

getAnywhere(print.tbl_df)
#> 2 differing objects matching ‘print.tbl_df’ were found
#> in the following places
#>   registered S3 method for print
#>   namespace:tibble
#> Use [] to view one of them

getAnywhere(print.tbl_df)[1]
#> function (x, ...) 
#> {
#>     o <- overrideMap(x, options)
#>     if (!is.null(o)) {
#>         overridePrint(o$x, o$options, o$className, o$nRow, o$nCol)
#>     }
#> }
#> <bytecode: 0x7fb2bdf8fd48>
#> <environment: 0x7fb2bd9567e8>
#> attr(,".rs.S3Override")
#> [1] TRUE

In a normal R console:

getAnywhere(print.tbl_df)
#> A single object matching ‘print.tbl_df’ was found
#> It was found in the following places
#>   namespace:tibble
#> with value
#> 
#> function (x, ..., n = NULL, width = NULL, n_extra = NULL) 
#> {
#>     NextMethod()
#> }
#> <bytecode: 0x7fb2b77d7040>
#> <environment: namespace:tibble>
Related