Recommended Workflow and Examples

Vilmantas Gegzna

2016-07-12

This vignette introduces to package spAddins, describes a recommended workflow and how the functions of the package should be used and gives several examples.

About The Package

spAddins is an R package that provides a set of RStudio addins which are designed to be used in combination with user-defined RStudio keyboard shortcuts. These addins either:

  1. insert text at the cursor position (e.g. insert operators %>%, <<-, %$%, etc.),
  2. replace symbols in selected pieces of text (e.g., convert backslashes to forward slashes which results in strings like "c:\data\" converted into "c:/data/") or
  3. enclose text with special symbols (e.g., converts “bold” into “**bold**” that is interpreted as “bold”) which is convenient for editing R Markdown files.

The Workflow

The recommended workflow consists of four main steps. Step 1 and Step 2 should usually be done only once. Step 3 and Step 4 can be repeated as many times as needed.

Step 1: Install the package

Released version of package spAddins can be installed from CRAN:

install.packages("spAddins")

Development version can be installed from GitHub:

if (!require(devtools)) install.packages("devtools")
library(devtools)
install_github("GegznaV/spAddins")

Step 2: Define keyboard shortcuts

Functions in the package are also RStudio addins. This fact allows setting user-defined RStudio keyboard shortcuts that call the functions in a more convenient way. Thus the second step is to define keyboard shortcuts for package’s spAddins functions of interest.

To learn how to add user defined RStudio keyboard shortcuts, visit these links: Keyboard Shorcuts and Customizing Keyboard Shortcuts.

Step 3: Place a cursor / Select some text

Package spAddins contains 3 types of addins:

For addins which insert text, in either R editor or in R console place your cursor at the position at which the text should be inserted.

For addins which either replace or enclose text, in R editor or in R console select a piece of text in which certain symbols should be replaced or enclosed.

Step 4: Use the keyboard shortcuts

To call a desired addin function, apply an appropriate keyboard shortcut which in Step 2 was associated with that function.

A Few Examples

Insert %>% operator

This example demonstrates how to use addins which insert text.

Let package spAddins be already installed. First, let’s set keyboard shortcut Ctrl + Alt + . to call function insertPipeline_Addin() which inserts forward-pipe operator %>% operator at the cursor position. This operator is in package magrittr thus the package must be loaded:

library(magrittr)

To learn more about the %>% operator type:

?`%>%`

Next, place your cursor at the position, where the operator should be inserted. For example, between the name of data set iris and the name of a function head():

iris  head()

Apply the shortcut combination Ctrl + Alt + . As a result the operator is inserted:

iris %>% head()

Replace \ with /

This example describes how to use addins which replace text.

Conciser having a string "c:\data\" in which backslashes (\) must be replaced with forward slashes (/). Addin function Back2ForwardSlash() is designed exactly for this task. Let’s define a new shortcut combination, say Ctrl + Alt + Shift + /, which calls the addin function. Now to know the place where the replacement should be applied an appropriate piece of text in either R editor or R console must be selected. Thus let’s select whole string "c:\data\" and push Ctrl + Alt + Shift + /. The result: as expected, the string is converted into "c:/data/".

Enclose with ****

This example describes how to use addins which enclose text.

Conciser having an R Markdown file and a string "This word should be in bold." in it. In this string word word must be in bold. Addin function enclose_with_asterisk2() encloses selected text in double asterisk ** signs, and this is interpreted as bold in R markdown. Let’s define shortcut combination, say Ctrl + Shift + B, which calls the addin function. Now select the word word which is either in R editor or R console and push Ctrl + Shift + B to call the addin function. The result: "This **word** should be in bold."

Available add-in functions

“Insert” family

These functions insert various R operators.

Function Inserts Package associated with operator
insertArrowLR2_Addin() <<- R base
insertArrowRL_Addin() -> R base
insertArrowRL2_Addin() ->> R base
insertIn_Addin() %in% R base
insertMatMuliplication_Addin() %*% R base
insertPipeline_Addin() %>% magrittr
insertTeeOperator_Addin() %T>% magrittr
insertCompAssignPipe_Addin() %<>% magrittr
insertExPipe_Addin() %$% magrittr
insertIfNULL_Addin() %if.NULL% spMisc1
insert_if_null_Addin() %if_null% spMisc
insert_if_null_or_len0_Addin() %if_null_or_len0% spMisc
insertNotIn_Addin() %!in% spMisc
insertPaste_Addin() %.+.% spMisc
insertPaste0_Addin() %++% spMisc

The following functions may be useful for editing R Markdown files or, if commented (#), for structuring R code files.

Function Description Example (first 10 symbols)
insert_ss_line_Addin() Insert single straight (SS) line ----------
insert_ds_line_Addin() Insert double straight (DS) line ==========
insert_sw_line_Addin() Insert single wavy (SW) line ~~~~~~~~~~

“Replace” family

Action Function Text to edit Result
Replace `\` with ` \\` Back2doubleBackSlash() c:\data\ c:\\data\\
Replace `\` with ` /` Back2ForwardSlash() c:\data\ c:/data/

“Enclose” family

These functions are useful for editing R Markdown files.

Action Function Text to edit Result Interpreted in markdown as
Enclose with single asterisk (*) enclose_with_asterisk() italics *italics* italics
Enclose with single underscore (_) enclose_with_underscore() italics _italics_ italics
Enclose with double asterisk (**) enclose_with_asterisk2() bold **bold** bold
Enclose with double underscore (__) enclose_with_underscore2() bold __bold__ bold
Enclose with single back tick (` ) enclose_with_backtick() code `code` code
Enclose with single dollar sign ($) enclose_with_dollar() equation^{inline} $equation^{inline}$ \(equation^{inline}\)
Enclose with single dollar sign ($$) enclose_with_dollar2() equation_{block} $$equation_{block}$$ \(equation_{block}\)

  1. Available at https://github.com/GegznaV/spMisc.