修复源的缩进并使其清洁

Ali*_*Ali 3 r indentation

有没有合适的程序来修复已编写的R脚本的缩进?

例如,如果它被提供这样的脚本:

foo = function(x) {
a = 1
    print(a)
 }
Run Code Online (Sandbox Code Playgroud)

它将其转换为:

foo = function(x) {
    a = 1
    print(a)
}
Run Code Online (Sandbox Code Playgroud)

或更好?

Dir*_*tel 11

是的,使用Yihui的formatR包.

之前和之后的演示:

R> system("cat /tmp/fex.R")
foo = function(x) {
a = 1
    print(a)
 }
R>
R> library(formatR)
R> tidy.source("/tmp/fex.R",replace.assign=TRUE)
foo <- function(x) {
    a <- 1
    print(a)
} 
R> 
Run Code Online (Sandbox Code Playgroud)

您当然可以使用重定向到新文件 tidy.source(..., file="NewFile.R")