Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 缩进代码行';n';空间_R - Fatal编程技术网

R 缩进代码行';n';空间

R 缩进代码行';n';空间,r,R,要将代码识别为此处的代码,它必须缩进四个空格。这可以手动完成,也可以使用括号图标或记号完成。如果我想通过R来实现这一点呢?显然,这是可以做到的(只需看看Formator)。以最少的代码编写量来实现这一点的方法是什么 那么对于下面的行(一个数据帧和一个函数),使用R将每行缩进正好4个空格的最佳方式(代码量最少)是什么 foo<-function(x,y){ z<-x*y super.z<-z^2 return(super.z) } 背景 options(pro

要将代码识别为此处的代码,它必须缩进四个空格。这可以手动完成,也可以使用括号图标或记号完成。如果我想通过R来实现这一点呢?显然,这是可以做到的(只需看看Formator)。以最少的代码编写量来实现这一点的方法是什么

那么对于下面的行(一个数据帧和一个函数),使用R将每行缩进正好4个空格的最佳方式(代码量最少)是什么

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

options(prompt = "    ")

将导致您在控制台中编写的任何代码被格式化,以便轻松粘贴到此处。但输出在开始时没有四个空格,可能需要不同的解决方法。

Format R:Format R code Automatically
可用于此目的。可用

库(格式化程序)

src这里有一个小函数,它将以StackOverflow兼容的方式格式化对象:

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

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

formatSO我决定今天浪费时间,按照我描述的那样制作这个函数。我已经创建了一个函数,它接受数据帧或函数,并且可以输入或剪切输入到缩进函数中。这将使用空格参数将代码缩进所需的空格数(默认值为四个空格)


缩进和
选项(continue=“”)
显然用4个空格编辑,但似乎在注释中去掉了多个空格…这是最接近的格式,但该方法将数据帧转换为类似dput()的结构,而不是更美观的data.frame格式。对于Emacs+ESS用户,这就像
C-xrt
一样简单。(我想我会提到这一点,作为那些考虑尝试使用Emacs的人的“另一边”的一个例子!)感谢这个例子;一个小问题是:
replace.assign
参数在这个特定示例中没有用处;它用于将
=
替换为

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