2011-11-18 92 views
4

要在此处将代码识别为代码,它必须缩进四个空格。这可以通过手动或使用括号图标或刻度标记来完成。如果我想通过R完成这个任务呢?显然这可以完成(只看格式R)。用最少的代码编写来做这件事的方式是什么?缩进代码行'n'空格

因此对于下面的行(一个数据框和一个函数),使用R来缩进每行完全4个空格的最佳方式(最少代码量)是多少?

foo<-function(x,y){ 
    z<-x*y 
    super.z<-z^2 
    return(super.z) 
} 

 id hs.grad race gender age 
1 ID1  yes asian female 32 
2 ID2  yes white female 30 
3 ID3  yes white female 34 
4 ID4  yes black female 25 
5 ID5  no white male 19 
+0

这是什么目的? –

+0

对于Emacs + ESS用户,这与“C-x r t ''一样简单。 (只是觉得我会提到,作为一个例子,对于那些想给Emacs一个尝试的人来说,'驼峰的另一端') –

回答

1

我决定去浪费我的时间今天实际上正如我描述的那样完成这个功能。我创建了一个可以获取数据框架或函数的函数,可以将其输入到缩进函数中或键入剪贴板。这会将代码缩小为空格参数所需的空间(默认值为四个空格)。

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 
3

设置

options(prompt = " ") 

会导致你在控制台中写任何代码进行格式化,因此很容易贴在这里。输出在开始时不会有四个空格,但可能需要采用不同的解决方法。

+0

和'options(continue =“”)*编辑*显然有4个空格,但SO似乎在评论中删除了多个空格...... – James

2

formatR: Format R Code Automatically可用于此目的。可用here

library(formatR) 
src <- c("foo<-function(x,y){ 
    z<-x*y 


       super.z<-z^2 
    return(super.z) 
} 
") 

tidy.source(text = src, replace.assign = TRUE) 


foo <- function(x, y) { 
    z <- x * y 
    super.z <- z^2 
    return(super.z) 
} 
+1

感谢这个例子;一个小点:'replace.assign'参数在这个特定的例子中没有用;当'='用于赋值时(例如,'z = x * y'将变为'z <-x * y'),它被用来将'='替换为'<-'。 –

4

这里的一个小功能,将在StackOverflow上兼容的方式格式化对象:

formatSO <- function(x) { 
    y <- get(x, parent.frame()) 
    d <- deparse(y) 

    cat(" ", x, "<-", d[1], "\n") 
    cat(paste(" ", d[-1], "\n", sep=""), sep="") 
} 

,并试图出来:

> foo<-function(x,y){ 
+ z<-x*y 
+ super.z<-z^2 
+ return(super.z) 
+ } 
> formatSO("foo") 
    foo <- function (x, y) 
    { 
     z <- x * y 
     super.z <- z^2 
     return(super.z) 
    } 
> x <- 5:3 
> formatSO("x") 
    x <- c(5L, 4L, 3L) 
+0

这是最接近的格式,但该方法将数据帧转换为其结构(),而不是更美观的data.frame格式。 –