knitr is using custom print method to do its magic. This means when a data.frame
object is a result of a chunk knit_print.data.frame
will be called.
The interactive table you see is a [paged_table()](https://pkgs.rstudio.com/rmarkdown/reference/paged_table.html)
and the function is used by default on data.frame through the df_print
argument (3.1 HTML document | R Markdown: The Definitive Guide)
By using print(df)
you are preventing the knit_print()
to be used, and thus you don't get all the processing that knitr would do usually.
Why do you think this is good practice to print the object ? In R Markdown document, this is definitely not a good practice.
- Open one copy in RStudio. Run it, and click Preview. Open the resulting file in browser.
This is indeed inconsistent but this is a IDE only feature. I believe RStudio IDE is overriding the print.data.frame
with the function used for paged tables. This is for better experience as notebook mode in the IDE. As html_notebook
is a special format tied to this, anything run interactively is cached and then reinject in the .nb.html
when doing Preview . When you render, it will execute all the chunk leaving out IDE only feature.
You can recreate it by overriding print.data.frame
method if you really need to
---
title: "R Notebook"
output: html_notebook
---
```{r}
print.data.frame <- rmarkdown:::print.paged_df
```
```{r}
df <- data.frame(One = 1:26, Two = letters)
df
```
```{r}
print(df)
```
Though I would not do print(df)
in the first place. If you print explicitely all the object, you will get some other side effect with other objects in R.