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:55
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)

I do a lot of work from mysql databases, so connecting right away is a godsend. I only wish there was a way of listing the avaialble databases so I wouldn't have to remember all the different names.

查看更多
余生无你
3楼-- · 2018-12-31 19:57

Here's mine. Nothing too innovative. Thoughts on why particular choices:

  • I went with setting a default for stringsAsFactors because I find it extremely draining to pass it as an argument each time I read a CSV in. That said, it has already caused me some minor vexation when using code written on my usual computer on a computer which did not have my .Rprofile. I'm keeping it, though, as the troubles it has caused pale in comparison to the troubles not having it set everyday used to cause.
  • If you don't load the utils package before options(error=recover), it cannot find recover when placed inside an interactive() block.
  • I used .db for my dropbox setting rather than options(dropbox=...) because I use it all the time inside file.path and it saves much typing. The leading . keeps it from appearing with ls().

Without further ado:

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)
    }
}
查看更多
其实,你不懂
4楼-- · 2018-12-31 19:58

Here is mine. It won't help you with the coloring but I get that from ESS and 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
查看更多
公子世无双
5楼-- · 2018-12-31 19:59

Mine is not too fancy:

# 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' )
查看更多
呛了眼睛熬了心
6楼-- · 2018-12-31 20:01
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
}
查看更多
后来的你喜欢了谁
7楼-- · 2018-12-31 20:04

Here's a little snippet for use exporting tables to LaTeX. It changes all the column names to math mode for the many reports I write. The rest of my .Rprofile is pretty standard and mostly covered above.

# Puts $dollar signs in front and behind all column names col_{sub} -> $col_{sub}$

amscols<-function(x){
    colnames(x) <- paste("$", colnames(x), "$", sep = "")
    x
}
查看更多
登录 后发表回答