是否可以使用RStudio删除R脚本中的所有注释?
我需要将文件缩小到最小大小。但是,此文件已被严重注释。
如果我是对的,则支持REGEX的Rstudio中的搜索和替换功能可能会对此有所帮助。
感谢您的帮助。
我不会使用正则表达式来完成此任务。它可能有效,但仅在简单情况下。考虑以下/tmp/test.R
脚本:
x <- 1 # a comment
y <- "#######"
z <- "# not a comment \" # not \"" # a # comment # here
f <- # a function
function(n) {
for (i in seq_len(n))
print(i)} #...
如您所见,陈述评论真正开始的位置有点复杂。
如果您不介意重新设置代码格式(那么,您表示希望使用最小的代码),请尝试以下操作:
writeLines(as.character(parse("/tmp/test.R")), "/tmp/out.R")
这将给/tmp/out.R
:
x <- 1
y <- "#######"
z <- "# not a comment \" # not \""
f <- function(n) {
for (i in seq_len(n)) print(i)
}
或者,使用formatR
包中的函数:
library(formatR)
tidy_source(source="/tmp/test.R", keep.comment=FALSE)
## x <- 1
## y <- "#######"
## z <- "# not a comment \" # not \""
## f <- function(n) {
## for (i in seq_len(n)) print(i)
## }
顺便说一句,tidy_source
有一个blank
论点,可能与您有关。但是我无法使其与formatR 0.10 + R 3.0.2一起使用...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句