maj*_*jom 12 comments r rstudio
有没有办法使用RStudio删除R脚本中的所有注释?
我需要将文件缩小到可能的最小尺寸.但是,此文件的评论很多.
如果我是对的,那么支持REGEX的Rstudio中的搜索和替换功能可能对此有所帮助.
我感谢任何帮助.
gag*_*ews 18
我不会用正则表达式来完成这项任务.它可能有用,但仅限于简单的情况.请考虑以下/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)} #...
Run Code Online (Sandbox Code Playgroud)
如您所见,说明评论真正开始的位置有点复杂.
如果您不介意重新格式化代码(嗯,您声明您希望尽可能使用最小的代码),请尝试以下操作:
writeLines(as.character(parse("/tmp/test.R")), "/tmp/out.R")
Run Code Online (Sandbox Code Playgroud)
这将给予/tmp/out.R
:
x <- 1
y <- "#######"
z <- "# not a comment \" # not \""
f <- function(n) {
for (i in seq_len(n)) print(i)
}
Run Code Online (Sandbox Code Playgroud)
或者,使用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)
## }
Run Code Online (Sandbox Code Playgroud)
BTW,tidy_source
有一个blank
论点,这可能是你感兴趣的.但我无法使用formatR 0.10 + R 3.0.2 ...