spsUtil
SPS framework come with a plenty of useful general R utility functions, like pretty logging, package namespace checking, URL checking, and more.
Since SPS 1.1, these functions are separated into a supporting package called spsUtil (systemPipeShiny Utility). You can install it from CRAN.
Installation
Read the developer tools main page, not repeating here.
Functions reference manual
In documents, we only highlight some important functions. Please read the reference manuals for details of every function.
Function highlights
logging with msg
basic
Often times in an R function, we want to use some text to inform users the
status and message. We can use functions like message
, warning
, stop
to generate different
levels of information.
{spsUtil} provides some more informative and prettier ways to generate these kind of messages.
## [INFO] 2021-12-15 01:57:33 my message
You can see it starts with a level
information, then a time stamp, and follows the
actual message. By default, it uses the INFO
level, and you can change to whatever
level you want. However, there are 3 keywords that have special meaning.
Levels
- INFO: equals
message
method in native R - WARNING: generates warnings the same as
warning
function - ERROR: generates error the same as
stop
function and will prevent downstream code get evaluated.
If the level is other than these 3, there is no special meaning in R, just cat
the message out.
## [INFO] 2021-12-15 01:57:33 I am info
## Warning: [WARNING] 2021-12-15 01:57:33 I am warning
## Error:
[ERROR] 2021-12-15 01:57:33 I am error
## [MY LEVEL] 2021-12-15 01:57:33 I am random level
Prefix
For the 3 key levels, you can specify the prefix in front of the level text to
over write the default level text INFO
, WARNING
, or ERROR
## [NEW-INFO] 2021-12-15 01:57:33 I am info
## Warning: [MY-WARNING] 2021-12-15 01:57:33 I am warning
## Error:
[STOP] 2021-12-15 01:57:33 I am error
Colors
Colors are automatically enabled if it is supported. If you try all code above in
your terminal or Rstudio, they all have colors. In Rmd, to enable the color,
you need to add the following code chunk. You also need to install the fansi
package.
```{r echo=FALSE, results='asis'}
options(crayon.enabled = TRUE)
old_hooks <- fansi::set_knit_hooks(knitr::knit_hooks, which = c("output", "message", "error", "warning"))
```
The 3 key levels has default colors:
- INFO: blue
- WARNING: orange
- ERROR: red
You can specify colors for your own levels
## Warning: [WARNING] 2021-12-15 01:57:33 I am warning
## Error:
[ERROR] 2021-12-15 01:57:33 I am error
## [SUCCESS] 2021-12-15 01:57:33 oh yeah
## [FAIL] 2021-12-15 01:57:33 oh no
Wrapper
You can use this logging function in your own projects by wrapping it inside a
upper level function, like what we do for spsinfo
, spswarn
, spserror
. They
have SPS-
prefix added, and have some SPS global settings appended.
## Warning: [SPS-WARNING] 2021-12-15 01:57:33 warning
## Error:
[SPS-ERROR] 2021-12-15 01:57:33 stop
To create a simple one for project is very easy. Assume your project is named “My Project”. You can create logging as:
## Warning: [MP-WARNING] 2021-12-15 01:57:33 warning
## Error:
[MP-ERROR] 2021-12-15 01:57:33 error
mute message with quiet
In R, you can easily mute message and warnings with suppressMessages()
, and
suppressWarnings()
, but not so easy with print
or cat
methods. spsUtil::quiet
enables you to mute all these methods or choose what to mute.
timeout
Run expressions with a time limit, stop an expression if it takes too long
## Error: Timout reached
## [1] 123
## [1] "It takes too long"
## Error: Timout reached
## [1] "some final words"
## [1] "runs even success"
## [1] 11
check “empty” values with emptyIsFalse
In R, values like NA
, ""
, NULL
, length(0) is not very meaningful in
condition judgment and will give you errors. Yet, R does not have a native
method to handle these “empty” values in if
like other languages. They are
meaningful in other ways, but in conditions, we may want to turn them to FALSE
.
You can see they all give errors. In other languages (javascript in this example),
these values are often treated as FALSE
.
This is how emptyIsFalse
work. If the input is one of these values, return FALSE
, else TRUE
check missing packages checkNameSpace
In our functions, sometimes we want to have the users to install certain packages
to enable more functionalities, like the DESeq2::lfcShrink
function. Or like
in a Rmd source code, before other people can rerender the document, they must
install certain packages. checkNameSpace
checks all required packages and returns
the missing names.
## Warning: [WARNING] 2021-12-15 01:57:34 These packages are missing from
## CRAN: random_pkg
## [1] "random_pkg"
You can add it to your function to or on the top of your Rmd document to inform your users the missing packages and where to install.
## Warning: [WARNING] 2021-12-15 01:57:34 These packages are missing from
## CRAN: pkg1,pkg2
## Warning: [WARNING] 2021-12-15 01:57:34 These packages are missing from
## Bioconductor: bio_pkg1,bio_pkg2
## Error in eval(expr, envir, enclos): Install packages
Or write your custom warning message:
## Use `install.packages(c('pkg1','pkg2'))` to install CRAN packages
## Use `BiocManager::install(c('bio_pkg1','bio_pkg2'))` to install Bioconductor packages
## Error in eval(expr, envir, enclos): Install packages
Stack methods
Simple stack
A simple stack data structure in R, with supporting of assiocated methods, like push, pop and others.
History stack
Methods for a history stack data structure. It can store history of certain repeating actions. For example, building the back-end of a file/image editor, allow undo/redo actions.
In-line operation
In-place operations like i += 1
with inc(x)
, i -= 1
with inc(x, -1)
,
i *= 2
with mult(x)
, i /= 2
with divi(x)
## [1] 1
## [1] 2
## [1] 1
## [1] 0
## [1] 2
## [1] 4
## [1] 2
## [1] 1
Uniquefy duplicated strings with strUniquefy
Fix duplicated values in a character vector, useful in column names and some ID structures that requires unique identifiers. If any duplicated string is found in the vector, a numeric index will be added after the these strings.
## [1] "1_1" "1_2" "1_3" "2" "3"
## [1] "1" "1_1" "1_2" "2" "3"
## [1] "1(1)" "1(2)" "1(3)" "2" "3"
## [1] "a_1" "b_1" "c" "a_2" "d" "b_2"
check a URL is reachable with checkUrl
Useful if you need make some big HTTP requests.
## [1] TRUE
## Warning: [WARNING] 2021-12-15 01:57:35 Bad url https://
## randomwebsite123.com
## Warning: [WARNING] 2021-12-15 01:57:35 Timeout was reached:
[randomwebsite123.com] Connection timed out after 1001 milliseconds
## [1] FALSE