2014-09-27 24 views
5

我从终端复制代码以在此处发布。它的格式如下:如何将代码转换为更具可读性的表单R

> ddf2 = ddf[ddf$stone_ny>'stone',] # this is first command 
> ddf2[!duplicated(ddf2$deltnr),] # second command 
    deltnr us stone_ny stone_mobility  
4 1536 63 stone   mobile 
10 1336 62 stone   mobile 

前两行是命令,后面三行输出。但是,由于命令以'>'开始,因此无法从此处复制到R终端。我如何将其转换为:

ddf2 = ddf[ddf$stone_ny>'stone',] # this is first command 
ddf2[!duplicated(ddf2$deltnr),] # second command 
# deltnr us stone_ny stone_mobility  
#4 1536 63 stone   mobile 
#10 1336 62 stone   mobile 

因此,它变得适合从这里复制。

我想:

text 
[1] "> ddf2 = ddf[ddf$stone_ny>'stone',] # this is first command\n> ddf2[!duplicated(ddf2$deltnr),] # second command\n deltnr us stone_ny stone_mobility  \n4 1536 63 stone   mobile \n10 1336 62 stone   mobile " 


text2 = gsub('\n','#',text) 
text2 = gsub('#>','\n',text2) 
text2 = gsub('#','\n#',text2) 
text2 
[1] "> ddf2 = ddf[ddf$stone_ny>'stone',] \n# this is first command\n 
ddf2[!duplicated(ddf2$deltnr),] \n# second command\n# deltnr us stone_ny stone_mobility  \n#4 1536 63 stone   mobile \n#10 1336 62 stone   mobile " 

但它不能得到粘贴到终端。

+0

如果您使用的是RStudio,您可以在脚本编辑器区域轻松编写代码,并使用Ctrl + Alt + C注释掉结果。 – A5C1D2H2I1M1N2O1R2T1 2014-09-27 01:31:52

+0

Ctrl + Alt + C在我的系统中不起作用(R studio 0.98。 501在Linux Debian稳定版上) – rnso 2014-09-27 01:42:04

+0

也可以看看'Sublime Text 3' +'Enhanced R package'(适用于ST3)。多行编辑令人难以置信 – 2014-09-27 03:50:50

回答

7

我一直在等待分享这个功能的机会,我保留在我的.Rprofile文件中。虽然它可能不会完全回答你的问题,但我觉得它正在完成一些与你之后非常接近的事情。所以你可以通过查看代码来获得一些想法。其他人可能会发现它就像它一样有用。功能:

SO <- function(script.file = '~/.active-rstudio-document') { 

    # run the code and store the output in a character vector 
    tmp <- tempfile() 
    capture.output(
     source(script.file, echo = TRUE, 
          prompt.echo = "> ", 
          continue.echo = "+ "), file = tmp) 
    out <- readLines(tmp) 

    # identify lines that are comments, code, results 
    idx.comments <- grep("^> [#]{2}", out) 
    idx.code  <- grep("^[>+] ", out) 
    idx.blank <- grep("^[[:space:]]*$", out) 
    idx.results <- setdiff(seq_along(out), 
          c(idx.comments, idx.code, idx.blank)) 
    # reformat 
    out[idx.comments] <- sub("^> [#]{2} ", "", out[idx.comments]) 
    out[idx.code]  <- sub("^[>+] ", " ", out[idx.code]) 
    out[idx.results] <- sub("^", " # ", out[idx.results]) 

    # output 
    cat(out, sep = "\n", file = stdout()) 
} 

SO功能是什么可以让我快速格式化我的答案对这个非常网站StackOverflow上的问题。我的工作流程如下:

1)在RStudio中,将我的答案写在一个无标题的脚本中(这是左上象限)。例如:

## This is super easy, you can do 

set.seed(123) 
# initialize x 
x <- 0 
while(x < 0.5) { 
    print(x) 
    # update x 
    x <- runif(1) 
} 

## And voila. 

2)在顶部附近,单击“源”按钮。它会在控制台中执行代码,而不是我们之后的代码:相反,它会将代码保存到默认文件'〜/ .active-rstudio-document'的副作用。

3)运行SO()从控制台(左下象限),这将保存的文件源代码(再次...),捕获输出,并在SO-友好的格式打印:

This is super easy, you can do 

    set.seed(123) 
    # initialize x 
    x <- 0 
    while(x < 0.5) { 
     print(x) 
     # update x 
     x <- runif(1) 
    } 
    # [1] 0 
    # [1] 0.2875775 

And voila. 

4)复制粘贴到stackoverflow并完成。

注意:对于需要一段时间才能运行的代码,可以通过将脚本保存到文件(例如'xyz.R')而不是单击“源”按钮来避免运行它两次。然后运行SO("xyz.R")

+0

这真的很酷,谢谢分享。当我运行它时,我在'print(x)'前面的注释字符一直下到'[1] 0.2875775'我需要调整某些东西吗? – 2014-09-27 03:38:15

+0

嗯。你是否使用默认提示符('“>”'和'“+”')?你可以通过运行getOption(“prompt”)和getOption(“continue”)来检查。通过在'source()'调用中加入'prompt.echo =“>”'和'continue.prompt =“+”',我的函数可以变得更加健壮。无论如何,我会补充一点。 – flodel 2014-09-27 03:49:15

+0

好吧,如你所见,当我重新格式化时,我假设提示符是“>”。因此,无论我使用默认的'prompt.echo = getOption(“prompt”)''来调用'source',但我需要在我的正则表达式中使用'getOption(“prompt”)',或者强制使用'“>”'将'prompt.echo =“>”'添加到源调用中。我去了后者,看到我的编辑。 – flodel 2014-09-27 04:01:54

2

你可以尝试catifelse条件。

cat(ifelse(substr(s <- strsplit(text, "\n")[[1]], 1, 1) %in% c("_", 0:9, " "), 
      paste0("# ", s), 
      gsub("[>] ", "", s)), 
    sep = "\n") 

这导致

ddf2 = ddf[ddf$stone_ny>'stone',] # this is first command 
ddf2[!duplicated(ddf2$deltnr),] # second command 
# deltnr us stone_ny stone_mobility  
# 4 1536 63 stone   mobile 
# 10 1336 62 stone   mobile 

"_"的和0:9在那里,因为在R中的规则之一是一个功能不能与一个_或数字开头。您可以调整它以适应您的需求。

+0

它的工作原理。我会尝试从终端复制的其他文本也。 – rnso 2014-09-27 02:05:07

+0

@rnso - 'cat'还有一个'labels'和一个'fill'参数可能会派上用场。例如'cat(paste(字母,10 * 1:15),fill = TRUE,labels = paste0(“{”,1:15,“}:”))' – 2014-09-27 02:28:03

+0

感谢您的提示。 – rnso 2014-09-27 02:31:22