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")
  );

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);
#> Warning: Could not write source to `/tmp/RtmpDUyFAE/file4b9ad45820da1.rock` - the file exists already, and preventOverwriting is set to TRUE.