专家R用户,你的.R档案里有什么?

我总是发现其他人的启动configuration文件对语言有用和有启发性。 此外,虽然我有一些定制Bash和Vim ,我没有什么R。

例如,我一直想要的一件事是在窗口terminal中input和输出文本的颜色不同,甚至可能是语法高亮。

这是我的。 它不会帮你着色,但我从ESS和Emacs那里得到了…

 options("width"=160) # wide display with multiple monitors options("digits.secs"=3) # show sub-second time stamps r <- getOption("repos") # hard code the US repo for CRAN r["CRAN"] <- "http://cran.us.r-project.org" options(repos = r) rm(r) ## put something this is your .Rprofile to customize the defaults setHook(packageEvent("grDevices", "onLoad"), function(...) grDevices::X11.options(width=8, height=8, xpos=0, pointsize=10, #type="nbcairo")) # Cairo device #type="cairo")) # other Cairo dev type="xlib")) # old default ## from the AER book by Zeileis and Kleiber options(prompt="R> ", digits=4, show.signif.stars=FALSE) options("pdfviewer"="okular") # on Linux, use okular as the pdf viewer 

我讨厌每次input完整的单词'head','summary','names',所以我使用别名。

你可以在你的.Rprofile文件中join别名,但是你必须使用函数的完整path(例如utils :: head),否则它将无法工作。

 # aliases s <- base::summary h <- utils::head n <- base::names 

编辑:回答你的问题,你可以使用颜色包在terminal中有不同的颜色。 凉! 🙂

 options(stringsAsFactors=FALSE) 

虽然在我的.R档案中实际上并没有这个function,因为它可能会破坏我的合着者的代码,所以我希望它是默认的。 为什么?

1)字符向量使用较less的内存(但几乎没有);

2)更重要的是,我们会避免如下问题:

 > x <- factor(c("a","b","c")) > x [1] abc Levels: abc > x <- c(x, "d") > x [1] "1" "2" "3" "d" 

 > x <- factor(c("a","b","c")) > x[1:2] <- c("c", "d") Warning message: In `[<-.factor`(`*tmp*`, 1:2, value = c("c", "d")) : invalid factor level, NAs generated 

当你需要的时候,因素是很好的(例如在图表中实施sorting),但是大部分时间都是令人讨厌的。

这是我的。 我总是使用主要的代码库,并且有代码可以很容易地获得开发包代码。

 .First <- function() { library(graphics) options("repos" = c(CRAN = "http://cran.r-project.org/")) options("device" = "quartz") } packages <- list( "describedisplay" = "~/ggobi/describedisplay", "linval" = "~/ggobi/linval", "ggplot2" = "~/documents/ggplot/ggplot", "qtpaint" = "~/documents/cranvas/qtpaint", "tourr" = "~/documents/tour/tourr", "tourrgui" = "~/documents/tour/tourr-gui", "prodplot" = "~/documents/categorical-grammar" ) l <- function(pkg) { pkg <- tolower(deparse(substitute(pkg))) if (is.null(packages[[pkg]])) { path <- file.path("~/documents", pkg, pkg) } else { path <- packages[pkg] } source(file.path(path, "load.r")) } test <- function(path) { path <- deparse(substitute(path)) source(file.path("~/documents", path, path, "test.r")) } 

我喜欢保存我的R命令历史logging,并使其每次运行R时都可用:

在shell或.bashrc中:

 export R_HISTFILE=~/.Rhistory 

in .Rprofile:

 .Last <- function() { if (!any(commandArgs()=='--no-readline') && interactive()){ require(utils) try(savehistory(Sys.getenv("R_HISTFILE"))) } } 

这里有两个function,我觉得与Windows的工作方便。

第一个将\ s转换为/

 .repath <- function() { cat('Paste windows file path and hit RETURN twice') x <- scan(what = "") xa <- gsub('\\\\', '/', x) writeClipboard(paste(xa, collapse=" ")) cat('Here\'s your de-windowsified path. (It\'s also on the clipboard.)\n', xa, '\n') } 

