create_chunk: Tools for creating (data) chunks and inserting them into...

Description Usage Arguments Details Functions Author(s) See Also Examples

View source: R/create_chunks.R

Description

These helper functions allow one to add the chunk header and tail to text containing chunk contents and then insert that into a Rmarkdown document.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
create_chunk(
  text = readLines(file),
  ...,
  chunk_label = NULL,
  chunk_type = "data",
  file = NULL,
  chunk_options_string = NULL,
  split_lines = TRUE,
  newline = platform.newline()
)

insert_chunk(chunk, line, rmd.text = readLines(rmd.file), rmd.file = NULL)

Arguments

text

Character vector with contents of chunk, one line per element of vector. If the character vector has just a single element, then an attempt will be made to split it into lines using readLines.

...

Additional chunk options. These are not evaluated, but rather included in the function call as they are entered in the function call.

chunk_label

Character string giving the label to be used for the chunk.

chunk_type

Character string giving the chunk type. Defaults to "data".

file

Path to file containing chunk contents. Ignored if text argument supplied. As a consequence, this means that all arguments must be named if the file argument is supplied to create_chunk.

chunk_options_string

Character vector with additional chunk options that will be included in the header after the arguments in ....

split_lines

Boolean indicating whether or not the chunk contents should be split along line breaks before returning. Defaults to TRUE.

newline

Character string used to end lines of text. Only relevant if split_lines=FALSE. Defaults to platform.newline().

chunk

Character vector with chunk contents including header and tail. If the character vector has just a single element, then an attempt will be made to split it into lines using readLines.

line

Line number where chunk to be inserted.

rmd.text

Character vector with contents of Rmarkdown document where chunk contents are to be inserted. It should have one element per line of text (as returned by readLines). If the character vector has just a single element, then an attempt will be made to split it into lines using readLines.

rmd.file

Filename of Rmarkdown document where chunk contents are to be inserted. Ignored if rmd.text is supplied.

Details

create_chunk takes in the (possibly encoded by data_encode) contents of a chunk and adds the chunk header and closer, invisibly returning entire chunk contents as a character string.

insert_chunk takes the chunk contents and inserts it at the given line number in the rmd.text or rmd.file.

Note that the additional arguments to create_chunk (...) are not evaluated, but rather they are placed in the chunk header as they appear in the function call as additional chunk options.

Functions

Author(s)

David M. Kaplan dmkaplan2000@gmail.com

See Also

Other Chunk tools: create_data_chunk_dialog(), insert_data_chunk_template(), list_rmd_chunks(), remove_chunks_dialog()

Examples

 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
# Use a temporary directory ----------------------------
owd = getwd()
td = tempdir()
setwd(td)

# Test --------------
library(knitrdata)
library(magrittr) # For pipe operator

# Create new Rmarkdown document
if (file.exists("test.create_chunks.Rmd"))
  file.remove("test.create_chunks.Rmd")
rmarkdown::draft("test.create_chunks.Rmd","github_document","rmarkdown",
                 edit=FALSE)

# List all chunks in document
chunklst = list_rmd_chunks(file="test.create_chunks.Rmd")
chunklst

# Remove the pressure chunk
xx = split_rmd_by_chunk(file="test.create_chunks.Rmd",chunk_label="pressure")
txt = c(xx$pre_chunk,xx$post_chunk)
writeLines(txt,"test.create_chunks.Rmd")

# List chunks again
chunklst = list_rmd_chunks(file="test.create_chunks.Rmd")
chunklst

# Remove all but setup chunk
remove_chunks(file="test.create_chunks.Rmd",
              chunk_labels = 2:nrow(chunklst),
              output.file="test.create_chunks.Rmd")

# List all chunks again
chunklst = list_rmd_chunks(file="test.create_chunks.Rmd")
chunklst

# Create some binary data
x = data.frame(a=1:10,b=(1:10)^2)
saveRDS(x,"test.create_chunks.RDS")

# Push chunks into Rmarkdown document
# Insert in reverse order to not have to figure out line number
txt = create_chunk(chunk_label="plot",c("x","plot(b~a,data=x)"),chunk_type="r") %>%
  insert_chunk(11,rmd.file="test.create_chunks.Rmd")
txt = data_encode("test.create_chunks.RDS","base64") %>%
  create_chunk(chunk_label="thedata",output.var="x",format="binary",loader.function=readRDS) %>%
  insert_chunk(11,txt)
txt = create_chunk(chunk_label="loadknitrdata","library(knitrdata)",chunk_type="r") %>%
  insert_chunk(11,txt)

writeLines(txt,"test.create_chunks.Rmd")

# List all chunks again
chunklst = list_rmd_chunks(file="test.create_chunks.Rmd")
chunklst

# Render document to test
if (rmarkdown::pandoc_available(version="1.12.3"))
  rmarkdown::render("test.create_chunks.Rmd")

# Clean up --------------
file.remove("test.create_chunks.Rmd","test.create_chunks.RDS",
            "test.create_chunks.md","test.create_chunks.html")
unlink("test.create_chunks_files",recursive=TRUE)

setwd(owd)

knitrdata documentation built on Dec. 8, 2020, 5:08 p.m.