Skip to contents

These functions write one or more source(s) from memory (as loaded by load_source() or load_sources() to a file.

Usage

write_source(
  x,
  output,
  encoding = rock::opts$get("encoding"),
  preventOverwriting = rock::opts$get("preventOverwriting"),
  silent = rock::opts$get("silent")
)

write_sources(
  x,
  output,
  filenamePrefix = "",
  filenameSuffix = "_written",
  recursive = TRUE,
  encoding = rock::opts$get("encoding"),
  preventOverwriting = rock::opts$get("preventOverwriting"),
  silent = rock::opts$get("silent")
)

Arguments

x

The source(s).

output

The filename (for rock::write_source()) or path (for rock::write_sources()) to write to.

encoding

The encoding to use.

preventOverwriting

Whether to prevent against overwriting of the file(s) to write. Set to FALSE to overwrite.

silent

Whether to be chatty or quiet.

filenamePrefix, filenameSuffix

Optional prefixes or suffixes to pre- or append to the filenames when writing the files.

recursive

Whether to recursively create directories if the output directory does not yet exist.

Value

Invisibly, the input (x), to enable chaining in pipes.

Examples

### Get path to example source
examplePath <-
  system.file("extdata", package="rock");

### Get a path to one example file
exampleFile <-
  file.path(examplePath, "example-1.rock");

### Get a temporary file to write to
tempFile <- tempfile(fileext = ".rock")

### For R versions below 4.1
loadedSource <-
  rock::load_source(exampleFile);

loadedSource <-
  rock::code_source(
    loadedSource,
    c("Lorem Ipsum" = "lorumIpsum")
  );
#> --------PRE: What is Lorem Ipsum?
#>        POST: What is Lorem Ipsum? [[lorumIpsum]]
#> --------PRE: Lorem Ipsum is simply dummy text of the printing and typesetting industry. [[parentCode1>childCode1]]
#>        POST: Lorem Ipsum is simply dummy text of the printing and typesetting industry. [[parentCode1>childCode1]] [[lorumIpsum]]
#> --------PRE: Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
#>        POST: Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, [[lorumIpsum]]
#> --------PRE: ~~~the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
#>        POST: ~~~the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, [[lorumIpsum]]
#> --------PRE: ~~including versions of Lorem Ipsum. [[parentCode2>childCode4]]
#>        POST: ~~including versions of Lorem Ipsum. [[parentCode2>childCode4]] [[lorumIpsum]]
#> --------PRE: The point of using Lorem Ipsum
#>        POST: The point of using Lorem Ipsum [[lorumIpsum]]
#> --------PRE: ~~desktop publishing packages and web page editors now use Lorem Ipsum as their
#>        POST: ~~desktop publishing packages and web page editors now use Lorem Ipsum as their [[lorumIpsum]]
#> --------PRE: Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
#>        POST: Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots [[lorumIpsum]]
#> --------PRE: looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum
#>        POST: looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum [[lorumIpsum]]
#> --------PRE: Lorem Ipsum comes from sections 1.10.32 and
#>        POST: Lorem Ipsum comes from sections 1.10.32 and [[lorumIpsum]]
#> --------PRE: popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor
#>        POST: popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor [[lorumIpsum]]
#> --------PRE: The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for
#>        POST: The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for [[lorumIpsum]]
#> --------PRE: [[uid=73xk6950]] There are many variations of passages of Lorem Ipsum available, but the majority
#>        POST: [[uid=73xk6950]] There are many variations of passages of Lorem Ipsum available, but the majority [[lorumIpsum]]
#> --------PRE: of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden
#>        POST: of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden [[lorumIpsum]]
#> --------PRE: in the middle of text. All the Lorem Ipsum generators on the Internet tend to
#>        POST: in the middle of text. All the Lorem Ipsum generators on the Internet tend to [[lorumIpsum]]
#> --------PRE: handful of model sentence structures, to generate Lorem Ipsum which looks
#>        POST: handful of model sentence structures, to generate Lorem Ipsum which looks [[lorumIpsum]]
#> --------PRE: reasonable. The generated Lorem Ipsum is therefore always free from [[grandchildCode2]]
#>        POST: reasonable. The generated Lorem Ipsum is therefore always free from [[grandchildCode2]] [[lorumIpsum]]

rock::write_source(
  loadedSource,
  tempFile
);

### From R 4.1 onwards, you can also chain
### these commands using the pipe operator.
###
### Note that that means that this example
### will not run if you have a previous
### version of R.
loadedSource <-

  rock::load_source(exampleFile) |>

  rock::code_source(c("Lorem Ipsum" = "lorumIpsum")) |>

  rock::write_source(tempFile);
#> --------PRE: What is Lorem Ipsum?
#>        POST: What is Lorem Ipsum? [[lorumIpsum]]
#> --------PRE: Lorem Ipsum is simply dummy text of the printing and typesetting industry. [[parentCode1>childCode1]]
#>        POST: Lorem Ipsum is simply dummy text of the printing and typesetting industry. [[parentCode1>childCode1]] [[lorumIpsum]]
#> --------PRE: Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
#>        POST: Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, [[lorumIpsum]]
#> --------PRE: ~~~the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
#>        POST: ~~~the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, [[lorumIpsum]]
#> --------PRE: ~~including versions of Lorem Ipsum. [[parentCode2>childCode4]]
#>        POST: ~~including versions of Lorem Ipsum. [[parentCode2>childCode4]] [[lorumIpsum]]
#> --------PRE: The point of using Lorem Ipsum
#>        POST: The point of using Lorem Ipsum [[lorumIpsum]]
#> --------PRE: ~~desktop publishing packages and web page editors now use Lorem Ipsum as their
#>        POST: ~~desktop publishing packages and web page editors now use Lorem Ipsum as their [[lorumIpsum]]
#> --------PRE: Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots
#>        POST: Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots [[lorumIpsum]]
#> --------PRE: looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum
#>        POST: looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum [[lorumIpsum]]
#> --------PRE: Lorem Ipsum comes from sections 1.10.32 and
#>        POST: Lorem Ipsum comes from sections 1.10.32 and [[lorumIpsum]]
#> --------PRE: popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor
#>        POST: popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor [[lorumIpsum]]
#> --------PRE: The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for
#>        POST: The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for [[lorumIpsum]]
#> --------PRE: [[uid=73xk6950]] There are many variations of passages of Lorem Ipsum available, but the majority
#>        POST: [[uid=73xk6950]] There are many variations of passages of Lorem Ipsum available, but the majority [[lorumIpsum]]
#> --------PRE: of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden
#>        POST: of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden [[lorumIpsum]]
#> --------PRE: in the middle of text. All the Lorem Ipsum generators on the Internet tend to
#>        POST: in the middle of text. All the Lorem Ipsum generators on the Internet tend to [[lorumIpsum]]
#> --------PRE: handful of model sentence structures, to generate Lorem Ipsum which looks
#>        POST: handful of model sentence structures, to generate Lorem Ipsum which looks [[lorumIpsum]]
#> --------PRE: reasonable. The generated Lorem Ipsum is therefore always free from [[grandchildCode2]]
#>        POST: reasonable. The generated Lorem Ipsum is therefore always free from [[grandchildCode2]] [[lorumIpsum]]
#> Warning: Could not write source to `/tmp/RtmpJ0b5YM/filecd3033012b0d9.rock` - the file exists already, and preventOverwriting is set to TRUE.