第二个在新的资源pipe理器窗口中打开工作目录。

 getw <- function() { suppressWarnings(shell(paste("explorer", gsub('/', '\\\\', getwd())))) } 

我有这个更dynamic的技巧来使用完整的terminal宽度,它试图从COLUMNS环境variables(在Linux上)读取:

 tryCatch( {options( width = as.integer(Sys.getenv("COLUMNS")))}, error = function(err) { write("Can't get your terminal width. Put ``export COLUMNS'' in your \ .bashrc. Or something. Setting width to 120 chars", stderr()); options(width=120)} ) 

这样,R即使在调整terminal窗口的时候也会使用全宽度。

我的大部分个人function和加载库都在Rfunctions.r脚本中

 source("c:\\data\\rprojects\\functions\\Rfunctions.r") .First <- function(){ cat("\n Rrrr! The statistics program for Pirates !\n\n") } .Last <- function(){ cat("\n Rrrr! Avast Ye, YO HO!\n\n") } #=============================================================== # Tinn-R: necessary packages #=============================================================== library(utils) necessary = c('svIDE', 'svIO', 'svSocket', 'R2HTML') if(!all(necessary %in% installed.packages()[, 'Package'])) install.packages(c('SciViews', 'R2HTML'), dep = T) options(IDE = 'C:/Tinn-R/bin/Tinn-R.exe') options(use.DDE = T) library(svIDE) library(svIO) library(svSocket) library(R2HTML) guiDDEInstall() shell(paste("mkdir C:\\data\\rplots\\plottemp", gsub('-','',Sys.Date()), sep="")) pldir <- paste("C:\\data\\rplots\\plottemp", gsub('-','',Sys.Date()), sep="") plot.str <-c('savePlot(paste(pldir,script,"\\BeachSurveyFreq.pdf",sep=""),type="pdf")') 

这是我的〜/ .Rprofile ,专为Mac和Linuxdevise的。

这些使错误更容易看到。

 options(showWarnCalls=T, showErrorCalls=T) 

我讨厌CRAN菜单的select,所以设置一个好的。

 options(repos=c("http://cran.cnr.Berkeley.edu","http://cran.stat.ucla.edu")) 

更多的历史!

 Sys.setenv(R_HISTSIZE='100000') 

