'Which knitr option controls typeset vs. code-like output from knitr (LaTeX)

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



Solution 1:[1]

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

Solution 2:[2]

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).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 mk9y