Title: | Helper Functions to Manage Protocols |
---|---|
Description: | Helper functions to manage INBO protocols. |
Authors: | Hans Van Calster [aut, cre] (<https://orcid.org/0000-0001-8595-8426>, Research Institute for Nature and Forest (INBO)), Thierry Onkelinx [aut] (<https://orcid.org/0000-0001-8804-4216>, Research Institute for Nature and Forest (INBO)), Floris Vanderhaeghe [aut] (<https://orcid.org/0000-0002-6378-6229>, Research Institute for Nature and Forest (INBO)), Els Lommelen [ctb] (<https://orcid.org/0000-0002-3481-5684>, Research Institute for Nature and Forest (INBO)), Research Institute for Nature and Forest (INBO) [cph, fnd] |
Maintainer: | Hans Van Calster <[email protected]> |
License: | GPL-3 |
Version: | 0.6.1 |
Built: | 2024-12-04 05:13:34 UTC |
Source: | https://github.com/inbo/protocolhelper |
index.Rmd
fileAdds dependencies to the YAML of an index.Rmd
file
add_dependencies( code_mainprotocol, protocol_code, version_number, params, appendix = !is.na(params) )
add_dependencies( code_mainprotocol, protocol_code, version_number, params, appendix = !is.na(params) )
code_mainprotocol |
Protocol code of the protocol for which dependencies
need to be declared in the YAML of its |
protocol_code |
Character vector of protocol codes that are dependencies to the main protocol. |
version_number |
Character vector of version numbers corresponding with protocol_code. |
params |
List of lists with protocol-specific parameters corresponding
with parameters from the protocols in protocol_code. Use |
appendix |
Logical vector indicating whether or not a dependency needs
to be included as a subprotocol (at the end of the main protocol in an
appendix). Default is |
Other creation:
add_one_subprotocol()
,
add_subprotocols()
,
create_protocol()
,
insert_protocolsection()
,
update_protocol()
,
update_version_number()
## Not run: protocolhelper::add_dependencies( code_mainprotocol = "spp-999-en", protocol_code = c("sfp-123-en", "spp-124-en"), version_number = c("2020.01", "2020.02"), params = list(NA, list(width = 8, height = 8)) ) ## End(Not run)
## Not run: protocolhelper::add_dependencies( code_mainprotocol = "spp-999-en", protocol_code = c("sfp-123-en", "spp-124-en"), version_number = c("2020.01", "2020.02"), params = list(NA, list(width = 8, height = 8)) ) ## End(Not run)
pander
tablesThe function adds a reference label to the caption so that pander
tables
can be cross-referenced in bookdown
using the \@ref(tab:label)
syntax.
The function should only be used in pander(x, caption = add_label())
.
add_label(caption = "", tag = "tab")
add_label(caption = "", tag = "tab")
caption |
The caption text as a string |
tag |
The tag to use as a prefix. Default is |
The caption text prefixed with a reference label.
Other utility:
get_path_to_protocol()
,
get_protocol_type()
,
get_protocolnumbers()
,
get_short_titles()
,
get_version_number()
,
increment_version_number()
add_label("caption text")
add_label("caption text")
The function renders the sub-protocol to
bookdown::markdown_document2()
and
saves the resulting md
file (and any associated media and data files) in a
subfolder of the directory of the project-specific protocol.
This function should normally not be called directly.
Use add_subprotocols()
instead.
add_one_subprotocol( code_subprotocol, version_number, params2 = NULL, code_mainprotocol, fetch_remote = TRUE )
add_one_subprotocol( code_subprotocol, version_number, params2 = NULL, code_mainprotocol, fetch_remote = TRUE )
code_subprotocol |
Character string giving the protocol code from
which a sub-protocol will be made (usually a |
version_number |
Character string with format |
params2 |
A list of parameter key-value pairs. |
code_mainprotocol |
Character string giving the protocol code for the main protocol |
fetch_remote |
Whether or not to fetch the remote. Default TRUE. |
Other creation:
add_dependencies()
,
add_subprotocols()
,
create_protocol()
,
insert_protocolsection()
,
update_protocol()
,
update_version_number()
## Not run: add_subprotocols(code_mainprotocol = 'spp-999-en') ## End(Not run)
## Not run: add_subprotocols(code_mainprotocol = 'spp-999-en') ## End(Not run)
The function should be called interactively (in the console)
after the dependencies section in the YAML
header of the index.Rmd
file
of the main protocol has been filled in with the aid of the
protocolhelper::add_dependencies()
function.
For reproducibility, it is good practice to save the call in a
separate R script.
For each sub-protocol a single markdown file and associated media and
data files will be written.
Each sub-protocol will be written to a subfolder of the main
protocol.
The subfolder name is the same as the version number of the
sub-protocol.
add_subprotocols(code_mainprotocol, fetch_remote = TRUE)
add_subprotocols(code_mainprotocol, fetch_remote = TRUE)
code_mainprotocol |
Character string giving the protocol code for the main protocol |
fetch_remote |
Whether or not to fetch the remote. Default TRUE. |
Other creation:
add_dependencies()
,
add_one_subprotocol()
,
create_protocol()
,
insert_protocolsection()
,
update_protocol()
,
update_version_number()
frontmatter
and structure
Combines check_frontmatter()
and check_structure()
in one function.
check_all(protocol_code, fail = !interactive())
check_all(protocol_code, fail = !interactive())
protocol_code |
Character string giving the protocol code |
fail |
Should the function drop an error in case of a problem?
Defaults to |
A report of all failed checks.
Other check:
check_all_author_info()
,
check_frontmatter()
,
check_structure()
,
protocolcheck
,
validate_orcid()
Checks the format of author names and orcid
ids.
check_all_author_info(author_list, problems_vect)
check_all_author_info(author_list, problems_vect)
author_list |
the yaml front matter part containing author info |
problems_vect |
character vector of previously encountered problems |
a character vector of previously encountered problems and problems
identified for author names and orcid
ids.
Other check:
check_all()
,
check_frontmatter()
,
check_structure()
,
protocolcheck
,
validate_orcid()
This function reads metadata from the yaml front matter stored
in the index.Rmd
file of a protocol and checks if the metadata format is
correct.
This function is intended for checking if a protocol is ready to be rendered
and published (for instance, it will fail if version number is
YYYY.NN.dev
).
check_frontmatter(protocol_code, fail = !interactive())
check_frontmatter(protocol_code, fail = !interactive())
protocol_code |
Character string giving the protocol code |
fail |
Should the function drop an error in case of a problem?
Defaults to |
A report of all failed checks.
Other check:
check_all()
,
check_all_author_info()
,
check_structure()
,
protocolcheck
,
validate_orcid()
This function reads the protocol and checks if the document structure is correct: chunks have head and tail, required headings are present and in the right order,... This function is intended for checking if a protocol is ready to be rendered and published.
check_structure(protocol_code, fail = !interactive())
check_structure(protocol_code, fail = !interactive())
protocol_code |
Character string giving the protocol code |
fail |
Should the function drop an error in case of a problem?
Defaults to |
A report of all failed checks.
Other check:
check_all()
,
check_all_author_info()
,
check_frontmatter()
,
protocolcheck
,
validate_orcid()
This function is derived from the
redoc::dedoc()
function and uses pandoc
to convert between docx and markdown.
Several options are preset to end-up with a markdown document that is in
syntax as close as possible to Rmarkdown files in RStudio.
During conversion, graphics (e.g. png, jpg) will be extracted from the docx
archive and placed in a folder ./media
and named image1
, image2
,
etcetera.
Additionally, .emf files will be converted to .png.
convert_docx_to_rmd( from, to = sub("docx$", "Rmd", from), dir_media = ".", wrap = NA, overwrite = FALSE, verbose = FALSE, wd = getwd() )
convert_docx_to_rmd( from, to = sub("docx$", "Rmd", from), dir_media = ".", wrap = NA, overwrite = FALSE, verbose = FALSE, wd = getwd() )
from |
The |
to |
The filename including path to write the resulting |
dir_media |
The directory to write the folder |
wrap |
The width at which to wrap text.
If |
overwrite |
Whether or not to overwrite the |
verbose |
Whether to print |
wd |
Current working directory (used to handle relative paths). |
Metadata in the page headers and footers of the docx are ignored and will thus be lost during conversion. In case the header or footer did contain important metadata, it will need to be recovered manually. Usually header information will go inside a yaml section of an Rmarkdown.
Other convert:
add_captions()
This function will create a new folder based on values that are
passed on via the parameters and creates a R-markdown (bookdown) skeleton
based on a template file to start working on a new protocol.
Optionally, the rmarkdown chapters are rendered to an html file which will
be saved in a matching subfolder of the docs
folder.
create_protocol( protocol_type = c("sfp", "spp", "sap", "sop", "sip"), title, short_title, authors, orcids, date = Sys.Date(), reviewers, file_manager, version_number = get_version_number(), theme = NULL, project_name = NULL, language = c("nl", "en"), subtitle = NULL, from_docx = NULL, protocol_number = NULL, template = protocol_type, render = FALSE ) create_sfp( title, subtitle = NULL, short_title, authors, orcids, date = Sys.Date(), reviewers, file_manager, version_number = get_version_number(), theme = c("generic", "water", "air", "soil", "vegetation", "species"), language = c("nl", "en"), from_docx = NULL, protocol_number = NULL, template = c("sfp", "generic"), render = FALSE ) create_spp( title, subtitle = NULL, short_title, authors, orcids, date = Sys.Date(), reviewers, file_manager, version_number = get_version_number(), project_name, language = c("nl", "en"), from_docx = NULL, protocol_number = NULL, template = c("spp"), render = FALSE ) create_sap( title, subtitle = NULL, short_title, authors, orcids, date = Sys.Date(), reviewers, file_manager, version_number = get_version_number(), language = c("nl", "en"), from_docx = NULL, protocol_number = NULL, template = c("sap", "generic"), render = FALSE ) create_sip( title, subtitle = NULL, short_title, authors, orcids, date = Sys.Date(), reviewers, file_manager, version_number = get_version_number(), language = c("nl", "en"), from_docx = NULL, protocol_number = NULL, template = c("sip", "generic"), render = FALSE ) create_sop( title, subtitle = NULL, short_title, authors, orcids, date = Sys.Date(), reviewers, file_manager, version_number = get_version_number(), language = c("nl", "en"), from_docx = NULL, protocol_number = NULL, template = c("sop", "generic"), render = FALSE )
create_protocol( protocol_type = c("sfp", "spp", "sap", "sop", "sip"), title, short_title, authors, orcids, date = Sys.Date(), reviewers, file_manager, version_number = get_version_number(), theme = NULL, project_name = NULL, language = c("nl", "en"), subtitle = NULL, from_docx = NULL, protocol_number = NULL, template = protocol_type, render = FALSE ) create_sfp( title, subtitle = NULL, short_title, authors, orcids, date = Sys.Date(), reviewers, file_manager, version_number = get_version_number(), theme = c("generic", "water", "air", "soil", "vegetation", "species"), language = c("nl", "en"), from_docx = NULL, protocol_number = NULL, template = c("sfp", "generic"), render = FALSE ) create_spp( title, subtitle = NULL, short_title, authors, orcids, date = Sys.Date(), reviewers, file_manager, version_number = get_version_number(), project_name, language = c("nl", "en"), from_docx = NULL, protocol_number = NULL, template = c("spp"), render = FALSE ) create_sap( title, subtitle = NULL, short_title, authors, orcids, date = Sys.Date(), reviewers, file_manager, version_number = get_version_number(), language = c("nl", "en"), from_docx = NULL, protocol_number = NULL, template = c("sap", "generic"), render = FALSE ) create_sip( title, subtitle = NULL, short_title, authors, orcids, date = Sys.Date(), reviewers, file_manager, version_number = get_version_number(), language = c("nl", "en"), from_docx = NULL, protocol_number = NULL, template = c("sip", "generic"), render = FALSE ) create_sop( title, subtitle = NULL, short_title, authors, orcids, date = Sys.Date(), reviewers, file_manager, version_number = get_version_number(), language = c("nl", "en"), from_docx = NULL, protocol_number = NULL, template = c("sop", "generic"), render = FALSE )
protocol_type |
Either |
title |
A character string giving the main title of the protocol |
short_title |
A character string of less than 20 characters to use in folder and file names |
authors |
A character vector for authors of the form
|
orcids |
A character vector of |
date |
A character string of the date in ISO 8601 format ( |
reviewers |
A character vector for reviewers of the form First name Last name |
file_manager |
A character string for the name of the document maintainer of the form First name Last name |
version_number |
A version number of the form |
theme |
A character string equal to one of |
project_name |
A character string that is used as the folder location
( |
language |
Language of the protocol, either |
subtitle |
A character string for an optional subtitle |
from_docx |
A character string with the path (absolute or relative) to
a |
protocol_number |
A character string giving the protocol number.
This parameter should normally not be specified (i.e. NULL), unless
|
template |
Which template to use?
Default is set equal to protocol_type.
However, you can also set this to |
render |
Whether or not to render the protocol to html. Defaults to FALSE. |
It is assumed that the source
folder is a subfolder of an RStudio
project with git version control.
A target folder to which files will be written will be created as
subdirectories beneath source
.
The subfolder structure is of the form
/sfp/<theme>/<sfp>_<protocolnumber>_<language>_<short_title>/
for
standard field protocols.
Or /spp/<project_name>/<spp>_<protocolnumber>_<language>_<short_title>/
for standard project protocols.
Or /sip/<sip>_<protocolnumber>_<language>_<short_title>/
for sips (and
analogous for sop and sap).
The folder names are determined by the corresponding arguments of the
function.
A matching subfolder structure will be created beneath the docs
folder (and
output files needed for rendering to html output will be placed in it if
render = TRUE
.
The template Rmarkdown files and the Rmarkdown files that result from
converting a docx protocol (see from_docx
argument), will be written to
the target folder beneath source
.
Template Rmarkdown files with the same name as Rmarkdown files that result
from converting a docx protocol will be overwritten by the latter.
Besides Rmarkdown files, this target folder will also contain files needed to
render to a Bookdown gitbook
such as a _bookdown.yml
and _output.yml
.
The NEWS.md
file must be used to document the changes between revisions
of the protocol.
Furthermore, a data
and a media
folder will be created as subdirectories
of the target folder.
The media
folder can be used to store image files and will contain image
files extracted from the docx protocol when the from_docx
argument is used.
The data
folder can be used to store tabular data that are needed for the
protocol.
Other creation:
add_dependencies()
,
add_one_subprotocol()
,
add_subprotocols()
,
insert_protocolsection()
,
update_protocol()
,
update_version_number()
## Not run: protocolhelper::create_protocol( protocol_type = "sfp", title = "Test 1", subtitle = "subtitle", short_title = "water 1", authors = c("Someone, Else", "Another, One"), orcids = c("0000-0001-2345-6789", "0000-0002-2345-6789"), reviewers = "me", file_manager = "who?", theme = "water", language = "en") ## End(Not run)
## Not run: protocolhelper::create_protocol( protocol_type = "sfp", title = "Test 1", subtitle = "subtitle", short_title = "water 1", authors = c("Someone, Else", "Another, One"), orcids = c("0000-0001-2345-6789", "0000-0002-2345-6789"), reviewers = "me", file_manager = "who?", theme = "water", language = "en") ## End(Not run)
A function that is used by other functions and should normally not be used directly.
For existing protocol codes, arguments theme
and project_name
are
always ignored.
The function will return the absolute path for that protocol.
For new sfp
or spp
protocols, also either the theme
or the
project_name
argument and short_title
are required apart from the
protocol_code
.
The function will construct the absolute path where the source code for that
new protocol will be written.
get_path_to_protocol( protocol_code, theme = NULL, project_name = NULL, short_title = NULL )
get_path_to_protocol( protocol_code, theme = NULL, project_name = NULL, short_title = NULL )
protocol_code |
Character string giving the protocol code |
theme |
A character string equal to one of |
project_name |
Character string giving the name of the project folder.
Defaults to NULL.
Only needed if no folder with the name of the protocol code exists and
the request is for a |
short_title |
A character string of less than 20 characters to use in folder and filenames. Defaults to NULL. Only needed if no folder with the name of the protocol code exists. |
A character vector containing the full path to the protocol.
Other utility:
add_label()
,
get_protocol_type()
,
get_protocolnumbers()
,
get_short_titles()
,
get_version_number()
,
increment_version_number()
## Not run: get_path_to_protocol(protocol_code = "sfp-401-nl") ## End(Not run)
## Not run: get_path_to_protocol(protocol_code = "sfp-401-nl") ## End(Not run)
The protocol type corresponds to the first 3 letters of the protocol code
get_protocol_type(protocol_code, labels = TRUE, auto_identifier = FALSE)
get_protocol_type(protocol_code, labels = TRUE, auto_identifier = FALSE)
protocol_code |
Character vector giving the protocol code(s) |
labels |
Logical. If |
auto_identifier |
Logical.
If |
A factor with 5 levels corresponding to sfp
, sip
, sap
, sop
and spp
. The labels depend on auto_identifier
setting.
Other utility:
add_label()
,
get_path_to_protocol()
,
get_protocolnumbers()
,
get_short_titles()
,
get_version_number()
,
increment_version_number()
This function will search for protocol numbers in filenames of Rmarkdown files listed underneath the source folder. The search will be restricted to files of a given protocol type and given language.
get_protocolnumbers( protocol_type = c("sfp", "sip", "sap", "sop", "spp"), language = c("nl", "en") )
get_protocolnumbers( protocol_type = c("sfp", "sip", "sap", "sop", "spp"), language = c("nl", "en") )
protocol_type |
A character string equal to |
language |
Language of the protocol, either |
A character vector with occupied protocol numbers for a specific protocol type
Other utility:
add_label()
,
get_path_to_protocol()
,
get_protocol_type()
,
get_short_titles()
,
get_version_number()
,
increment_version_number()
## Not run: get_protocolnumbers() ## End(Not run)
## Not run: get_protocolnumbers() ## End(Not run)
This function will search for short titles in filenames of Rmarkdown files listed underneath the source folder. The search will be restricted to files of a given protocol type and given language.
get_short_titles( protocol_type = c("sfp", "sip", "sap", "sop", "spp"), language = c("nl", "en") )
get_short_titles( protocol_type = c("sfp", "sip", "sap", "sop", "spp"), language = c("nl", "en") )
protocol_type |
A character string equal to |
language |
Language of the protocol, either |
A character vector with short titles that are in use for a given protocol type.
Other utility:
add_label()
,
get_path_to_protocol()
,
get_protocol_type()
,
get_protocolnumbers()
,
get_version_number()
,
increment_version_number()
## Not run: get_short_titles() ## End(Not run)
## Not run: get_short_titles() ## End(Not run)
Looks up pre-existing version numbers of protocols in the main branch and calculates an incremented (next) version number for the currently checkout branch containing the created/in development/updated/ready to be released protocol. Your local main branch needs to up to date (aligned with remote) for this. If this is not the case, or other issues are detected regarding a non-clean local git repository - informative error messages will be given.
get_version_number(path = ".")
get_version_number(path = ".")
path |
Defaults to current working directory.
This should correspond with the root directory of the |
A string containing the next (incremented) version number
Other utility:
add_label()
,
get_path_to_protocol()
,
get_protocol_type()
,
get_protocolnumbers()
,
get_short_titles()
,
increment_version_number()
Given a set of published protocol version numbers, calculate the next version number
increment_version_number(versions)
increment_version_number(versions)
versions |
a character vector with previously published version numbers |
A string containing the next (incremented) version number
Other utility:
add_label()
,
get_path_to_protocol()
,
get_protocol_type()
,
get_protocolnumbers()
,
get_short_titles()
,
get_version_number()
The idea is to execute this function in an R chunk with knitr
option results="asis"
.
insert_protocolsection( code_subprotocol, version_number, file_name, section = NULL, demote_header = c(0, 1, 2, -1), fetch_remote = TRUE )
insert_protocolsection( code_subprotocol, version_number, file_name, section = NULL, demote_header = c(0, 1, 2, -1), fetch_remote = TRUE )
code_subprotocol |
Character string giving the protocol code from
which a sub-protocol will be made (usually a |
version_number |
Character string with format |
file_name |
Character string with the name of the Rmarkdown file (a chapter starting with a level 1 heading). |
section |
Optional character string with the name of a section within an Rmarkdown file. Can also be a unique substring of a section title. If not specified (the default): the whole file is taken. It is assumed that the section has a level 2 heading. |
demote_header |
Number of '#' to prefix to all titles before inserting in current protocol. Default is 0. A negative value can be used to remove '#' from all section titles. Allowed values are visible in the usage section. |
fetch_remote |
Whether or not to fetch the remote. Default TRUE. |
The function will return Rmarkdown
Other creation:
add_dependencies()
,
add_one_subprotocol()
,
add_subprotocols()
,
create_protocol()
,
update_protocol()
,
update_version_number()
## Not run: insert_protocolsection( code_subprotocol = "sfp-401-nl", version_number = "2021.01", file_name = "07_stappenplan.Rmd" ) ## End(Not run)
## Not run: insert_protocolsection( code_subprotocol = "sfp-401-nl", version_number = "2021.01", file_name = "07_stappenplan.Rmd" ) ## End(Not run)
protocolcheck
R6 classA class that collects and shows all check results.
protocol_code
Character string giving the protocol code.
path
Character string giving the relative path to the protocol.
error
Character vector containing all errors found in the protocol
new()
Create a new Protocolcheck
object.
protocolcheck$new(protocol_code)
protocol_code
Character string giving the protocol code.
A new Protocolcheck
object
add_error()
Add a new error to the Protocolcheck
object.
protocolcheck$add_error(msg)
msg
Error message to be added.
check()
Give error report from Protocolcheck
object.
protocolcheck$check(fail)
fail
Should an error be dropped if the report contains errors?
An error report (and if desired an error is dropped).
clone()
The objects of this class are cloneable with this method.
protocolcheck$clone(deep = FALSE)
deep
Whether to make a deep clone.
Other check:
check_all()
,
check_all_author_info()
,
check_frontmatter()
,
check_structure()
,
validate_orcid()
This function is a simple wrapper around
bookdown::render_book()
and can be used to render a protocol to html and
pdf in order to preview updates that have been made.
render_protocol(protocol_code = NULL, output_dir = NULL, ...)
render_protocol(protocol_code = NULL, output_dir = NULL, ...)
protocol_code |
Character string giving the protocol code |
output_dir |
The output directory. If |
... |
additional parameters passed on to |
The rendered html and pdf file and associated files needed by the
html file will be put in the directory implied by the output_dir
parameter.
## Not run: render_protocol(protocol_code = "sfp_401-nl") ## End(Not run)
## Not run: render_protocol(protocol_code = "sfp_401-nl") ## End(Not run)
The function creates a branch named after the protocol_code
update_protocol(protocol_code)
update_protocol(protocol_code)
protocol_code |
Character string giving the protocol code |
NULL invisibly
Other creation:
add_dependencies()
,
add_one_subprotocol()
,
add_subprotocols()
,
create_protocol()
,
insert_protocolsection()
,
update_version_number()
index.Rmd
file and optionally in protocol NEWS.md
Makes use of get_version_number
to get a new version number
and changes this accordingly in the YAML section of index.Rmd
file and
optionally in NEWS.md
.
update_version_number( protocol_code, commit = TRUE, update_news = TRUE, path = "." )
update_version_number( protocol_code, commit = TRUE, update_news = TRUE, path = "." )
protocol_code |
The protocol_code corresponding with the name of the branch that contains the new or updated protocol. |
commit |
Logical. Default TRUE. Whether or not to add and commit the changes to the protocol branch |
update_news |
Logical. Default TRUE. Whether or not to find and replace
old version number by new version number in the |
path |
Default is current working directory. Should correspond with
root directory of |
TRUE if version number in yaml is updated. FALSE otherwise.
Other creation:
add_dependencies()
,
add_one_subprotocol()
,
add_subprotocols()
,
create_protocol()
,
insert_protocolsection()
,
update_protocol()
Generates check digit as per ISO 7064 11,2.
The last character in the ORCID ID
is a checksum.
In accordance with ISO/IEC 7064:2003, MOD 11-2,
this checksum must be "0-9" or "X", a capital letter X which represents
the value 10.
validate_orcid(orcid)
validate_orcid(orcid)
orcid |
An |
Logical. TRUE if check digit is correct.
Other check:
check_all()
,
check_all_author_info()
,
check_frontmatter()
,
check_structure()
,
protocolcheck
validate_orcid("0000-0002-6378-6229")
validate_orcid("0000-0002-6378-6229")