我决定去浪费我的时间今天实际上正如我描述的那样完成这个功能。我创建了一个可以获取数据框架或函数的函数,可以将其输入到缩进函数中或键入剪贴板。这会将代码缩小为空格参数所需的空间(默认值为四个空格)。
indent <- function(object = "clipboard", space = 4) {
y <- if (object == "clipboard") {
as.list(readClipboard())
} else {
strsplit(as.vector(object), "[\\n]")
}
spacer <- function(x) paste(paste(rep(" ", space - 2),
collapse = ""), x)
z <- if (object == "clipboard") {
sapply(y, spacer)
} else {
lapply(y, spacer)
}
zz <- as.matrix(as.data.frame(z))
dimnames(zz) <- list(c(rep("", nrow(zz))), c(""))
noquote(zz)
}
#==========================================================
# Test it out!!!!!!
#==========================================================
indent(" id hs.grad race gender age
1 ID1 yes white male 37
2 ID2 yes white male 32
3 ID3 yes asian male 20
4 ID4 no black female 24
5 ID5 no white female 32")
#==========================================================
indent("ascii<-function(x, header=TRUE,...){
name <-textConnection(x)
DF <- read.table(name, header, ...)
close(name)
on.exit(closeAllConnections())
DF
}", space = 10)
#============================================================
# THE NEXT TWO CAN BE CUT AND PASTED WITH THE CLIPBOARD ARG
#============================================================
id hs.grad race gender age
1 ID1 yes white male 37
2 ID2 yes white male 32
3 ID3 yes asian male 20
4 ID4 no black female 24
5 ID5 no white female 32
indent("clipboard")
#============================================================
ascii<-function(x, header=TRUE,...){
name <-textConnection(x)
DF <- read.table(name, header, ...)
close(name)
on.exit(closeAllConnections())
DF
}
indent() #clipboard is the default arg not needed
这是什么目的? –
对于Emacs + ESS用户,这与“C-x r t''一样简单。 (只是觉得我会提到,作为一个例子,对于那些想给Emacs一个尝试的人来说,'驼峰的另一端') –