Skip to content

Commit 288faab

Browse files
authored
Enable the IDE cell evaluation with webr-r tag (#12)
* Enable the IDE cell evaluation with webr-r tag * Bump version
1 parent 2f73a78 commit 288faab

File tree

5 files changed

+68
-45
lines changed

5 files changed

+68
-45
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.luarc.json

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ filters:
4545

4646
This is a webr-enabled code cell in a Quarto HTML document.
4747

48-
```{webr}
48+
```{webr-r}
4949
fit = lm(mpg ~ am, data = mtcars)
5050
summary(fit)
5151
```
@@ -148,8 +148,8 @@ For additional justificaiton on why the headers are required, please see [webR's
148148
149149
### Engine Registration
150150
151-
If using the `knitr` engine instead of the `jupyter` engine, there is a known warning
152-
that will appear in the render processing output of:
151+
If using the `knitr` engine instead of the `jupyter` engine and you are using the original tag of `{webr}` instead of `{webr-r}`,
152+
there is a known warning that will appear in the render processing output of:
153153
154154
```r
155155
Warning message:
@@ -160,11 +160,12 @@ In get_engine(options$engine) :
160160
This warning does not prevent or impact the ability of the `webr` filter to function.
161161
Though, we would like to address it at some point since it is not aesthetically pleasing.
162162

163-
164163
## Acknowledgements
165164

166165
We appreciate the early testing feedback from [Eli E. Holmes](https://eeholmes.github.io/) and [Bob Rudis](https://rud.is/).
167166

167+
We also appreciate the Quarto team assisting with [setting up a new code cell type](https://github.com/quarto-dev/quarto-cli/discussions/4761#discussioncomment-5336636).
168+
168169
This repository builds ontop of the initial proof of concept for a standalone Quarto HTML document in:
169170

170171
<https://github.com/coatless-r-n-d/webR-quarto-demos>

_extensions/webr/_extension.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: webr
22
title: Embedded webr code cells
33
author: James Joseph Balamuta
4-
version: 0.0.3
4+
version: 0.0.4
55
quarto-required: ">=1.2.198"
66
contributes:
77
filters:

_extensions/webr/webr.lua

+43-28
Original file line numberDiff line numberDiff line change
@@ -92,39 +92,54 @@ return {
9292
-- Should display the following elements:
9393
-- https://pandoc.org/lua-filters.html#type-codeblock
9494

95-
-- Ensure that the {webr} tag is present and the document type is HTML
95+
-- Verify the element has attributes and the document type is HTML
9696
-- not sure if this will work with an epub (may need html:js)
97-
-- Right now, we're using `{webr}` if engine is jupyter and `webr` if engine is `knitr` since the later dislikes custom engines
98-
if el.attr and (el.attr.classes:includes("{webr}") or el.attr.classes:includes("webr")) and quarto.doc.is_format("html") then
99-
100-
-- Make sure we've initialized the code block
101-
ensureWebRSetup()
97+
if el.attr and quarto.doc.is_format("html") then
10298

103-
-- Modify the counter variable each time this is run to create
104-
-- unique code cells
105-
counter = counter + 1
106-
107-
-- 7 is the default height and width for knitr. But, that doesn't translate to pixels.
108-
-- So, we have 504 and 360 respectively.
109-
-- Should we check the attributes for this value? Seems odd.
110-
-- https://yihui.org/knitr/options/
111-
local substitutions = {
112-
["WEBRCOUNTER"] = counter,
113-
["WIDTH"] = 504,
114-
["HEIGHT"] = 360,
115-
["WEBRCODE"] = escapeControlSequences(el.text)
116-
}
117-
118-
-- Make sure we perform a copy
119-
local copied_editor_template = editor_template
99+
-- Check to see if any form of the {webr} tag is present
120100

121-
-- Make the necessary substitutions
122-
local webr_enabled_code_cell = substitute_in_file(copied_editor_template, substitutions)
101+
-- Look for the original compute cell type `{webr}`
102+
-- If the compute engine is:
103+
-- - jupyter: this appears as `{webr}`
104+
-- - knitr: this appears as `webr`
105+
-- since the later dislikes custom engines
106+
local originalEngine = el.attr.classes:includes("{webr}") or el.attr.classes:includes("webr")
123107

124-
-- Return the modified HTML template as a raw cell
125-
return pandoc.RawInline('html', webr_enabled_code_cell)
108+
-- Check for the new engine syntax that allows for the cell to be
109+
-- evaluated in VS Code or RStudio editor views, c.f.
110+
-- https://github.com/quarto-dev/quarto-cli/discussions/4761#discussioncomment-5336636
111+
local newEngine = el.attr.classes:includes("{webr-r}")
112+
113+
if (originalEngine or newEngine) then
114+
115+
-- Make sure we've initialized the code block
116+
ensureWebRSetup()
117+
118+
-- Modify the counter variable each time this is run to create
119+
-- unique code cells
120+
counter = counter + 1
121+
122+
-- 7 is the default height and width for knitr. But, that doesn't translate to pixels.
123+
-- So, we have 504 and 360 respectively.
124+
-- Should we check the attributes for this value? Seems odd.
125+
-- https://yihui.org/knitr/options/
126+
local substitutions = {
127+
["WEBRCOUNTER"] = counter,
128+
["WIDTH"] = 504,
129+
["HEIGHT"] = 360,
130+
["WEBRCODE"] = escapeControlSequences(el.text)
131+
}
132+
133+
-- Make sure we perform a copy
134+
local copied_editor_template = editor_template
135+
136+
-- Make the necessary substitutions
137+
local webr_enabled_code_cell = substitute_in_file(copied_editor_template, substitutions)
138+
139+
-- Return the modified HTML template as a raw cell
140+
return pandoc.RawInline('html', webr_enabled_code_cell)
141+
end
126142
end
127-
128143
-- Allow for a pass through in other languages
129144
return el
130145
end

webr-demo.qmd

+18-12
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@ filters:
88

99
## Demo
1010

11-
This is a webr-enabled code cell in a Quarto HTML document.
11+
WebR-enabled code cell are established by using `{webr-r}` in a Quarto HTML document.
1212

13-
```{webr}
13+
```{webr-r}
1414
1 + 1
1515
```
1616

1717
## Sample cases
1818

1919
### Fit a linear regression model
2020

21-
```{webr}
21+
```{webr-r}
2222
fit = lm(mpg ~ am, data = mtcars)
2323
summary(fit)
2424
```
2525

2626
### Create a graph with base R
2727

28-
```{webr}
28+
```{webr-r}
2929
plot(pressure)
3030
```
3131

@@ -36,7 +36,7 @@ plot(pressure)
3636

3737
You can view what packages are available for webR by either executing the following R code (either with WebR or just R):
3838

39-
```{webr}
39+
```{webr-r}
4040
available.packages(
4141
repos="https://repo.r-wasm.org/",
4242
type="source")[,c("Package", "Version")]
@@ -50,15 +50,15 @@ Or, by navigating to the WebR repository:
5050

5151
Installing `ggplot2` may take at least 2 minutes to run.
5252

53-
```{webr}
53+
```{webr-r}
5454
webr::install("ggplot2")
5555
```
5656

5757
#### Using a Package
5858

5959
Once `ggplot2` is loaded, then use the package as normal.
6060

61-
```{webr}
61+
```{webr-r}
6262
library(ggplot2)
6363
6464
p = ggplot(mpg, aes(class, hwy))
@@ -67,33 +67,39 @@ p + geom_boxplot()
6767

6868
### Define variables and re-use them in later cells
6969

70-
```{webr}
70+
```{webr-r}
7171
name = "James"
7272
age = 42
7373
```
7474

7575

76-
```{webr}
76+
```{webr-r}
7777
message(name, " is ", age, " years old!")
7878
```
7979

8080

8181
### Escape characters in a string
8282

83-
```{webr}
83+
```{webr-r}
8484
seven_seas = "Ahoy, matey!\nLet's set sail for adventure!\n"
8585
seven_seas
8686
```
8787

8888
### Anonymous function definition
8989

90-
```{webr}
90+
```{webr-r}
9191
add_one <- \(x) x + 1
9292
add_one(2)
9393
```
9494

9595
### Empty code cell
9696

97-
```{webr}
97+
```{webr-r}
9898
99+
```
100+
101+
### Pre-rendered code cell
102+
103+
```{r}
104+
message("Hello!")
99105
```

0 commit comments

Comments
 (0)