我总是发现其他人的创业简介文件对这门语言既有用又有指导意义。此外,虽然我对Bash和Vim进行了一些定制,但对R没有任何定制。

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


当前回答

下面是将表导出到LaTeX的一小段代码。它将我编写的许多报告的所有列名更改为数学模式。我的. r配置文件的其余部分是相当标准的,上面已经介绍了大部分内容。

# 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)
options(stringsAsFactors=FALSE)

虽然我的. r配置文件中没有这个,因为它可能会破坏我的合作者的代码,但我希望它是默认的。为什么?

1)字符向量使用更少的内存(但只是很少);

2)更重要的是,我们可以避免这样的问题:

> 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

因子在你需要的时候很有用(比如在图中实现排序),但大多数时候都很麻烦。

下面是将表导出到LaTeX的一小段代码。它将我编写的许多报告的所有列名更改为数学模式。我的. r配置文件的其余部分是相当标准的,上面已经介绍了大部分内容。

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

amscols<-function(x){
    colnames(x) <- paste("$", colnames(x), "$", sep = "")
    x
}

这是我的~/。Rprofile,为Mac和Linux设计。

这使得错误更容易被发现。

options(showWarnCalls=T, showErrorCalls=T)

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

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

更多的历史!

Sys.setenv(R_HISTSIZE='100000')

下面是从终端在Mac OSX上运行的(我更喜欢R.app,因为它更稳定,你可以通过目录来组织你的工作;还要确保得到一个好的~/.inputrc)。默认情况下,你会得到一个X11显示,这看起来不太好;这反而给出了一个与GUI相同的石英显示。if语句应该在Mac上从终端运行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")
}

跑龙套的地方。r是我在通量下随机选取的一袋东西。

此外,由于其他人提到了控制台宽度,以下是我如何做到这一点。

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中,因为每次调整终端窗口大小时都必须重新运行它。我有它在util。r那么我只是在必要的时候引用它。

Stephen Turner关于. rprofiles的帖子有几个有用的别名和启动函数。

我发现自己经常使用他的“ht”和“hh”。

#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]