Updated: 2023-06-09
Things that I find it useful during my work:
Debuging warnings
Sometimes it’s nice to know where in the code that generate warnings especially
when it’s related to NA introduced by coercion
. To do this we can change the
options to make warning as error.
options(warn = 2)
RStudio
Though I don’t use RStudio as my main IDE, it’s good to know that RStudio has
many RStudio specific commands that starts with .rs.*
such as:
.rs.restartR()
Package ..\inst folder
Folder inst
can be used to save data for testthat
or as example when
creating an R package. It’s easier to save it as RDS
format with:
saveRDS(objName, file.path(system.file(package = "pkgName"), "folder/data.rds"))
folder
is the folder inside inst
folder in R package folder structure. To
access the data then:
obj <- readRDS(system.file("folder", "data.rds", package = "pkgName"))
withr package
Good to have this package to isolate options eg. withr::local_options()
.
Evaluate string
When you need to evaluate string or text command, you could use:
-
str2lang()
-
str2expression()
-
paste(text)
Sometime this is needed when programming where input comes from different sources such as Access, SQLite etc. Example to use it:
txt <- "5 + 5"
tot <- eval(str2lang(txt))
## or
tot <- eval(paste(text = txt))
Show all functions in a package
The easiest way to show all functions in a package is using:
library(norgeo)
ls("package:norgeo")
This approach require that you have to load the package first. An alternative to
do it without loading the package is by specifying envir
:
ls("package:norgeo", envir = loadNamespace("norgeo"))