Create default comment header template in R?

Is it possible to create a default comment header template in R for all new scripts?

I usually include some standard information at the beginning of all my scripts and would like to automate the process of creating a comment title.

For instance:

##################################################
## Project:
## Script purpose:
## Date:
## Author:
##################################################
+4
source share
3 answers

I don't know if you can do this directly, but you can approximate it by putting the following in your Rprofile.

make_r_template <- function(filename = "r_template.R", dir = getwd())
{
  if (file.exists(file.path(dir, filename))) invisible(NULL)
  else{
    write(c("##################################################",
            "## Project:",
            "## Script purpose:",
            "## Date:",
            "## Author:",
            "##################################################"),
          file = file.path(dir, filename),
          sep = "\n")
  }
}

make_r_template()

This will start every time R starts up and writes an empty template to the working directory if it does not exist. You can also run the function at any point with a different value filenameto create an empty template elsewhere.

, , , .

+3

@lmo , RStudio, , Preferences > Code > Edit Snippets

. , ,

1:

snippet header_script
  ##################################################
  ## Project:
  ## Script purpose:
  ## Date:
  ## Author:
  ##################################################

2: script:

snippet header_section
  ## Section:
  ##################################################

, , (, header_script), ( <), enter, .

+2

If you use vim , you can define such a snippet ultisnips one in a separate R -snippet file:

snippet header "R Header template" b
#' ---
#' title:  ${1:title here}
#' author: ${2:John Smith (John@Smith.com)}
#' date:   ${3:`date +%Y-%m-%d`}
#' ---
endsnippet

You would need to enter header<tab>, which is a significant time saver, especially since it fills in the predefined values ​​(such a date).

It is also compatible with Rmarkdown, as discussed here .

0
source

Source: https://habr.com/ru/post/1660827/


All Articles