Replacing the print function in knitr chunk evaluation
You need to selectively overrule the print method for the object class you want to print with pander. Do methods(pander)
to figure out what is available. Some methods are not exported, so you will have to use :::
to access them. Here is a simple example.
TEST
====
```{r cache = F, comment = NA}
print.lm <- pander:::pander.lm
lm(mpg ~ wt, data = mtcars)
```
Output
TEST
====
```r
print.lm <- pander:::pander.lm
lm(mpg ~ wt, data = mtcars)
```
```
--------------------------------------------------------------
Estimate Std. Error t value Pr(>|t|)
----------------- ---------- ------------ --------- ----------
**(Intercept)** 37.29 1.878 19.86 8.242e-19
**wt** -5.344 0.5591 -9.559 1.294e-10
--------------------------------------------------------------
Table: Fitting linear model: mpg ~ wt
```
Display Block of R Code in Knitr With Evaluation Turned Off
Is this what you want to do, or have I misunderstood? The eval = FALSE
is in one code chunk and the second chunk still plots.
---
title: "A Test Knit"
output: html_document
---
## Show code but don't run
```{r, eval = FALSE}
summary(cars)
```
## Run and render plot
```{r}
plot(pressure)
```
How to show code but hide output in RMarkdown?
As @ J_F answered in the comments, using {r echo = T, results = 'hide'}
.
I wanted to expand on their answer - there are great resources you can access to determine all possible options for your chunk and output display - I keep a printed copy at my desk!
You can find them either on the RStudio Website under Cheatsheets (look for the R Markdown cheatsheet and R Markdown Reference Guide) or, in RStudio, navigate to the "Help" tab, choose "Cheatsheets", and look for the same documents there.
Finally to set default chunk options, you can run (in your first chunk) something like the following code if you want most chunks to have the same behavior:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = T,
results = "hide")
```
Later, you can modify the behavior of individual chunks like this, which will replace the default value for just the results option.
```{r analysis, results="markup"}
# code here
```
knitr::kable does not pretty print after running an R chunk
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?
How can I remove the prefix (index indicator) [1] in knitr output?
Nothing is impossible. Take a look at what can be done with knitr hooks.
Have fun!
Rmarkdown script gist
# A Prefix nulling hook.
# Make sure to keep the default for normal processing.
default_output_hook <- knitr::knit_hooks$get("output")
# Output hooks handle normal R console output.
knitr::knit_hooks$set( output = function(x, options) {
comment <- knitr::opts_current$get("comment")
if( is.na(comment) ) comment <- ""
can_null <- grepl( paste0( comment, "\\s*\\[\\d?\\]" ),
x, perl = TRUE)
do_null <- isTRUE( knitr::opts_current$get("null_prefix") )
if( can_null && do_null ) {
# By default R print output aligns at the right brace.
align_index <- regexpr( "\\]", x )[1] - 1
# Two cases: start or newline
re <- paste0( "^.{", align_index, "}\\]")
rep <- comment
x <- gsub( re, rep, x )
re <- paste0( "\\\n.{", align_index, "}\\]")
rep <- paste0( "\n", comment )
x <- gsub( re, rep, x )
}
default_output_hook( x, options )
})
knitr::opts_template$set("kill_prefix"=list(comment=NA, null_prefix=TRUE))
Normal:
```{r}
print( 1:50 )
```
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## [47] 47 48 49 50
Null prefix
```{r, null_prefix=TRUE}
print( 1:50 )
```
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## 47 48 49 50
Set as default (and remove comment string):
```{r}
knitr::opts_chunk$set(opts.label="kill_prefix")
```
```{r}
print( 1:50 )
```
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
47 48 49 50
Ensure we aren't killing strings with [:digit:] patterns.
```{r}
print( paste0( paste0("[", 1:50), "]" ),quote = FALSE)
```
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
[15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25] [26] [27] [28]
[29] [30] [31] [32] [33] [34] [35] [36] [37] [38] [39] [40] [41] [42]
[43] [44] [45] [46] [47] [48] [49] [50]
Related Topics
Add a Page Refresh Button by Using R Shiny
Display Correlation Tables as Descending List
Efficiently Locf by Groups in a Single R Data.Table
Multiple Lines Each Based on a Different Dataframe in Ggplot2 - Automatic Coloring and Legend
How to Export an Excel Sheet Range to a Picture, from Within R
Creating a Heat Map from (X,Y) Corrdinates in R
How to Name the List of the Group_Split Output in Dplyr
Replace Characters in Column Names Gsub
Why Is Foreach() %Do% Sometimes Slower Than For
R: How to Display Clustered Matrix Heatmap (Similar Color Patterns Are Grouped)
Replacing the "Print" Function in Knitr Chunk Evaluation
Filter Each Column of a Data.Frame Based on a Specific Value
Copy Upper Triangle to Lower Triangle for Several Matrices in a List
Find Overlapping Dates for Each Id and Create a New Row for the Overlap
Matrix Expression Causes Error "Requires Numeric/Complex Matrix/Vector Arguments"
How to Create Design Matrix in R