I have a .Rprofile I have copied from https://www.r-bloggers.com/fun-with-rprofile-and-customizing-r-startup/ However, when I load my R session the functions that are in env$
they don't work and the functions not in env
works perfectly, here is an example:
sshhh <- function(a.package){
suppressWarnings(suppressPackageStartupMessages(
library(a.package, character.only=TRUE)))
}
auto.loads <-c("dplyr", "ggplot2")
if(interactive()){
invisible(sapply(auto.loads, sshhh))
}
.env <- new.env()
attach(.env)
.env$unrowname <- function(x) {
rownames(x) <- NULL
x
}
.env$unfactor <- function(df){
id <- sapply(df, is.factor)
df[id] <- lapply(df[id], as.character)
df
}
message("n*** Successfully loaded .Rprofile ***n")
Once R is loaded I can type sshhh
and it shows the function, but if I type unfactor
it shows object not found
Any help? Should I put all the functions on my workspace???
They functions created in a separate environment are intentionally hidden. This is to protect them from calls to rm(list=ls())
.
From the original article:
[Lines 58-59]: This creates a new hidden namespace that we can store some functions in. We need to do this in order for these functions to survive a call to “rm(list=ls())” which will remove everything in the current namespace. This is described wonderfully in this blog post [1].
To use the unfactor function, you would call .env$unfactor()
.
If you want to make those function available in the global namespace without having to refer to .env
, you can simply leave out the whole .env
part and just add the function the same way you did for the sshhh
function.
[1] http://gettinggeneticsdone.blogspot.com.es/2013/07/customize-rprofile.html