forked from coatless-r-n-d/webR-quarto-demos
-
Notifications
You must be signed in to change notification settings - Fork 10
/
webr-quarto.qmd
161 lines (116 loc) · 2.88 KB
/
webr-quarto.qmd
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
---
title: "webR with Quarto HTML Standalone Document Proof of Concept"
subtitle: "Experiments with an Interactive Quarto Document using webR v0.1.0"
author: "James J Balamuta; modified by EEH"
repo: https://github.com/RVerse-Tutorials/webR-quarto-demos
engine: knitr
execute:
echo: true
error: true
embed-resources: true
format:
html:
toc: true
code-tools:
source: https://github.com/RVerse-Tutorials/webR-quarto-demos/blob/main/webr-quarto.qmd
include-in-header: webr-setup.html
repo-url: https://github.com/RVerse-Tutorials/webR-quarto-demos
repo-actions: [edit, source]
editor: source
---
{{< include _webr-setup.qmd >}}
# Background
This is not my work. Forked from and go there to see how this works
James J Balamuta: <https://github.com/coatless-r-n-d/webR-quarto-demos>
This was created by JJB to explore how [WebR](https://docs.r-wasm.org/webr/latest/) can be embedded in a Quarto Document for the purposes of teaching _R_.
## Setup
The yaml includes this. Note the `webr-setup.html` and _webr-setup.qmd` files.
```
---
title: "the title"
engine: knitr
execute:
echo: true
error: true
embed-resources: true
format:
html:
toc: true
include-in-header: webr-setup.html
editor: source
---
{{< include _webr-setup.qmd >}}
```
## Calling WebR
Use `{webr}` rather than `{r}` for your code.
### Linear Regression
We'll first start with the WebR team's demo example or the statistician way of
saying, "Hello world!"... Aka linear regression:
```{webr}
fit = lm(mpg ~ am, data=mtcars)
summary(fit)
```
### Retrieving prior objects
Each WebR cell appears to be connected to each other. Thus, we can access the
`fit` outcome:
```{webr}
coef(fit)
```
```{webr}
anova(fit)
```
### Mixing active and non-active _R_ code
```{webr}
# Let's classify
temperature = 60
```
```{webr}
if (temperature > 76) {
print("Hot!")
} else {
print("Cold!")
}
```
### Summarize Data
Glancing at data frames yields:
```{webr}
summary(mtcars)
```
### Errors and Warnings
```{webr}
stop("What happens if an error is present?")
```
```{webr}
warning("You shouldn't be here...")
```
### Base graphics
Graphing with base R
```{webr}
plot(pressure)
```
More advanced base R graphing...
```{webr}
x1 = seq(0, 1, length = 20)
y1 = rep(0, 20)
x2 = rep(0, 20)
y2 = seq(0.75, 0, length = 20)
plot(0, type = "n",
axes = FALSE, ylab = "", xlab = "",
xlim = c(0, 1), ylim = c(0, 0.75), asp = 1,
main = "Straight Lines as a Curve")
segments(x1, y1, x2, y2)
box(col = "grey")
```
### ggplot2 Graphics
There is an extra step of installing packages that are not part of the base.
CLICK RUN AND WAIT LIKE 1 MINUTE. When you see output, it means it is loaded. Be patient.
```{webr}
# Install non-base R packages
webr::install("ggplot2")
```
Now we can use ggplot2 in the qmd.
```{webr}
library("ggplot2")
p = ggplot(mpg, aes(class, hwy))
p + geom_boxplot()
```