Home > Software design >  Which knitr option controls typeset vs. code-like output from knitr (LaTeX)
Which knitr option controls typeset vs. code-like output from knitr (LaTeX)

Time:02-22

What controls the difference between these two outputs?

To produce the desired R-like output in the first image, I had to add %>% print()

desired LaTeX output from knitr

The following seems to have become the default format (presumably due to my inadvertent setting of a knitr option):

undesirable LaTeX output from knitr

CodePudding user response:

There's a generic function called knit_print with methods for various classes; run methods("knit_print") to see what they are with the packages you have loaded. If your final object (which probably has classes "tbl_df", "tbl", and "data.frame") has a class for which there's a knit_print method, that's what it will use. Running example(knit_print) will create a data.frame method.

From the other answer, it appears that it was the printr package which added a knit_print.data.frame method. To work around this, you can use the render chunk option, e.g.

```{r}
library(printr)
head(mtcars)
```

```{r render = print}
head(mtcars)
```

which yields this output:

screenshot

CodePudding user response:

After reading vignette('knit_print', package = 'knitr') I discovered that in my case the cause was that I had blindly added library(printr) in the preamble of my Rnw file, which produced formatted output (as in my second inserted image). When I commented out that line, the output was as you would see it in interacting with R in the console (as in the first image).

  • Related