Debian Codegraph @

tests/testthat/test-notebook.R

raw
1 context("notebook")
2
3 # expect that the default evaluate hook is restored after the notebook is
4 # rendered and parsed
5 hook_evaluate_get <- function() knitr::knit_hooks$get("evaluate")
6 hook_evaluate <- hook_evaluate_get()
7 expect_default_evaluate_hook <- function() {
8 expect_identical(hook_evaluate_get(), hook_evaluate)
9 }
10
11 test_that("an example R Notebook document can be rendered and parsed", {
12 # check initial status of hook
13 expect_default_evaluate_hook()
14
15 # generate the file
16 path <- test_path("resources/r-notebook.Rmd")
17 file <- tempfile(fileext = ".nb.html")
18 on.exit(unlink(file), add = TRUE)
19 rmarkdown::render(path, output_file = file, quiet = TRUE)
20
21 # confirm the evaluate hook has been restored post-render
22 expect_default_evaluate_hook()
23
24 # if running interactively, try running the following code to open the
25 # generated document -- in RStudio, you should see the source .Rmd opened,
26 # with outputs populated in the editor view
27 #
28 # file.edit(file)
29
30 # try parsing and validating the parse format
31 parsed <- parse_html_notebook(file)
32 expect_identical(parsed$rmd, read_utf8(path))
33 })
34
35 test_that("a custom output_source can be used on render", {
36 # set up output_source hook
37 png_path <- normalizePath(testthat::test_path("resources/tinyplot.png"), winslash = "/")
38 output_options <- list(output_source = function(code, context, ...) {
39
40 label <- context$label
41
42 if (label == "chunk-one") {
43 expect_true(context$chunk.index == 1)
44 output <- summary(cars)
45 return(list(
46 html_notebook_output_code("summary(cars)"),
47 output
48 ))
49 }
50
51 if (label == "chunk-two") {
52 expect_true(context$chunk.index == 2)
53 return(html_notebook_output_img(png_path))
54 }
55
56 if (label == "chunk-three") {
57 expect_true(context$chunk.index == 3)
58 if (requireNamespace("dygraphs", quietly = TRUE)) {
59
60 library(dygraphs)
61 widget <- dygraph(nhtemp, main = "New Haven Temperatures") %>%
62 dyRangeSelector(dateWindow = c("1920-01-01", "1960-01-01"))
63
64 return(list(
65 html_notebook_output_code("# This is a fake comment!"),
66 widget
67 ))
68 }
69
70 return(html_notebook_output_html("<!-- dygraphs not installed -->"))
71 }
72
73 if (label == "chunk-four") {
74 expect_true(context$chunk.index == 4)
75 styles <- list(
76 "background-color" = "#4AF",
77 "width" = "100px",
78 "height" = "100px",
79 "border" = "1px solid black",
80 "box-shadow" = "0 0 10px #ACE"
81 )
82
83 pasted <- paste(names(styles), styles, sep = ": ", collapse = "; ")
84 format <- '<div style="%s"><pre style="margin-top: 30px; text-align: center;">Box!</pre></div>'
85 rendered <- sprintf(format, pasted)
86 return(html_notebook_output_html(rendered))
87 }
88
89 })
90
91 input_file <- testthat::test_path("resources/r-notebook.Rmd")
92 # output_file <- "~/Desktop/output.nb.html"
93 output_file <- tempfile(fileext = ".nb.html")
94 on.exit(unlink(output_file), add = TRUE)
95
96 # check initial status of hook
97 expect_default_evaluate_hook()
98
99 render(input_file, output_options = output_options, output_file = output_file, quiet = TRUE)
100
101 # confirm the evaluate hook has been restored post-render
102 expect_default_evaluate_hook()
103
104 # parse notebook
105 parsed <- parse_html_notebook(output_file)
106
107 })
108
109 test_that("UFT8 character in html widget does not break notebook annotation", {
110
111 # as of htmltools v0.5.1 we no longer use token based htmlPreserve
112 skip_if(packageVersion("htmltools") >= "0.5.1")
113
114 # from issue in https://github.com/rstudio/rmarkdown/issues/1762
115 # simulate html widget code
116 html_dependency_dummy <- function() {
117 htmltools::htmlDependency(
118 name = "dummy",
119 version = "0.0.0",
120 src = "dummy_file",
121 script = "dummy.js")
122 }
123 utf8_strings <- enc2utf8("éà")
124 output <- htmltools::htmlPreserve(utf8_strings)
125 output <- knitr::asis_output(output, meta = html_dependency_dummy())
126 reg_res <- paste0(
127 "<!-- rnb-htmlwidget-begin .*-->\n",
128 utf8_strings,"\n",
129 "<!-- rnb-htmlwidget-end -->\n"
130 )
131 expect_match(notebook_render_html_widget(output),
132 reg_res)
133 })