-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial_Rmd.Rmd
114 lines (85 loc) · 3.4 KB
/
tutorial_Rmd.Rmd
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
---
output:
pdf_document: default
html_document: default
word_document: default
---
```{r reprex-options, include = FALSE}
options(
keep.source = TRUE,
rlang_backtrace_on_error_report = "full",
crayon.enabled = FALSE,
reprex.current_venue = "gh"
)
```
```{r, results = 'asis', echo = FALSE, include = file.exists('.Rprofile'), eval = file.exists('.Rprofile')}
cat(sprintf("*Local `.Rprofile` detected at `%s`*", normalizePath(".Rprofile")))
```
---
output:
reprex::reprex_document:
venue: "gh"
advertise: FALSE
session_info: TRUE
style: TRUE
comment: "#;-)"
tidyverse_quiet: FALSE
std_out_err: TRUE
knit: reprex::reprex_render
---
This template demonstrates many of the bells and whistles of the `reprex::reprex_document()` output format. The YAML sets many options to non-default values, such as using `#;-)` as the comment in front of output.
## Code style
Since `style` is `TRUE`, this difficult-to-read code (look at the `.Rmd` source file) will be restyled according to the Tidyverse style guide when it's rendered. Whitespace rationing is not in effect!
```{r}
x=1;y=2;z=x+y;z
```
## Quiet tidyverse
The tidyverse meta-package is quite chatty at startup, which can be very useful in exploratory, interactive work. It is often less useful in a reprex, so by default, we suppress this.
However, when `tidyverse_quiet` is `FALSE`, the rendered result will include a tidyverse startup message about package versions and function masking.
```{r, eval = requireNamespace("tidyverse", quietly = TRUE)}
library(tidyverse)
```
## Chunks in languages other than R
Remember: knitr supports many other languages than R, so you can reprex bits of code in Python, Ruby, Julia, C++, SQL, and more. Note that, in many cases, this still requires that you have the relevant external interpreter installed.
Let's try Python!
```{python, eval = Sys.which("python") != "", python.reticulate = requireNamespace("reticulate", quietly = TRUE)}
x = 'hello, python world!'
print(x.split(' '))
```
And bash!
```{bash, eval = Sys.which("bash") != ""}
echo "Hello Bash!";
pwd;
ls | head;
```
Write a function in C++, use Rcpp to wrap it and ...
```{Rcpp, eval = requireNamespace("Rcpp", quietly = TRUE)}
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
return x * 2;
}
```
then immediately call your C++ function from R!
```{r, eval = requireNamespace("Rcpp", quietly = TRUE)}
timesTwo(1:4)
```
## Standard output and error
Some output that you see in an interactive session is not actually captured by rmarkdown, when that same code is executed in the context of an `.Rmd` document. When `std_out_err` is `TRUE`, `reprex::reprex_render()` uses a feature of `callr:r()` to capture such output and then injects it into the rendered result.
Look for this output in a special section of the rendered document (and notice that it does not appear right here).
```{r}
system2("echo", args = "Output that would normally be lost")
```
## Session info
Because `session_info` is `TRUE`, the rendered result includes session info, even though no such code is included here in the source document.
<details style="margin-bottom:10px;">
<summary>Standard output and standard error</summary>
`/Users/enricopirani/Statistica/tutorial_Rmd_std_out_err.txt`
</details>
<details style="margin-bottom:10px;">
<summary>Session info</summary>
```{r }
sessioninfo::session_info()
```
</details>