File: embed.Rmd

package info (click to toggle)
r-cran-diffobj 0.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,432 kB
  • sloc: ansic: 455; javascript: 96; sh: 32; makefile: 8
file content (181 lines) | stat: -rwxr-xr-x 4,186 bytes parent folder | download | duplicates (4)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
---
title: "Embed Diffs in R Markdown Or Shiny"
author: "Brodie Gaslam"
output:
    rmarkdown::html_vignette:
        toc: true
        css:
          - !expr diffobj::diffobj_css()
          - styles.css
vignette: >
  %\VignetteIndexEntry{Embed Diffs in R Markdown Or Shiny}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}

---

```{r echo=FALSE}
library(diffobj)
```

## Rmarkdown

### Basic Requirements

Any R chunks that produce diffs should include the `results='asis'` option,
e.g.:

````
```{r, comment="", results="asis"}`r ''`
# R code here
```
````

### Embedded CSS

This is what a basic code block should look like:

````
```{r, comment="", results="asis"}`r ''`
cat(                                 # output to screen
  as.character(                      # convert to diff to character vector
    diffPrint(                       # run diff
      1:5, 2:6,
      format="html",                 # specify html output
      style=list(
        html.output="diff.w.style"   # configure html style
      )
) ) )
```
````

Here we use this same code as an actual markdown R code block:

```{r results='asis'}
cat(
  as.character(
    diffPrint(
      1:5, 2:6,
      format="html",
      style=list(html.output="diff.w.style")
) ) )
```

This is an ugly implementation because it produces illegal HTML.  The styles are
directly embedded in the body of the document, outside of the HEAD tags.
Although this is illegal HTML, it seems to work in most browsers.  Another
problem is that every diff you use in your document will inject the same CSS
code over and over.

### External CSS

A better option is to provide the CSS directly by modifying the `output`
portion of the [YAML
header](https://bookdown.org/yihui/rmarkdown/r-package-vignette.html):

```
---
output:
    rmarkdown::html_vignette:
        toc: true
        css: !expr diffobj::diffobj_css()
---
```

In reality you will probably want to specify multiple CSS files, including the
original `rmarkdown` one:

```
---
output:
    rmarkdown::html_vignette:
        toc: true
        css:
          - !expr diffobj::diffobj_css()
          - !expr system.file("rmarkdown", "templates", "html_vignette", "resources", "vignette.css", package = "rmarkdown")
---
```

Once you set this up then you can use:

```{r results='asis'}
cat(
  as.character(
    diffPrint(
      1:5, 2:6,
      format="html",
      style=list(html.output="diff.only")   # notice this changed
) ) )
```

This will omit the CSS, but since we include it via the YAML everything should
work as expected.

### Use Options

Almost all `diffobj` parameters can be specified via options:

```{r eval=FALSE}
options(
  diffobj.format="html",
  diffobj.style=list(html.output="diff.only")
)
```
```{r echo=FALSE}
old.opts <- options(
  diffobj.format="html",
  diffobj.style=list(html.output="diff.only")
)
```
Then you can just run the diff as normal:
```{r results='asis'}
cat(as.character(diffPrint(1:5, 2:6)))
```
```{r echo=FALSE}
options(old.opts)
```

## Shiny

Shiny usage is very similar to `rmarkdown`.  In both cases we want to get
`diffobj` to produce HTML output to embed in our document.  If we are willing to
embed the CSS with each diff, we can use:

```{r, eval=FALSE}
library(shiny)
shinyApp(
  ui=fluidPage(htmlOutput('diffobj_element')),
  server=function(input, output) {
    output$diffobj_element <- renderUI({
      HTML(
        as.character(
          diffPrint(
            1:5, 2:6,
            format="html",
            style=list(html.output="diff.w.style")
) ) )}) } )
```

If we have many diffs, it may be preferable to use options and external
style sheet:

```{r, eval=FALSE}
options(
  diffobj.format="html",
  diffobj.style=list(html.output="diff.only")
)
shinyApp(
  ui=fluidPage(
    includeCSS(diffobj_css()),
    htmlOutput('diffobj_element')
  ),
  server=function(input, output) {
    output$diffobj_element <- renderUI({
      HTML(as.character(diffPrint(1:5, 2:6,)))
}) } )
```

Unlike with our [rmarkdown example](#external-css), this CSS is included
in the body of the HTML document instead of in the header, so it is technically
illegal like in our [embedded css example](#embedded-css).