Reputation: 331
I noticed this weird behavior of kable
- the pretty printing goes away after running a chunk.
> knitr::kable(head(iris, 3))
| Sepal.Length| Sepal.Width| Petal.Length| Petal.Width|Species |
|------------:|-----------:|------------:|-----------:|:-------|
| 5.1| 3.5| 1.4| 0.2|setosa |
| 4.9| 3.0| 1.4| 0.2|setosa |
| 4.7| 3.2| 1.3| 0.2|setosa |
Now do File > New File > R Notebook. This creates a demo notebook, with a single chunk containing plot(cars)
. Execute that chunk within the notebook
> plot(cars)
Then print the table again. This time the output looks different. Why?
> knitr::kable(head(iris, 3))
[1] "| Sepal.Length| Sepal.Width| Petal.Length| Petal.Width|Species |"
[2] "|------------:|-----------:|------------:|-----------:|:-------|"
[3] "| 5.1| 3.5| 1.4| 0.2|setosa |"
[4] "| 4.9| 3.0| 1.4| 0.2|setosa |"
[5] "| 4.7| 3.2| 1.3| 0.2|setosa |"
attr(,"format")
[1] "markdown"
attr(,"class")
[1] "knit_asis"
attr(,"knit_cacheable")
[1] NA
> packageVersion("knitr")
[1] ‘1.17’
> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X El Capitan 10.11.6
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.3.3 highr_0.6 knitr_1.17
> RStudio.Version()
$citation
To cite RStudio in publications use:
RStudio Team (2016). RStudio: Integrated Development for R. RStudio,
Inc., Boston, MA URL http://www.rstudio.com/.
A BibTeX entry for LaTeX users is
@Manual{,
title = {RStudio: Integrated Development Environment for R},
author = {{RStudio Team}},
organization = {RStudio, Inc.},
address = {Boston, MA},
year = {2016},
url = {http://www.rstudio.com/},
}
$mode
[1] "desktop"
$version
[1] ‘1.0.153’
Upvotes: 1
Views: 1405
Reputation: 44808
When you run that chunk, the tools:rstudio
environment on the search list is changed to add a function called print.knitr_kable
which is different from the internal one in the knitr
package.
Here's what I see before:
> ls("tools:rstudio")
[1] "debugSource" "knit_with_parameters"
[3] "registerShinyDebugHook" "RStudio.Version"
[5] "rstudioDiagnosticsReport" "RStudioGD"
[7] "source.with.encoding"
and here's what I see after:
> ls("tools:rstudio")
[1] "debugSource" "dplyr_tibble_print_original"
[3] "knit_with_parameters" "print.knitr_kable"
[5] "registerShinyDebugHook" "RStudio.Version"
[7] "rstudioDiagnosticsReport" "RStudioGD"
[9] "source.with.encoding"
For whatever reason, the tools:rstudio
function is executed instead of the original one. I can get the original behaviour back by running detach("tools:rstudio")
, but that messes up Rstudio in several ways, so I don't recommend it. A less extreme way is to say
e <- as.environment("tools:rstudio")
e$print.knitr_kable <- knitr:::print.knitr_kable
but that doesn't last: RStudio apparently fixes it whenever you run a chunk in the notebook. You could also do an explicit call every time you want to print, e.g.
knitr:::print.knitr_kable(knitr::kable(head(iris, 3)))
Probably the best solution is for knitr
to change so it does what RStudio wants when run in an RStudio notebook and the pretty printing otherwise, or for RStudio to change to run the knitr
function when it's not in a notebook, but I imagine this isn't a huge priority: if you're running a notebook, why would you care what you see in the console?
Upvotes: 1