provide_file: Manage static files in project directory

View source: R/provide_file.R

provide_fileR Documentation

Manage static files in project directory

Description

Copies or overwrites files from a source directory to your project directory. Can be used to make sure that files you are referencing in your generated XML code are present and up to date.

Usage

provide_file(rel, to, from, overwrite = TRUE, mode = "0777", quiet = FALSE)

Arguments

rel

Relative path of file as to be used in HTML.

to

Full path to the project directory where files should be copied to.

from

Full path to the directory where the file can be found under its rel_path.

overwrite

Logical, whether existing files should be re-written or kept in place.

mode

Permissions for newly created directories below to.

quiet

Logical, whether you would like to see a message when files are copied or already exist.

Details

The function returns the relative path that was given as its first argument, e.g. it can be used inside XMLNode to add relative paths to arguments while also copying the referenced file to the given output directory, keeping the relative path.

It can be useful to write a simple wrapper around this function to set the relevant from and to paths for a project (see examples).

Value

When called, the file is copied from the from to the to directory, including the relative path given by rel. Missing target directories below to are created on-the-fly. If successful, the function finally returns an invisible character string identical to rel.

Examples

## Not run: 
# a direct call that would copy the file ~/webpage/v1/static/css/bootstrap.min.css
# to the project directory as /tmp/static/css/bootstrap.min.css
# and include "static/css/bootstrap.min.css" in the <link> tag
my_HTML <- XMLNode(
  "link",
  rel="stylesheet",
  type="text/css",
  href=provide_file(
    rel="static/css/bootstrap.min.css",
    to="/tmp",
    from="~/webpage/v1"
  )
)

# for larger projects, a wrapper function might become handy
prov <- function(
  rel,
  to="/tmp",
  from="~/webpage/v1",
  overwrite=TRUE,
  mode="0777"
){
  provide_file(rel=rel, to=to, from=from, overwrite=overwrite, mode=mode)
}
# let's combine it with a shortcut function for <link>
gen_tag_functions("link")
# now this code produces the same result as the direct call above
my_HTML2 <- link_(
  rel="stylesheet",
  type="text/css",
  href=prov("static/css/bootstrap.min.css")
)

## End(Not run)

XiMpLe documentation built on Aug. 22, 2023, 5:07 p.m.