text output

Publish date: Apr 25, 2019
Tags: minimal example

This is render from 007-text-output.Rmd

Testing text output

See if chunk options like tidy, prompt and echo, etc work as expected.

A normal chunk

1+1
## [1] 2
for (i in 1:10) {
# nothing before 10
if(i>=10)print(i)
}
## [1] 10
# two blank lines below


dnorm(0)
## [1] 0.3989423

Do not evaluate

1+1
for (i in 1:10) {
# nothing before 10
if(i>=10)print(i)
}
# two blank lines below


dnorm(0)

Add prompts

> 1+1
## [1] 2
> for (i in 1:10) {
+ # nothing before 10
+ if(i>=10)print(i)
+ }
## [1] 10
> # two blank lines below
> 
> 
> dnorm(0)
## [1] 0.3989423

No evaluate or tidy

1+1
for (i in 1:10) {
# nothing before 10
if(i>=10)print(i)
}
# two blank lines below


dnorm(0)

Do not tidy

1+1
## [1] 2
for (i in 1:10) {
# nothing before 10
if(i>=10)print(i)
}
## [1] 10
# two blank lines below


dnorm(0)
## [1] 0.3989423

Do not echo

## [1] 2
## [1] 10
## [1] 0.3989423

Do not comment out results

1+1
[1] 2
for (i in 1:10) {
# nothing before 10
if(i>=10)print(i)
}
[1] 10
# two blank lines below


dnorm(0)
[1] 0.3989423

Do not echo the 2nd expression

1+1
## [1] 2
## [1] 10
# two blank lines below


dnorm(0)
## [1] 0.3989423

Do not evaluate, echo the 2nd expression

for (i in 1:10) {

Only evaluate the first two expressions

1+1
## [1] 2
for (i in 1:10) {
# nothing before 10
if(i>=10)print(i)
}
## [1] 10
## # two blank lines below
## 
## 
## dnorm(0)

Add prompts but no tidy

> 1+1
## [1] 2
> for (i in 1:10) {
+ # nothing before 10
+ if(i>=10)print(i)
+ }
## [1] 10
> # two blank lines below
> 
> 
> dnorm(0)
## [1] 0.3989423

Prompts, no evaluate or tidy

> 1+1
> for (i in 1:10) {
+ # nothing before 10
+ if(i>=10)print(i)
+ }
> # two blank lines below
> 
> 
> dnorm(0)

Change prompts

options(prompt='R> ', continue='+  ')
R> 1+1
## [1] 2
R> for (i in 1:10) {
+  # nothing before 10
+  if(i>=10)print(i)
+  }
## [1] 10
R> # two blank lines below
R> 
R> 
R> dnorm(0)
## [1] 0.3989423

Backslashes

{
# can you deal with \code{foo} or \n, \a?
gsub('\\.', '\\\\', 'a.b.c') # \link{bar}
}
## [1] "a\\b\\c"
cat('a\tb\nc')
## a	b
## c

Other formatR options

We can set formatR options globally:

options(formatR.blank = FALSE)
1+1
for (i in 1:10) {
# nothing before 10
if(i>=10)print(i)
}
# two blank lines below


dnorm(0)

Or locally in one chunk via tidy.opts. Do not keep comments:

1+1
for (i in 1:10) {
# nothing before 10
if(i>=10)print(i)
}
# two blank lines below


dnorm(0)

Move left braces to the next line:

for (i in 1:10) {
# nothing before 10
if(i>=10)print(i)
}
## [1] 10

Indent by 2 spaces:

1+1
for (i in 1:10) {
# nothing before 10
if(i>=10)print(i)
}
# two blank lines below


dnorm(0)

See http://yihui.name/formatR for details.

Empty chunks

Messages

Do not include messages:

1+1
## [1] 2
message('helloooo!')

No warnings:

1:2+1:3
## [1] 2 4 4
warning('no no no')

Select warnings using numeric indices:

1:2+1:3
## [1] 2 4 4
warning('no no no')
## Warning: no no no

Invalid indices will select nothing:

1:2+1:3
## [1] 2 4 4
warning('no no no')

The results option

Do not show text results:

1+1
for (i in 1:10) {
# nothing before 10
if(i>=10)print(i)
}
# two blank lines below


dnorm(0)

Flush all results to the end of a chunk:

1+1
for (i in 1:10) {
# nothing before 10
if(i>=10)print(i)
}
# two blank lines below


dnorm(0)
## [1] 2
## [1] 10
## [1] 0.3989423

Output as is:

cat('_Markdown_,', 'oh yeah, **Markdown**')

Markdown, oh yeah, Markdown