Smerla
Smerla

Reputation: 231

Renamed: Solution for boilerplate code in Rstudio

EDIT: This question turned out to have an interface solution that is not related to programming per se. The question is therefore not really regarding the r-language any more.

Original titel: Paste code to R console, preserving line breaks and indention without formatting?

I'm looking for a way to easily store and subsequently print code to the R console. Preferably without having to deal with escaped special characters such as \".

the expr() function is almost the right thing as it does not demand any formatting. Unfortunately it does not preserve line breaks:

# A)   
expr("17" %>%
       as.numeric())
   

is therefore equivalent of

# B) 
expr("17" %>% as.numeric())

Both printing:

"17" %>% as.numeric()

However, the functions I have found that do respect new lines and keeps indention demands escape characters. For example glue() from the glue package:

glue("
\"17\" %>%
  as.numeric()")

which prints beautifully:

"17" %>%
  as.numeric()

Is there any function already that can handle my problem?

I believe that my case is pretty much what the "Error: unexpected ..." output does in R when you for example have an extra ) in the end of a call. But I do not know how to find the underlying code that produces those error messages.

Upvotes: 1

Views: 218

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

Many IDEs, including RStudio, have a feature to insert frequently-used code snippets with placeholders.

These snippets can be added/customised in the preferences, and are accessible via the auto-completion menu.

Upvotes: 1

Related Questions