| Type: | Package |
| Title: | Function and Pipeline Development |
| Version: | 1.0.0 |
| Date: | 2026-06-02 |
| Encoding: | UTF-8 |
| Maintainer: | Gael Millot <gael.millot@pasteur.fr> |
| Description: | Set of functions that perform checks according to the safer-r project recommendations for R function development (see https://github.com/safer-r). This includes checking argument values, ensuring correct specification of all mandatory arguments for embedded functions, as well as their explicit package namespace qualification, among other things. |
| URL: | <https://github.com/safer-r/saferDev>, <https://safer-r.github.io/saferDev/> |
| License: | GPL-3 |
| RoxygenNote: | 7.3.3 |
| Imports: | ggplot2 |
| Suggests: | knitr, rmarkdown, testthat (≥ 3.0.0) |
| VignetteBuilder: | knitr |
| NeedsCompilation: | no |
| Packaged: | 2026-06-02 16:14:25 UTC; gmillot |
| Author: | Haiding Wang [ctb],
Yushi Han [ctb],
Mia Legras [ctb],
Gael Millot |
| Repository: | CRAN |
| Date/Publication: | 2026-06-08 18:10:03 UTC |
Function for All Arguments Here
Description
Get the $MISSING_ARG_NAMES, $MISSING_ARGS and $NEW of all_args_here().
Usage
.all_args_here_fill(
arg_full,
arg_full_names,
tempo_split,
three_dots_log,
i2,
col1_i2,
col2_i2,
arg_user_setting_x,
warn,
warn_count,
lib_path,
error_text
)
Arguments
arg_full |
List of all arguments of the function with default value. |
arg_full_names |
Vector of strings of the names of the arguments of the function. Can be |
tempo_split |
Vector of strings of the observed argument writing of the function. |
three_dots_log |
Vector of logical. Is |
i2 |
Single positive and non null integer value indicating the loop number. |
col1_i2 |
Single positive and non null integer value indicating the code line number of the checked function. |
col2_i2 |
Single string indicating the name of the checked sub function. |
arg_user_setting_x |
Single string indicating the name of the checked function. |
warn |
Single warning string. Can be |
warn_count |
Single positive integer value indicating the warning count. |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Details
Warnings:
requires
saferDev::arg_check.In the safer Backbone section
"######## check of the required functions from the required packages", add alsosaferDev::arg_checkwhen checking for the presence of.all_args_here_fill.
Value
A list:
-
col6: the$MISSING_ARG_NAMES. -
col7: the$MISSING_ARGS. -
col8: the$STATUS.
Author(s)
Haiding Wang
Yushi Han
Base Operator Check
Description
Check if critical operators of R are not present in other packages or in the global env.
Usage
.base_op_check(error_text)
Arguments
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Value
An error message if at least one of the checked operator is present in the R scope, nothing otherwise.
Author(s)
Haiding Wang
Yushi Han
Colons Check Message
Description
Create the message for the colons_check() function.
Usage
.colons_check_message(
list_fun,
list_fun_pos,
line_nb,
ini,
arg_user_setting2,
text,
internal_fun_names,
lib_path,
error_text
)
Arguments
list_fun |
List of names of all the functions. |
list_fun_pos |
List of positions of first character of names of all the functions in |
line_nb |
Vector of integers of corresponding line numbers. |
ini |
Vector of strings of the initial function code analyzed. |
arg_user_setting2 |
List of argument user settings. |
text |
Either |
internal_fun_names |
Vector of strings of names of internal functions in the function code analyzed. Can be |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Details
Warnings:
requires
saferDev::arg_check,.noclean_functions.In main safer functions, in the section
"######## check of the required functions from the required packages", add also these 2 functions when checking for the presence of.colons_check_message.
Value
A list:
-
output.cat: the message (string). -
colon_not_here: logical vector. Doeslist_funcontain function names without::or:::?
Author(s)
Haiding Wang
Yushi Han
Extract All Function Names
Description
Extract all function names.
Usage
.extract_all_fun_names(text, pattern, lib_path, error_text)
Arguments
text |
Single string. |
pattern |
Single string of a Perl regex to extract function names. |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Details
Warnings:
requires
saferDev::arg_check.In the safer Backbone section
"######## check of the required functions from the required packages", add alsosaferDev::arg_checkwhen checking for the presence of.extract_all_fun_names.
Value
List containing:
-
string: the function names without parenthesis. -
pos: position of the first character of the function names in the input string
Author(s)
Haiding Wang
Yushi Han
Function Arguments Position
Description
Return the positions of 1st letter of the first function name in a string, as well as its own opening and closing parenthesis, as well as positions of all the internal parenthesis (inside the own parenthesis).
Usage
.fun_args_pos(text, pattern, lib_path, error_text)
Arguments
text |
Single string. |
pattern |
Single string of a Perl regex to extract function name and |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Details
Warnings:
The tested string must be cleaned form brackets between quotes. Use
.in_quotes_replacement()for that.Quotes in strings are escaped, so that position of
(in\"a(is 3, not 4.requires
saferDev::arg_check.In the safer Backbone section
"######## check of the required functions from the required packages", add alsosaferDev::arg_checkwhen checking for the presence of.fun_args_pos.
Value
A list containing positions:
-
begin_fun: position of the 1st letter of the first function name in the tested string. -
begin: position of the opening"("of the first function. -
end: position of the closing")"of the first function. -
middle_bracket_pos: list of the positions of each couple of brackets in the middle of thebeginandendpositions. In each compartment, the first number is the position of(and the second the position of).NULLif no inside brackets.
Author(s)
Haiding Wang
Yushi Han
Functions Detection
Description
Detect all the functions names inside a function.
Usage
.functions_detect(x, skipped_base, arg_user_setting2, lib_path, error_text)
Arguments
x |
Function name to test, written without quotes and brackets. |
skipped_base |
Vector of strings specifying the R basic functions skipped from detection. |
arg_user_setting2 |
Argument user settings list. |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Details
Does not check if the functions inside the code exist.
Use the regex pattern "([a-zA-Z]|\.[a-zA-Z._])[a-zA-Z0-9._]*\s*\(" to detect a function in the code.
The result in all_basic_funs are all the functions in base::c("package:stats", "package:graphics", "package:grDevices", "package:utils", "package:datasets", "package:methods", "Autoloads", "package:base").
Warnings:
Requires
saferDev::arg_check,.extract_all_fun_names,.has_odd_number_of_quotes.In the safer Backbone section
"######## check of the required functions from the required packages", add also these 3 functions when checking for the presence of.functions_detect.
Value
A list:
-
code: vector of strings of the code of the tested function. -
all_basic_funs: vector or strings of names of all the basic R functions. -
fun_names: list of names of all the functions, not consideringbase::c("function", "if", "for", "while", "repeat"). Compartment names indicate the code line number of the functions incode. -
fun_names_pos: list of position of the first character of eachfun_names. Compartment names indicate the code line number of the functions incode. -
code_line_nb: vector of integers of the code line numbers of code for each non empty compartment offun_namesandfun_names_pos. -
internal_fun_names: vector of string of names of internal functions in the code of the tested function. -
arg_user_setting: list of argument user settings of the tested function.
Author(s)
Haiding Wang
Yushi Han
Has Odd Number of Quotes
Description
Determine if a string has a odd number of quotes.
Usage
.has_odd_number_of_quotes(input_string, pattern, lib_path, error_text)
Arguments
input_string |
Single string. |
pattern |
Either |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Details
The function considers any present ' (pattern = "'\''") or " (pattern = '"'). Meaning that it does not treat special situation, like escape quotes \", or situation like this "'". All these must be removed from the input_string if necessary, using for instance: input_string <- gsub( x = input_string, pattern = '"\'"', replacement = ' ')
Warnings:
requires
saferDev::arg_check.In the safer Backbone section
"######## check of the required functions from the required packages", add alsosaferDev::arg_checkwhen checking for the presence of.has_odd_number_of_quotes.
Value
TRUE or FALSE.
Author(s)
Haiding Wang
Yushi Han
Replacement Inside Parenthesis
Description
Parse a string and replace any pattern present between brackets () by another pattern.
Usage
.in_parenthesis_replacement(
string,
pattern,
no_regex_pattern,
replacement,
perl,
open_pos,
close_pos,
lib_path,
error_text
)
Arguments
string |
Single string. |
pattern |
Single string indicating the pattern to detect. Warning: must be very simple pattern, like |
no_regex_pattern |
Same as |
replacement |
Single string for |
perl |
Single logical value. Use Perl regex in |
open_pos |
Single positive and non zero integer indicating the position of the opening parenthesis. |
close_pos |
Single positive and non zero integer indicating the position of the closing parenthesis. |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Details
Warnings:
Must be very simple pattern, like
"\(".requires
saferDev::arg_check.In the safer Backbone section
"######## check of the required functions from the required packages", add alsosaferDev::arg_checkwhen checking for the presence of.in_parenthesis_replacement.
Value
A list containing:
-
string: the input string with all pattern replaced by the replacement pattern. -
pos: the positions of the 1st character of the replaced pattern.NULLif no replaced pattern. In that case,stringis identical to the input string
Author(s)
Haiding Wang
Yushi Han
Replacement Inside Quotes
Description
Parse a string and replace any pattern inside simple or double quotes by another replacement pattern and get the position of replacements.
Usage
.in_quotes_replacement(
string,
pattern,
no_regex_pattern,
replacement,
perl,
lib_path,
error_text
)
Arguments
string |
Single string. |
pattern |
Single string indicating the pattern to detect. Warning: must be very simple pattern, like |
no_regex_pattern |
Same as |
replacement |
Single string for |
perl |
Single logical value. Use Perl regex in |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Details
The function works first by replacing in the code: 1) the three consecutive characters '"' or '"' by three spaces, 2) "\"'" and '\'"' by four spaces and 3) escape quotes like \" or \' by two spaces, before looking for the pattern inside quotes.
Warnings:
Must be very simple pattern, like
"\(".requires
saferDev::arg_check,.has_odd_number_of_quotes.In the safer Backbone section
"######## check of the required functions from the required packages", add also these 2 functions when checking for the presence of.in_quotes_replacement.
Value
A list containing:
-
string: The input string with all pattern replaced by the replacement pattern. -
pos: vector of the positions of the 1st character of the replaced pattern.NULLif no replaced pattern. In that case,stringis identical to the input string.
Author(s)
Haiding Wang
Yushi Han
No-Clean Functions
Description
Indicate if function names are inside quotes or after $.
Usage
.noclean_functions(col1, col2, col3, ini, lib_path, error_text)
Arguments
col1 |
Vector of integers of line numbers in a code. |
col2 |
Vector of strings of the function names scrutinized. |
col3 |
Vector of strings of the code before the function name. |
ini |
Vector of strings of the initial function code analyzed. |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Details
Warnings:
requires
saferDev::arg_check,.has_odd_number_of_quotes.In the safer Backbone section
"######## check of the required functions from the required packages", add also these 2 functions when checking for the presence of.noclean_functions.
Value
A logical vector indicating if function names of col2 are inside quotes or after $ (TRUE) in ini or not (FALSE). Can be length 0.
Author(s)
Haiding Wang
Yushi Han
Package and Function Check
Description
Check if 1) required functions are present in required packages and 2) required packages are installed locally.
Simplified version of saferDev::is_function_here(), used as internal function for the other functions of the saferDev package.
Usage
.pack_and_function_check(fun, lib_path, error_text)
Arguments
fun |
Character vector of the names of the required functions, preceded by the name of the package they belong to, separated by a double or triple colon. Example: |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Value
An error message if at least one of the checked packages is missing in lib_path, or if at least one of the checked functions is missing in the required package, nothing otherwise.
Author(s)
Haiding Wang
Yushi Han
All Arguments Here
Description
Verify that all the functions used inside a function are written with all their arguments. For instance: base::paste0(letters[1:2], collapse = NULL, recycle0 = FALSE) and not paste0(letters[1:2]).
Usage
all_args_here(
x,
export = FALSE,
out_path = ".",
df_name = "res.tsv",
overwrite = FALSE,
safer_check = TRUE,
lib_path = NULL,
error_text = ""
)
Arguments
x |
Function name, written without quotes and brackets. |
export |
Single logical value. Export the result data frame into a |
out_path |
Single character string indicating the absolute pathway of the folder where to export the data frame. |
df_name |
Single character string indicating the name of the exported data frame file. Ignored if export is |
overwrite |
Single logical value. Overwrite potential |
safer_check |
Single logical value. Perform the "safer" checks? If |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Details
More precisely, all_args_here() verifies that all the strings before an opening bracket ( are written with all their arguments. Thus, it cannot check function names written without brackets, like in the FUN argument of some functions, e.g., sapply(1:3, FUN = as.character).
The Perl regex used to detect a function name is: "([a-zA-Z]|\.[a-zA-Z._])[a-zA-Z0-9._]*\s*\(".
Currently, all_args_here() cannot detect functions written between quotes, like "+"() or "rownames<-"(x, "a").
Function names preceded by $ are not considered.
The following R functions are skipped: function, if, for, while, repeat and else.
Most of the time, all_args_here() does not check inside comments, but some unexpected writing could dupe all_args_here(). Please, report here if it is the case.
The returned line numbers are indicative, depending on which source is checked. For instance, saferDev::report (compiled) has not the same line numbers as its source file. Notably, compiled functions do not have comments anymore, compared to the same source function sourced into the working environment. In addition, the counting starts at the "<- function" line, i.e., without counting the #' header lines potentially present in source files.
The function works first by replacing in the code: 1) the three consecutive characters '"' or '"' by three spaces, 2) "\"'" and '\'"' by four spaces and 3) escape quotes like \" or \' by two spaces.
See more examples here.
Warnings:
The following R functions are also skipped (as indicated by
"SKIPPED"in theDEF_ARGScolumn of the returned data frame):as.environment(). Click to see the explanation.Some functions, like
rownames(), have different arguments depending on whether something is assigned to it (e.g.,rownames(x) <- "a") or not. Theall_args_here()function always proposes the arguments defined in the help page without assignment, meaning that it cannot detect the assignment. A way to bypass this is to use the "exact" writing of the function. For instance,base::"rownames<-"(x, "a")instead of usingbase::rownames(x) <- "a", and usegetAnywhere("rownames<-")to see the arguments of the function.The function could not properly work if any comma is present in default argument values. Please, report here if it is the case.
Proposals in the
STATUScolumn are only suggestions, as it is difficult to anticipate all the exceptions with arguments writing.
Value
A data frame indicating the missing arguments or a message saying that everything seems fine.
If export argument is TRUE, then the data frame is exported as res.tsv instead of being returned.
Column descriptions:
-
$LINE_NB: the line number in the function code (starting at the"<- function"line, i.e., without counting the#'header lines). -
$FUN_NAME: the function name. -
$FUN_ARGS: the written arguments ofFUN_NAME."NOT_CONSIDERED"means that the function is between quotes or after$. -
$FUN_POS: the position of the first character of the function name in the$LINE_NBline. -
$DEF_ARGS: the defaults arguments ofFUN_NAME."NO_ARGS"means that the function has no arguments."INTERNAL_FUNCTION"means that the function has been created inside the checked function."SKIPPED"means that the function is not analyzed for the reason indicated in the "details" section. -
$MISSING_ARG_NAMES: the missing argument names inFUN_ARGS. -
$MISSING_ARGS: the missing arguments with their values inFUN_ARGS. -
$STATUS: either"GOOD", meaning that all the arguments are already written, or a new proposal of arguments writing, or indicates if some arguments are not fully written (abbreviation is discouraged), or nothing.
An additional message "EVERYTHING SEEMS CLEAN" if the STATUS column is only made of "" and "GOOD".
Author(s)
Haiding Wang
Yushi Han
Examples
# Warning: these examples may not work well when using the "Run examples" link
# because of a particular environment. Please, copy-paste in a local environment.
# See also https://safer-r.github.io/saferDev/articles/all_args_here.html
## Not run:
# Example that returns an error
saferDev::all_args_here(mean) # Example that returns an error
source("https://raw.githubusercontent.com/safer-r/saferDev/main/dev/other/test2.R")
saferDev::all_args_here(test2) # the checked function must be executable
## End(Not run)
FUN2 <- function(x, y){
middle_bracket2 <- base::do.call(
what = base::c,
args = code_for_col,
quote = FALSE,
envir = base::parent.frame()
)
}
saferDev::all_args_here(FUN2, safer_check = FALSE)
## Not run:
# Example that creates a file/folder in the working directory
source("https://raw.githubusercontent.com/safer-r/.github/refs/heads/main/profile/backbone.R")
saferDev::all_args_here(BACKBONE, export = TRUE, safer_check = FALSE)
## End(Not run)
Argument Check
Description
Check expected values of an argument of functions: class, type, mode, length, restricted values panel, kind of numeric values (in addition to the distinction between 'integer' and 'double')? Proportion only? Inf values authorized? negative values authorized? Integers of type 'double'?
Usage
arg_check(
data,
class = NULL,
typeof = NULL,
mode = NULL,
length = NULL,
prop = FALSE,
double_as_integer_allowed = FALSE,
options = NULL,
all_options_in_data = FALSE,
na_contain = FALSE,
neg_values = TRUE,
inf_values = TRUE,
print = FALSE,
data_name = NULL,
data_arg = TRUE,
safer_check = TRUE,
lib_path = NULL,
error_text = ""
)
Arguments
data |
Object to test. |
class |
Single character string among |
typeof |
Single character string among |
mode |
Single character string among |
length |
Single numeric value indicating the length of the object. Not considered if |
prop |
Single logical value. Are the numeric values between 0 and 1 (proportion)? If |
double_as_integer_allowed |
Single logical value. If |
options |
Vector of character strings or integers indicating all the possible option values for the |
all_options_in_data |
Single logical value. If |
na_contain |
Single logical value. Can the |
neg_values |
Single logical value. Are negative numeric values authorized? Warning: the default setting is |
inf_values |
Single logical value. Are infinite |
print |
Single logical value. Print the message if the |
data_name |
Single character string indicating the name of the object to test. If |
data_arg |
Single logical value. Is the tested object a function argument? If |
safer_check |
Single logical value. Perform some "safer" checks? If |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Details
If options == NULL, then at least class or type or mode or length argument must be non-null.
If options is non-null, then class, type and mode must be NULL, and length can be NULL or specified.
The function tests what is written in its arguments, even if what is written is incoherent. For instance, arg_check(data = factor(1), class = "factor", mode = "character") will return a problem, whatever the object tested in the data argument, because no object can be class "factor" and mode "character" (factors are class "factor" and mode "numeric"). Of note, the length of object of class "environment" is always 0.
If the tested object is NULL, then the function will always return a checking problem.
The class argument is a simplified version of class(data):
-
"vector"tests objects of class"numeric","integer","character","logical","complex"or"expression"(no error ifclass(data)returns one of these values). -
"matrix"tests objects of class"matrix"for R < 4.0.0 andc(""matrix", "array"")otherwise. -
"ggplot2"tests objects of classc("gg", "ggplot")for R < 4.5 andc("ggplot2::ggplot", "ggplot", "ggplot2::gg", "S7_object", "gg")otherwise. -
"ggplot2"tests objects of class"ggplot_built"for R < 4.5 andc("ggplot2::ggplot_built", "ggplot_built", "ggplot2::gg", "S7_object")otherwise.
Regarding typeof, the value "object" comes from the fact that if an object has type OBJSXP and has the S4 object flag set (IS_S4_OBJECT(x) is true), typeof() returns "S4". If an object has type OBJSXP without the S4 flag set, typeof() returns "object".
Value
A list containing:
-
problem: logical. Is there any problem detected? -
text: message indicating the details of the problem, or the absence of problem. -
object.name: value of thedata_nameargument (i.e., name of the checked object if provided,NULLotherwise).
Author(s)
Haiding Wang
Yushi Han
See Also
Examples
# Warning: these examples may not work well when using the "Run examples" link
# because of a particular environment. Please, copy-paste in a local environment.
# See also https://safer-r.github.io/saferDev/articles/arg_check.html
test <- matrix(1:3)
## Not run:
# Example that returns an error
saferDev::arg_check(data = test, print = TRUE, class = "vector", mode = "numeric")
## End(Not run)
saferDev::arg_check(data = test, print = TRUE, class = "matrix", mode = "numeric")
saferDev::arg_check(
data = test,
print = TRUE,
class = "matrix",
mode = "numeric",
error_text = " using saferDev::arg_check()"
)
Colons Check
Description
Verify that all the functions used inside a function are preceded by a package attribution. For instance: base::mean() and not mean(), or saferDev:::.base_op_check() and not .base_op_check(). Warning: does not check that the package is the good one. Use all_args_here() for that.
Usage
colons_check(x, safer_check = TRUE, lib_path = NULL, error_text = "")
Arguments
x |
a function name, written without quotes and brackets. |
safer_check |
Single logical value. Perform some "safer" checks? If |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Details
The result is used to modify the code inside the tested function like this: <PACKAGE>::<FUNCTION> (or <PACKAGE>:::<FUNCTION> for function names starting by a dot).
More precisely, colons_check() verifies that all the strings before an opening bracket ( are preceded by ::.
::: are not checked per se, because incorrect writing, like saferDev::.colons_check_message() returns an error when executing the tested function, and because base:::sum() is as ok as base::sum(). In the same manner, more than three colons are not checked because it returns an error when executing the tested function.
Warning: colons_check() cannot check function names written without brackets, like in the FUN argument of some functions, e.g., sapply(1:3, FUN = as.character).
The Perl regex used to detect a function name is: "([a-zA-Z]|\.[a-zA-Z._])[a-zA-Z0-9._]*\s*\(".
Currently, colons_check() cannot detect functions written between quotes, like "+"() or "rownames<-"(x, "a").
Function names preceded by $ are not considered.
The following R functions are skipped: "function", "if", "for", "while", "repeat" and "else".
Most of the time, colons_check() does not check inside comments, but some unexpected writing could dupe colons_check(). Please, report here if it is the case.
The returned line numbers are indicative, depending on which source is checked. For instance, saferDev::report (compiled) has not the same line numbers as its source file (source file). Notably, compiled functions do not have comments anymore, compared to the same source function sourced into the working environment. In addition, the counting starts at the "<- function" line, i.e., without counting the #' header lines potentially present in source files.
Of note, during package creation, the devtools::check() command tells which functions where wrongly attributed to package. Example:
checking dependencies in R code ... WARNING
'::' or ':::' import not declared from: 'sbase'
Missing or unexported objects:
'base::dev.off' 'base::graphics.off' 'base::hcl' 'base::par' 'base::read.table' 'saferGG::report'
Value
A table-like text message indicating the missing :: or ::: or a message saying that everything seems fine.
Table-like: column 1, the line number in the function code (starting at the "<- function" line, i.e., without counting the #' header lines); column 2, the function name; column 3, the code preceding the function name.
With missing :: or :::, the message also indicates if internal functions are created inside the checked function code, since these functions cannot have :: or :::.
Author(s)
Haiding Wang
Yushi Han
Examples
# Warning: these examples may not work well when using the "Run examples" link
# because of a particular environment. Please, copy-paste in a local environment.
# See also https://safer-r.github.io/saferDev/articles/colons_check.html
saferDev::colons_check(mean)
saferDev::colons_check(colons_check)
source("https://raw.githubusercontent.com/safer-r/saferDev/main/dev/other/test.R")
saferDev::colons_check(test)
Environment Check
Description
Verify that object names in the environment defined by the pos parameter are identical or not to object names in the above environments (following R Scope). This can be used to verify that names used for objects inside a function or in the working environment do not override names of objects already present in the above R environments, following the R scope.
Usage
env_check(
pos = 1,
name = NULL,
safer_check = TRUE,
lib_path = NULL,
error_text = ""
)
Arguments
pos |
Single non null positive integer indicating the position of the environment checked (argument |
name |
Single character string of the name of the checked environment (used in the output string only, where the name of the ckecked env is replaced by the name value). Can be used when |
safer_check |
Single logical value. Perform some "safer" checks? If |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Value
A character string indicating the object names of the tested environment that match object names in the above environments, following the R scope, or NULL if no match.
Author(s)
Haiding Wang
Yushi Han
See Also
Examples
# Warning: these examples may not work well when using the "Run examples" link
# because of a particular environment. Please, copy-paste in a local environment.
# See also https://safer-r.github.io/saferDev/articles/env_check.html
# Examples in the working environment
# creation of the object mean with value 1 in the .GlobalEnv environment,
# knowing that the mean() function also exists in the environment base, above .GlobalEnv:
mean <- 1
# creation of the object t.test with value 1 in the .GlobalEnv environment,
# knowing that the t.test() function also exists in the environment stats, above .GlobalEnv:
t.test <- 1
search() # current R scope (order of the successive R environments).
utils::find("mean") # where the objects with the name "mean" are present.
utils::find("t.test") # where the objects with the name "mean" are present.
# test if any object name of the global environment are above environments:
a <- saferDev::env_check(pos = 1)
a # output string.
cat(a)
# test if any object of the stats environment (one step above .GlobalEnv)
# are in upper environments of stats. Returns NULL since no object names of
# stats are in upper environments:
saferDev::env_check(pos = 2)
rm(mean)
rm(t.test)
# Examples inside a function
# saferDev::env_check() checks if the object names inside the fun1 function
# exist in the .GlobalEnv environment and above:
fun1 <- function() {
t.test <- 0
mean <- 5
saferDev::env_check(pos = 1)
}
a <- fun1()
cat(a) # Warning: cat(fun1()) creates an additional layer of environment.
# saferDev::env_check() checks if the object names inside the environment one step above fun2(),
# here .GlobalEnv, exist in the upper environments of .GlobalEnv:
fun2 <- function() {
sum <- 0
saferDev::env_check(pos = 2)
}
fun2()
# Warning: cat(fun2()) does not return NULL, because the environment tested is not
# anymore .GlobalEnv but inside cat().
a <- fun2()
cat(a) # nothing displayed because fun2() returns NULL
# With the name of the function fun3 indicated in the message:
fun3 <- function() {
t.test <- 0
mean <- 5
saferDev::env_check(pos = 1, name = "fun3")
}
a <- fun3()
cat(a)
# Alternative way:
# sys.calls() gives the name of the imbricated functions and
# sys.calls()[[length(sys.calls())]] the name of the function one step above.
fun4 <- function() {
t.test <- 0
mean <- 5
name <- as.character(sys.calls()[[length(sys.calls())]])
saferDev::env_check(pos = 1, name = name)
}
a <- fun4()
cat(a)
# A way to have the name of the tested environment according to test.pos value:
fun7 <- function() {
min <- "VALUE"
fun8 <- function() {
test.pos <- 1 # value 1 tests the fun8 env, 2 tests the fun7 env.
range <- "VALUE"
name <- if(length(sys.calls()) >= test.pos) {
as.character(sys.calls()[[length(sys.calls()) + 1 - test.pos]])
} else {
search()[(1:length(search()))[test.pos - length(sys.calls())]]
}
saferDev::env_check(pos = test.pos, name = name)
}
fun8()
}
a <- fun7()
cat(a)
Get Message
Description
Evaluate an instruction written between "" and return the first of the error message, or the last of the warning or standard (non error non warning) messages if ever exist.
Using argument print_no = FALSE, return NULL if nothing reported, which is convenient in some cases.
Usage
get_message(
data,
kind = "error",
header = TRUE,
print_no = FALSE,
text = NULL,
env = NULL,
safer_check = TRUE,
lib_path = NULL,
error_text = ""
)
Arguments
data |
Single character string of an instruction to evaluate |
kind |
Single character string. Either |
header |
Single logical value. Add a header in the returned message? |
print_no |
Single logical value. Print a message saying that nothing ( |
text |
Single character string added to the header of the output message, even if nothing reported ( |
env |
An object corresponding to an existing environment. Then the |
safer_check |
Single logical value. Perform some "safer" checks? If |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Details
Warnings:
Only the first error or last warning/standard message is returned.
Always use the
envargument whenget_message()is used inside functions.The function does not prevent printing, for instance if
print()orshow()is used inside the instruction tested. To prevent that, usetempo <- utils::capture.output(error <- get_message(data = "arg_check(data = 'a', class = mean, neg_values = FALSE, print = TRUE)")). The return ofget_message()is assigned intoerrorand the printed messages are captured byutils::capture.output()and assigned intotempo. See the examples.
The env argument is used as value for the envir argument of the base::eval() function.
Value
The function returns the error, warning, or standard message, as a single character string, depending on what has been selected using the kind argument and if such message exists.
NULL if no selected message returned and the print_no argument is FALSE.
The following message if no selected message returned and the print_no argument is TRUE: "NO (ERROR|WARNING|STANDARD) MESSAGE REPORTED".
The following message if 1) the kind argument is "warning" or "message" while an error message is returned and 2) the print_no argument is TRUE: "NO POTENTIAL (WARNING|STANDARD) MESSAGE BECAUSE OF ERROR MESSAGE REPORTED".
Author(s)
Haiding Wang
Yushi Han
See Also
try.
Examples
# Warning: these examples may not work well when using the "Run examples" link
# because of a particular environment. Please, copy-paste in a local environment.
# See also https://safer-r.github.io/saferDev/articles/get_message.html
# Report error message by default. Here no error to report:
saferDev::get_message(data = "wilcox.test(c(1,1,3), c(1, 2, 4), paired = TRUE)")
# Warning message to report:
saferDev::get_message(data = "wilcox.test(c(1,1,3), c(1, 2, 4), paired = TRUE)", kind = "warning")
# No standard message to report:
saferDev::get_message(data = "wilcox.test(c(1,1,3), c(1, 2, 4), paired = TRUE)", kind = "message",
print_no = TRUE, text = "IN A")
# Standard message cannot be caught because wilcox.test() returns an error message:
saferDev::get_message(data = "wilcox.test()", kind = "message", print_no = TRUE, text = "IN A")
# Text added in warning message to report:
saferDev::get_message(data = "wilcox.test()", kind = "error", print_no = TRUE, text = "IN A")
# No error message to report with text added:
saferDev::get_message(data = "sum(1)", kind = "error", print_no = TRUE, text = "IN A")
# No error message to report with text added:
saferDev::get_message(data = "message('ahah')", kind = "error", print_no = TRUE, text = "IN A")
# Standard message reported with text added:
saferDev::get_message(data = "message('ahah')", kind = "message", print_no = TRUE, text = "IN A")
# Messages from ggplot2 functions caugth:
saferDev::get_message(data = "ggplot2::ggplot(data = data.frame(X = 1:10, stringsAsFactors = TRUE),
mapping = ggplot2::aes(x = X)) + ggplot2::geom_histogram()", kind = "message",
print_no = TRUE, text = "IN INSTRUCTION 1")
Is Function Here
Description
Check if required functions are present in installed packages. This controls the potential modifications of package contents.
Usage
is_function_here(fun, safer_check = TRUE, lib_path = NULL, error_text = "")
Arguments
fun |
Character vector of the names of the required functions, preceded by the name of the package they belong to and a double or triple colon. Example: |
safer_check |
Single logical value. Perform some "safer" checks? If |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Value
An error message if at least one of the checked packages is missing in lib_path, or if at least one of the checked functions is missing in the required package, nothing otherwise.
Author(s)
Haiding Wang
Yushi Han
See Also
exists and findFunction
Examples
# Warning: these examples may not work well when using the "Run examples" link
# because of a particular environment. Please, copy-paste in a local environment.
# See also https://safer-r.github.io/saferDev/articles/is_function_here.html
## Not run:
# Example that returns an error
saferDev::is_function_here(fun = "ggplot2::notgood", error_text = " INSIDE P1::F1")
saferDev::is_function_here(fun = c("ggplot2::geom_point", "grid::gpar"))
saferDev::is_function_here(fun = "c")
## End(Not run)
saferDev::is_function_here(fun = "base::c", error_text = " INSIDE P1::F1")
Is Package Here
Description
Check if required packages are installed locally.
Usage
is_package_here(
req_package,
safer_check = TRUE,
lib_path = NULL,
error_text = ""
)
Arguments
req_package |
Character vector of package names to check. |
safer_check |
Single logical value. Perform some "safer" checks? If |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Value
An error message if at least one of the checked packages is missing in lib_path, nothing otherwise.
Author(s)
Haiding Wang
Yushi Han
See Also
Examples
# Warning: these examples may not work well when using the "Run examples" link
# because of a particular environment. Please, copy-paste in a local environment.
# See also https://safer-r.github.io/saferDev/articles/is_package_here.html
## Not run:
# Example that returns an error
is_package_here(req_package = "nopackage", error_text = " INSIDE P1::F1")
# Example that returns an error
is_package_here(req_package = "ggplot2", lib_path = "NOTGOOD")
## End(Not run)
is_package_here(req_package = "ggplot2")
Report
Description
Print a character string or a data object into a same output file. Convenient for log file generation.
Usage
report(
data,
output = "log.txt",
path,
append = FALSE,
rownames_kept = FALSE,
vector_cat = FALSE,
noquote = TRUE,
sep = 2,
safer_check = TRUE,
lib_path = NULL,
error_text = ""
)
Arguments
data |
Object to print in the output file. If |
output |
Single character string. Name of the output file. |
path |
Single character string indicating the path where to write the output file. |
append |
Single logical value. If the output file already exists and |
rownames_kept |
Single logical value. Defines whether row names have to be removed or not in 2D objects. Warning: in 1D tables, names over the values are taken as row names, and are thus removed if |
vector_cat |
Single logical value. If |
noquote |
Single logical value. If |
sep |
Single non null and positive integer representing the number of empty lines added in the output file after the printed data. |
safer_check |
Single logical value. Perform some "safer" checks? If |
lib_path |
Vector of characters specifying the absolute pathways of the directories containing the required packages for the function, if not in the default directories. Useful when R packages are not installed in the default directories because of lack of admin rights. More precisely, |
error_text |
Single character string used to add information in error messages returned by the function, notably if the function is inside other functions, which is practical for debugging. Example: |
Value
Nothing (implementation of an existing file) or a text file.
Author(s)
Haiding Wang
Yushi Han
See Also
Examples
# Warning: these examples may not work well when using the "Run examples" link
# because of a particular environment. Please, copy-paste in a local environment.
# See also https://safer-r.github.io/saferDev/articles/report.html
## Not run:
# Example that creates a file/folder in the working directory
saferDev::report(data = "THE FOLLOWING VECTOR IS:\n", output = "results.txt", path = ".",
append = TRUE, sep = 1)
saferDev::report(data = 1:3, output = "results.txt", path = ".", append = TRUE,
rownames_kept = FALSE, vector_cat = FALSE, noquote = FALSE, sep = 2)
saferDev::report(data = "THE FOLLOWING MATRIX IS:\n", output = "results.txt", path = ".",
append = TRUE, sep = 1)
saferDev::report(data = matrix(1:5), output = "results.txt", path = ".", append = TRUE,
rownames_kept = FALSE, vector_cat = FALSE, noquote = FALSE, sep = 5)
saferDev::report(data = "THE FOLLOWING DATA FRAME IS:\n", output = "results.txt", path = ".",
append = TRUE, sep = 1)
saferDev::report(data = data.frame(A = 1:8, B = letters[1:8]), output = "results.txt", path = ".",
append = TRUE, rownames_kept = FALSE, vector_cat = FALSE, noquote = FALSE, sep = 1)
## End(Not run)