下面是从terminal上运行的Mac OSX(我非常喜欢R.app,因为它比较稳定,你可以通过目录来组织你的工作,也请确保获得一个好的〜/ .inputrc )。 默认情况下,你会得到一个X11显示,看起来不太好; 这反而给石英显示相同的GUI。 if语句应该是在Mac上的terminal上运行R时遇到的。

 f = pipe("uname") if (.Platform$GUI == "X11" && readLines(f)=="Darwin") { # http://www.rforge.net/CarbonEL/ library("grDevices") library("CarbonEL") options(device='quartz') Sys.unsetenv("DISPLAY") } close(f); rm(f) 

并预装一些库,

 library(plyr) library(stringr) library(RColorBrewer) if (file.exists("~/util.r")) { source("~/util.r") } 

util.r是随机的东西,我使用,在stream量下。

另外,由于其他人提到控制台宽度,这是我怎么做。

 if ( (numcol <-Sys.getenv("COLUMNS")) != "") { numcol = as.integer(numcol) options(width= numcol - 1) } else if (system("stty -a &>/dev/null") == 0) { # mac specific? probably bad in the R GUI too. numcol = as.integer(sub(".* ([0-9]+) column.*", "\\1", system("stty -a", intern=T)[1])) if (numcol > 0) options(width= numcol - 1 ) } rm(numcol) 

这实际上不在.Rprofile因为每次调整terminal窗口时都必须重新运行它。 我有它util.r然后我只是在必要时util.r它。

这是我的:

 .First <- function () { options(device="quartz") } .Last <- function () { if (!any(commandArgs() == '--no-readline') && interactive()) { require(utils) try(savehistory(Sys.getenv("R_HISTFILE"))) } } # Slightly more flexible than as.Date # my.as.Date("2009-01-01") == my.as.Date(2009, 1, 1) == as.Date("2009-01-01") my.as.Date <- function (a, b=NULL, c=NULL, ...) { if (class(a) != "character") return (as.Date(sprintf("%d-%02d-%02d", a, b, c))) else return (as.Date(a)) } # Some useful aliases cd <- setwd pwd <- getwd lss <- dir asd <- my.as.Date # examples: asd("2009-01-01") == asd(2009, 1, 1) == as.Date("2009-01-01") last <- function (x, n=1, ...) tail(x, n=n, ...) # Set proxy for all web requests Sys.setenv(http_proxy="http://192.168.0.200:80/") # Search RPATH for file <fn>. If found, return full path to it search.path <- function(fn, paths = strsplit(chartr("\\", "/", Sys.getenv("RPATH")), split = switch(.Platform$OS.type, windows = ";", ":"))[[1]]) { for(d in paths) if (file.exists(f <- file.path(d, fn))) return(f) return(NULL) } # If loading in an environment that doesn't respect my RPATH environment # variable, set it here if (Sys.getenv("RPATH") == "") { Sys.setenv(RPATH=file.path(path.expand("~"), "Library", "R", "source")) } # Load commonly used functions if (interactive()) source(search.path("afazio.r")) # If no R_HISTFILE environment variable, set default if (Sys.getenv("R_HISTFILE") == "") { Sys.setenv(R_HISTFILE=file.path("~", ".Rhistory")) } # Override q() to not save by default. # Same as saying q("no") q <- function (save="no", ...) { quit(save=save, ...) } # ---------- My Environments ---------- # # Rather than starting R from within different directories, I prefer to # switch my "environment" easily with these functions. An "environment" is # simply a directory that contains analysis of a particular topic. # Example usage: # > load.env("markets") # Load US equity markets analysis environment # > # ... edit some .r files in my environment # > reload() # Re-source .r/.R files in my environment # # On next startup of R, I will automatically be placed into the last # environment I entered # My current environment .curr.env = NULL # File contains name of the last environment I entered .last.env.file = file.path(path.expand("~"), ".Rlastenv") # Parent directory where all of my "environment"s are contained .parent.env.dir = file.path(path.expand("~"), "Analysis") # Create parent directory if it doesn't already exist if (!file.exists(.parent.env.dir)) dir.create(.parent.env.dir) load.env <- function (string, save=TRUE) { # Load all .r/.R files in <.parent.env.dir>/<string>/ cd(file.path(.parent.env.dir, string)) for (file in lss()) { if (substr(file, nchar(file)-1, nchar(file)+1) %in% c(".r", ".R")) source(file) } .curr.env <<- string # Save current environment name to file if (save == TRUE) writeLines(.curr.env, .last.env.file) # Let user know environment switch was successful print (paste(" -- in ", string, " environment -- ")) } # "reload" current environment. reload <- resource <- function () { if (!is.null(.curr.env)) load.env(.curr.env, save=FALSE) else print (" -- not in environment -- ") } # On startup, go straight to the environment I was last working in if (interactive() && file.exists(.last.env.file)) { load.env(readLines(.last.env.file)) } 
 sink(file = 'R.log', split=T) options(scipen=5) .ls.objects <- function (pos = 1, pattern, order.by = "Size", decreasing=TRUE, head = TRUE, n = 10) { # based on postings by Petr Pikal and David Hinds to the r-help list in 2004 # modified by: Dirk Eddelbuettel (http://stackoverflow.com/questions/1358003/tricks-to- manage-the-available-memory-in-an-r-session) # I then gave it a few tweaks (show size as megabytes and use defaults that I like) # a data frame of the objects and their associated storage needs. napply <- function(names, fn) sapply(names, function(x) fn(get(x, pos = pos))) names <- ls(pos = pos, pattern = pattern) obj.class <- napply(names, function(x) as.character(class(x))[1]) obj.mode <- napply(names, mode) obj.type <- ifelse(is.na(obj.class), obj.mode, obj.class) obj.size <- napply(names, object.size) / 10^6 # megabytes obj.dim <- t(napply(names, function(x) as.numeric(dim(x))[1:2])) vec <- is.na(obj.dim)[, 1] & (obj.type != "function") obj.dim[vec, 1] <- napply(names, length)[vec] out <- data.frame(obj.type, obj.size, obj.dim) names(out) <- c("Type", "Size", "Rows", "Columns") out <- out[order(out[[order.by]], decreasing=decreasing), ] if (head) out <- head(out, n) out } 

使data.frames显示有点像'头',而不必键入'头'

 print.data.frame <- function(df) { if (nrow(df) > 10) { base::print.data.frame(head(df, 5)) cat("----\n") base::print.data.frame(tail(df, 5)) } else { base::print.data.frame(df) } } 

( 如何让'head'自动应用于输出? )

我经常有一连串的debugging电话,我需要打电话和取消他们可以是非常繁琐的。 在SO社区的帮助下,我find了以下解决scheme,并将其插入到我的.Rprofile.site# BROWSER在我的Eclipse任务中,以便在“任务视图”窗口中浏览浏览器调用。

 # turn debugging on or off # place "browser(expr = isTRUE(getOption("debug"))) # BROWSER" in your function # and turn debugging on or off by bugon() or bugoff() bugon <- function() options("debug" = TRUE) bugoff <- function() options("debug" = FALSE) #pun intended 

我不太喜欢:

 # So the mac gui can find latex Sys.setenv("PATH" = paste(Sys.getenv("PATH"),"/usr/texbin",sep=":")) #Use last(x) instead of x[length(x)], works on matrices too last <- function(x) { tail(x, n = 1) } #For tikzDevice caching options( tikzMetricsDictionary='/Users/cameron/.tikzMetricsDictionary' ) 
 setwd("C://path//to//my//prefered//working//directory") library("ggplot2") library("RMySQL") library("foreign") answer <- readline("What database would you like to connect to? ") con <- dbConnect(MySQL(),user="root",password="mypass", dbname=answer) 

我从MySQL数据库做了很多工作,所以马上连接是一个天赐之物。 我只希望有一个列出可用数据库的方法,所以我不必记住所有不同的名称。

Stephen Turner在.Rprofiles上的post有几个有用的别名和启动函数。

我发现自己经常使用他的。

 #ht==headtail, ie, show the first and last 10 items of an object ht <- function(d) rbind(head(d,10),tail(d,10)) # Show the first 5 rows and first 5 columns of a data frame or matrix hh <- function(d) d[1:5,1:5] 

我使用以下命令获取cacheSweave(或pgfSweave)以使用RStudio中的“Compile PDF”button:

 library(cacheSweave) assignInNamespace("RweaveLatex", cacheSweave::cacheSweaveDriver, "utils") 

我包括options(menu.graphics=FALSE)因为我喜欢在R中禁用/禁止tcltk popup为CRAN镜像select 。

这是我的。 没有太创新。 思考为什么特定的select:

  • 我去为stringsAsFactors设置了一个默认值,因为我发现它每次读取CSV时都会把它作为一个parameter passing给它。这就是说,当我在计算机上用通常的计算机写的代码时,已经引起了一些麻烦没有我的.R档案。 不过,我保留它,因为它与日常所使用的麻烦相比,已经造成了一些麻烦。
  • 如果在options(error=recover)之前没有加载utils包,则将其置于interactive()块内时无法find恢复。
  • 我用我的Dropbox设置而不是options(dropbox=...) . file.path ,因为我一直在file.path使用它,它节省了很多input。 领先. 使它不会出现ls()

无需再费周折:

 if(interactive()) { options(stringsAsFactors=FALSE) options(max.print=50) options(repos="http://cran.mirrors.hoobly.com") } .db <- "~/Dropbox" # `=` <- function(...) stop("Assignment by = disabled, use <- instead") options(BingMapsKey="blahblahblah") # Used by taRifx.geo::geocode() .First <- function() { if(interactive()) { require(functional) require(taRifx) require(taRifx.geo) require(ggplot2) require(foreign) require(R.utils) require(stringr) require(reshape2) require(devtools) require(codetools) require(testthat) require(utils) options(error=recover) } } 

这是一个使用LaTeX导出表格的小片段。 它将我写的许多报告的所有列名更改为math模式。 我的.Rprofile的其余部分是相当标准的,大部分都在上面。

 # Puts $dollar signs in front and behind all column names col_{sub} -> $col_{sub}$ amscols<-function(x){ colnames(x) <- paste("$", colnames(x), "$", sep = "") x } 

我在我的个人资料中设置了我的格子颜色主题 这里有两个其他的调整我使用:

 # Display working directory in the titlebar # Note: This causes demo(graphics) to fail utils::setWindowTitle(base::getwd()) utils::assignInNamespace("setwd",function(dir) {.Internal(setwd(dir));setWindowTitle(base::getwd())},"base") # Don't print more than 1000 lines options(max.print=2000) 

我有一个环境variablesR_USER_WORKSPACE指向我的包的顶部目录。 在.Rprofile中,我定义了一个函数devlib来设置工作目录(这样data()起作用),并且在R子目录中find所有的.R文件。 这和Hadley的l()函数非常相似。

 devlib <- function(pkg) { setwd(file.path(Sys.getenv("R_USER_WORKSPACE", "."), deparse(substitute(pkg)), "dev")) sapply(list.files("R", pattern=".r$", ignore.case=TRUE, full.names=TRUE), source) invisible(NULL) } .First <- function() { setwd(Sys.getenv("R_USER_WORKSPACE", ".")) options("repos" = c(CRAN = "http://mirrors.softliste.de/cran/", CRANextra="http://www.stats.ox.ac.uk/pub/RWin")) } .Last <- function() update.packages(ask="graphics") 

这是我的,包括一些提到的想法。

你可能想看两件事情:

  • .set.width()/ w()将您的打印宽度更新到terminal的打印宽度。 不幸的是,我没有find一种方法来自动在terminalresize – R的文档提到这是由一些R解释器完成。
  • 历史logging每次与时间戳和工作目录一起保存

 .set.width <- function() { cols <- as.integer(Sys.getenv("COLUMNS")) if (is.na(cols) || cols > 10000 || cols < 10) options(width=100) options(width=cols) } .First <- function() { options(digits.secs=3) # show sub-second time stamps options(max.print=1000) # do not print more than 1000 lines options("report" = c(CRAN="http://cran.at.r-project.org")) options(prompt="R> ", digits=4, show.signif.stars=FALSE) } # aliases w <- .set.width .Last <- function() { if (!any(commandArgs()=='--no-readline') && interactive()){ timestamp(,prefix=paste("##------ [",getwd(),"] ",sep="")) try(savehistory("~/.Rhistory")) } } 

我发现了两个function是非常必要的:首先,当我在几个函数上设置了debug() ,并且我已经解决了这个bug,所以我想要undebug()所有的函数 – 不是一个一个的。 作为接受的答案添加的undebug_all()函数是最好的。

其次,当我定义了许多函数,并且正在寻找一个特定的variables名时,很难在ls()所有结果中find它,包括函数名。 这里发布的lsnofun()函数非常好。