我決定去浪費我的時間今天實際上正如我描述的那樣完成這個功能。我創建了一個可以獲取數據框架或函數的函數,可以將其輸入到縮進函數中或鍵入剪貼板。這會將代碼縮小爲空格參數所需的空間(默認值爲四個空格)。
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一個嘗試的人來說,'駝峯的另一端') –