获取R代码段以接受插入文本中的空格 [英] Get R snippet to accept whitespace in inserted text

查看:93
本文介绍了获取R代码段以接受插入文本中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个代码段,以使我能够使用标准格式快速插入评论文本:

I am trying to write a snippet to allow me to quickly insert comment text using my standard format:

#######################################><###################
##  [date and time goes here] ------------------------------
##  [comment goes here, can span multiple
##  lines]
#######################################><###################

这是我到目前为止所得到的:

This is what I've got so far:

snippet comm
    `r paste0(
        "#######################################><###################\n##  ", 
        date(), 
        " -------------------------------\n##  ", 
        eval(
            paste0(
                gsub(
                    ".{1,51}\\s?\\K\\b", 
                    "\n##  ", 
                    gsub("\\.", " ", paste0(text)), 
                    perl = T
                )
            )
        ), 
        "###################################><###################\n"
    )`

此代码段有效,但要求注释文本中不能包含任何空格。作为一种变通方法,我编写了摘录以将解释为空格。

This snippet works, but requires that the comment text not have any spaces in it. As a work around, I've written the snippet to interpret . as a space.

commlong.comment.text.1111111.aaaaaaa.2222222.bbbbbbb.3333333.ccccccc.4444444.ddddddd.5555555.eeeeeee.6666666.fffffff.7777777.ggggggg.8888888.9999999.0000000

#######################################><###################
##  Tue Jul 24 12:40:55 2018 -------------------------------
##  1111111 aaaaaaa 2222222 bbbbbbb 3333333 ccccccc 
##  4444444 ddddddd 5555555 eeeeeee 6666666 fffffff 
##  7777777 ggggggg 8888888 9999999 0000000
##  ###################################><###################

由于用句点而不是空格来输入注释是很痛苦的,所以我d想修改我的代码段以接受带空格的注释文本。任何想法如何做到这一点?谢谢。

Since typing comments with periods instead of spaces is a pain, I'd like to modify my snippet to accept comment text with spaces. Any ideas how to do that? Thanks.

推荐答案

事实证明,这比预期的要容易得多。我发现,代码段中不在刻度`内的任何内容都被解释为好像直接在RStudio的源窗格中键入的一样。 `r ...`中的所有内容都将被评估为好像直接在控制台窗格中键入,但是输出将插入到源窗格中的当前位置(相对到现有文字或周围的摘要)。所以我写了这个代码片段,基本上就是我想要的:

Turns out this was a lot easier than anticipated. What I figured out is that anything in the code of a snippet that is not inside ticks ` is interpreted as if it were typed directly into the source pane of RStudio. Anything inside the `r ... ` is evaluated as if it were typed directly into the console pane, but the output is inserted into the current position in the source pane (relative to the existing text or the surrounding snippet). So I wrote this snippet, which does essentially what I want:

snippet commentblock
    ######################################## # # # # # # # # # #  ##  ##  ##  ##  ##
    # `r date()`
    # ${1:title} -------------------------------------------------------
    # ${2:text}
    ######################################## # # # # # # # # # #  ##  ##  ##  ##  ##

我添加了 --- 在标题之后,因为我希望此行被文档大纲解释为节标题。可能可以编写一个片段来计算标题中的字符数,并插入-的确切数目需要使 --- 一直保持到空白,但弄清楚这似乎并不能很好地利用我的时间。

I added the --- after the title because I want this line to be interpreted as a section title by the document outline. It may be possible to write a snippet that would count the number of characters in title and insert the exact number of -'s needed to make the --- go all the way to the margin, but figuring that out doesn't seem to be a good use of my time.

这篇关于获取R代码段以接受插入文本中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