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:47

I hate to type the full words 'head', 'summary', 'names' every time, so I use aliases.

You can put aliases into your .Rprofile file, but you have to use the full path to the function (e.g. utils::head) otherwise it won't work.

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

EDIT: to answer your question, you can use the colorout package to have different colors in the terminal. Cool! :-)

查看更多
情到深处是孤独
3楼-- · 2018-12-31 19:49

Here are two functions I find handy for working with windows.

The first converts the \s to /.

.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')
 }

The second opens the working directory in a new explorer window.

getw <- function() {
    suppressWarnings(shell(paste("explorer",  gsub('/', '\\\\', getwd()))))
}
查看更多
梦寄多情
4楼-- · 2018-12-31 19:49

I set my lattice color theme in my profile. Here are two other tweaks I use:

# 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)
查看更多
看风景的人
5楼-- · 2018-12-31 19:50
options(stringsAsFactors=FALSE)

Although I don't actually have that in my .Rprofile, because it might breaks my coauthors' code, I wish it was the default. Why?

1) Character vectors use less memory (but only barely);

2) More importantly, we would avoid problems such as:

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

and

> 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

Factors are great when you need them (e.g. implementing ordering in graphs) but a nuisance most of the time.

查看更多
只若初见
6楼-- · 2018-12-31 19:50

I like saving my R command history and having it available each time I run R:

In the shell or .bashrc:

export R_HISTFILE=~/.Rhistory

in .Rprofile:

.Last <- function() {
        if (!any(commandArgs()=='--no-readline') && interactive()){
                require(utils)
                try(savehistory(Sys.getenv("R_HISTFILE")))
        }
}
查看更多
余欢
7楼-- · 2018-12-31 19:50

Most of my personal functions and loaded libraries are in the Rfunctions.r script

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")')
查看更多
登录 后发表回答