Dynamic Rstudio Code Snippet

眉间皱痕 提交于 2019-12-12 13:15:42

问题


I tend to use a lot of line breaks in my code like the following:

# Data =========================================================================

Where the entire comment is always 80 characters long (including the hashtag). What I would like to do is write a code snippet for Rstudio that will insert the hashtag, then a space, then allow the user to type out a series of words, then insert another space, and finally fill in a bunch of "=" until the 80 character limit is reached.

I'm not familiar with how snippets work at all so I'm not sure how difficult this is.

I have this much:

snippet lb
  # ${1:name}

but I have no idea how to add a dynamic number of "=" signs. Also, lb = linebreak.


回答1:


You can't do this with snippets, unfortunately; a snippet is a text template that contains fixed text with slots for user-inserted text.

There is a command built into RStudio to do something very similar, however; from the Code menu, choose Insert Section (or Ctrl+Shift+R). This will do exactly what you're describing, with two small differences:

  1. The line will extend to 5 characters before the print margin (you can adjust the print margin in Tools -> Global Options -> Code.

  2. The line is composed of - rather than = characters.

One advantage to sections marked in this way is that you can use them to fold and navigate inside the file (look at the editor status bar after adding one).




回答2:


You can write a snippet to manipulate text (somewhat). I wrote the snippet below to do something similar to what you want to do. I'm still ironing out the issues (just asked this question).

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

I think if you write an R code snippet using an anonymous function that accepts text input via $$, counts the nchar in the text, calculates the number of -'s needed at the end, and then uses eval(paste0()) to insert the comment you should be able to make it work. I'll post a comment or answer here if I figure it out. Please do the same on my question if you get it to work. Thanks. (P.S. Go Badgers!)




回答3:


You can use the rstudioapi (which can return column position) inside the snippet to get something like what you want.

Below is a snippet I use called endhead. I use it by commenting my header title and then applying the snippet, eg:

# Section name endhead

which results in:

# Section name -----------------------------------------------------------------
snippet endhead
    `r paste0(rep.int("-", 88 - rstudioapi::primary_selection(rstudioapi::getActiveDocumentContext())$range$start[2]), collapse = "")`



来源:https://stackoverflow.com/questions/32633920/dynamic-rstudio-code-snippet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!