When knitting R-Markdown in RStudio, I would like to all console outputs in one chunk to be placed together in one code block. How can this be done?
As a workaround, I write two code blocks of the same code and set eval=FALSE
on the first block and echo=FALSE
on the second.
```{r Vector Demo 2, eval=FALSE}
# examine the class and structure of vectors
class(nums)
class(char)
str(nums)
str(char)
```
```{r Vector Demo 2b, echo=FALSE}
# examine the class and structure of vectors
class(nums)
class(char)
str(nums)
str(char)
```
This however produces the following output:
# examine the class and structure of vectors class(nums) class(char) str(nums) str(char)
## [1] "numeric"
## [1] "character"
## num [1:5] 1 2 3 4 5
## chr [1:3] "A" "B" "C"
What I would like is to have the output of the second chunk (i.e. Vector Demo 2b) to be placed together in one code block just like the first chunk (i.e. Vector Demo 2).
This is a sample output of how I would prefer to have my result:
# examine the class and structure of vectors class(nums) class(char) str(nums) str(char)
## [1] "numeric"
## [1] "character"
## num [1:5] 1 2 3 4 5
## chr [1:3] "A" "B" "C"
Note to bounty hunters:
Better still, I would be grateful for a way to have one code chunk first print the input code, and then print the output code. That way I could avoid duplication and possible inconsistencies that could come with it.
Here is a solution to your problem. There are two things to do:
## Test
```{r echo = F, cache = F}
knitr::knit_hooks$set(document = function(x){
gsub("```\n*```r*\n*", "", x)
})
```
```{r VectoDemo, results = 'hold'}
nums = 1:5
char = LETTERS[1:5]
# examine the class and structure of vectors
class(nums)
class(char)
str(nums)
str(char)
```
I have done two things here
results = 'hold'
to "hold" printing of output after source is printed.With knitr >= 1.5.23
You can get quite a bit of variety from the built-in knitr options.
For instance... get the same results as Ramnath's hook using collapse=TRUE, results="hold"
(no global document hook needed).
If you really want distinct source/output parts as shown in the question then you are already on the right path. Just make use of chunk reuse
to get the output combo without having to repeat yourself...
```{r}
nums <- 1:5
char <- LETTERS[1:3]
```
```{r "Vector Demo", eval=FALSE}
# examine the class and structure of vectors
class(nums)
class(char)
str(nums)
str(char)
```
```{r "Vector Demo", echo=FALSE}
```
The last chunk is your basic chunk reuse
pattern.
You can see a variety of the basic combinations here.
Since you aren't making a knitr tutorial ignore the source
and use the rendered
examples.
@Ramnath's solution appears a bit simpler than this one. His is likely better in many (most?) circumstances, but this alternate solution might be good in others:
Test.
```{r echo=F,cache=F}
knitr::knit_hooks$set(document=function(x) {
paste(rapply(strsplit(x, '\n'), function(y) Filter(function(z) !grepl('# HIDEME',z),y)), collapse='\n')
})
```
```{r Vector Demo 1, results='hold', tidy=FALSE}
nums = 1:5
chars = LETTERS[1:5]
# examine the class and structure of vectors
{ # HIDEME
print(class(nums))
print(class(chars))
str(nums)
str(chars)
} # HIDEME
```
Notes:
Use of the brackets ({
and }
) around the code keeps the output together. However, commands that return things and don't print things will be silent (unless last), ergo my print
addition to those lines. This may or may not be a factor depending on your actual commands.
In my install, for some reason, tidy
defaults to TRUE
which was shifting my first # HIDEME
comment to before the left bracket (and I have since edited to code to reflect the definition of nums
and chars
). Odd, but likely a side-effect of source tidying. This is why I force tidy=FALSE
. Since this might affect how you present your code, using it as a per-block option at least limits the pretty-printing problem.
The # HIDEME
is really just "comment-character plus some obscure string" for easy grep
ing.
The knit_hook
I added is not "simple," but I find it less likely to have side-effects on other chunks within a document. This could likely be done with more specificity (I know @Ramnath has worked on other knitr
problems with Yihui, so there could be more "correct" ways to do this with more specificity.) (I tried and failed to do this as an "output" hook instead of "document" hook. Homework.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With