I want to make an R package called my_package with the following with the following behavior:
library(my_package)
my_package::get_username() # should throw error "no username set"
my_package::set_username("john doe")
my_package::get_username() # should print "john doe"
I'm not sure how to do this. I tried with the following inside an R file test.R and using source('test.R') it works. But I don't know what would be the proper way to do this when creating a package.
pkg_env <- new.env()
pkg_env$username <- NULL
set_username <- function(username) {
pkg_env$username <- username
}
get_username <- function() {
if (is.null(pkg_env$username)) {
print("ERROR")
} else {
print(pkg_env$username)
}
}