Expert R users, what's in your .Rprofile? [clo

2018-12-31 19:12发布

I have always found startup profile files of other people both useful and instructive about the language. Moreover, while I have some customization for Bash and Vim, I have nothing for R.

For example, one thing I always wanted is different colors for input and output text in a window terminal, and maybe even syntax highlighting.

24条回答
浅入江南
2楼-- · 2018-12-31 19:51

I've got this, more dynamic trick to use full terminal width, which tries to read from the COLUMNS environment variable (on 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)}
)

This way R will use the full width even as you resize your terminal window.

查看更多
时光乱了年华
3楼-- · 2018-12-31 19:52

I found two functions really necessary: First when I have set debug() on several functions and I have resolved the bug, so I want to undebug() all functions - not one by one. The undebug_all() function added as the accepted answer here is the best.

Second, when I have defined many functions and I am looking for a specific variable name, it's hard to find it within all results of the the ls(), including the function names. The lsnofun() function posted here is really good.

查看更多
浅入江南
4楼-- · 2018-12-31 19:53

Here's from my ~/.Rprofile, designed for Mac and Linux.

These make errors easier to see.

options(showWarnCalls=T, showErrorCalls=T)

I hate the CRAN menu choice, so set to a good one.

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

More history!

Sys.setenv(R_HISTSIZE='100000')

The following is for running on Mac OSX from the terminal (which I greatly prefer to R.app because it's more stable, and you can organize your work by directory; also make sure to get a good ~/.inputrc). By default, you get an X11 display, which doesn't look as nice; this instead gives a quartz display same as the GUI. The if statement is supposed to catch the case when you're running R from the terminal on Mac.

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)

And preload a few libraries,

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

where util.r is a random bag of stuff I use, under flux.

Also, since other people were mentioning console width, here's how I do it.

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)

This actually isn't in .Rprofile because you have to re-run it every time you resize the terminal window. I have it in util.r then I just source it as necessary.

查看更多
妖精总统
5楼-- · 2018-12-31 19:53

Mine includes options(menu.graphics=FALSE) because I like to Disable/suppress tcltk popup for CRAN mirror selection in R.

查看更多
低头抚发
6楼-- · 2018-12-31 19:54

Here's mine. I always use the main cran repository, and have code to make it easy to source in-development package code.

.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"))  
}
查看更多
步步皆殇っ
7楼-- · 2018-12-31 19:54

Stephen Turner's post on .Rprofiles has several useful aliases and starter functions.

I find myself using his ht and hh often.

#ht==headtail, i.e., 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]
查看更多
登录 后发表回答