[英]I placed a function into my .Rprofile file, but it doesn't appear when I load R? I don't understand why
我的.Rprofile文件如下所示:
# Will be run at the start of an R sesssion
# -----------------------------------------
.First <- function(){
# Set the default CRAN
CRAN <- "http://cran.ma.imperial.ac.uk/"
# Load standard libraries
library(Hmisc)
# My functions
# ------------
# R version of lookfor: `names(data)[grep('pattern',names(data))]`
lkf <- function(d,p) names(d)[grep(p,names(d))]
cat("\nWelcome at", date(), "\n")
}
# Will be run at the end of an R sesssion
# -----------------------------------------
.Last <- function(){
cat("\nGoodbye at ", date(), "\n")
}
我使用lkf
函数快速查找特定数据lkf
的变量。 它以Stata lookfor
函数为模型。 但是,当我启动R时,它对我不可用。
例如
> ls()
[1] "fresh_install"
> mydata <- data.frame(id = c(1:10))
> ls()
[1] "fresh_install" "mydata"
> lkf(mydata,"id")
Error: could not find function "lkf"
我做错了什么?
从?Startup
,
接下来,如果在搜索路径上找到了
.First
函数,则将其作为.First()
执行.First()
因此,与其他任何函数一样, lkf
将在函数环境中创建,并在函数退出时销毁。
相反,您可以在.First
之外定义lkf
:
lkf <- function(d,p) grep(p, names(d), value = TRUE)
.First <- function() {
[...]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.