Skip to content

Latest commit

 

History

History
541 lines (387 loc) · 4.42 KB

007-text-output.md

File metadata and controls

541 lines (387 loc) · 4.42 KB

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

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

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

Do not echo

## [1] 2
## [1] 10
## [1] 0.3989

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

Do not echo the 2nd expression

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


dnorm(0)
## [1] 0.3989

Do not evaluate, echo the 2nd expression

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

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

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

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) {
    if (i >= 10)
        print(i)
}
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.org/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.3989

Output as is:

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

Markdown, oh yeah, Markdown