Home

Awesome

snippets-addin

Travis-CI Build Status Issue Count codecov CRAN version

RStudio add-in to add some code snippets, or help with code editing. It is aimed to be used on Windows. It requires RStudio (>= 0.99.1111) and rstudioapi (>= 0.5-1).

Currently it contains following functions:

Insert and reformat pipe

This functionality inserts pipes at the current position(s) of the cursor(s) or replaces all selections. It reformats pipe(s) surroundings to achieve following format:

sub <- data %>%
    select(column.1, column.2) %>%
        filter(column.1 > x) %>%
            group_by(column.1) %>%
                summarise(n=n_distinct(column.2)) %>%
                    ungroup

Reverse slashes

This functionality is especially useful, when copying paths in Windows.

It will reverse all slashes either in the selected block(s) of code, or if there is no selection (or only whitespace is selected), it will reverse all slashes in the clipboard and paste it to the current cursor(s) position(s).

Copy data to clipboard

At the moment this is Windows only function.

Function will copy the content of the variable 'under the cursor' into the clipboard. It will be represented as a tab separated value for an easy paste to MS Excel.

There is no need to precisely select the name of the variable. Cursor can be placed anywhere in the name, or variable name needs to be first valid name in the code selection. Add-in will adjust the selection. In the case that it is not possible to generate tsv, message will be written to the console. Otherwise tsv will be 'silently' copied to clipboard without any messages.

At the moment following data structures are supported:

Vectors

Vectors are represented in a horizontal fashion. If they are named, then first row will contain names and second values. If they are unnamed then only one row with values is copied into the clipboard.

Matrices

Value copied to clipboard will either have M x N or (M+1) x N, M x (N+1) or (M+1) x (N+1) cells, where M and N are matrix dimensions. If matrix has specified columns names and/or rows names than they will be displayed in the first column and/or row.

In the case that both columns' names and rows' names are specified, the content of the top left cell will be constructed from dimensions' names, if they exist; in the following format: Rows names dimension name, backslash, columns names dimension name. Examples below shows all cases. If dimensions are not named, then variable name will be used.

mat.1 <- matrix( 1:9
               , nrow=3
               , dimnames=list( rows=letters[1:3]
                              , columns=letters[24:26]
                              )
               )
rows\columnsxyz
a147
b258
c369
mat.2 <- matrix( 1:9
               , nrow=3
               , dimnames=list(         letters[1:3]
                              , columns=letters[24:26]
                              )
               )
\columnsxyz
a147
b258
c369
mat.3 <- matrix( 1:9
               , nrow=3
               , dimnames=list( rows=letters[1:3]
                              ,      letters[24:26]
                              )
               )
rows\xyz
a147
b258
c369
mat.4 <- matrix( 1:9
               , nrow=3
               , dimnames=list( letters[1:3]
                              , letters[24:26]
                              )
               )
mat.4xyz
a147
b258
c369

Data frames

Data frames act as matrices.

Arrays

1D arrays

1D arrays act as vectors.

2D arrays

2D arrays act as matrices.

3+D arrays

3+D arrays will be flatten into a matrix. Matrix will have N+1 columns where N is a number of dimensions and M or M+1 rows, where M is a product of array dimensions. E.g. if array has following dimensions dim=c(2, 4, 2), then the output table will have N=3+1=4 columns and M=242=16 rows. If array dimensions are named, then header row will be added. First N columns will be take names from dimensions' names and the last column will be named after variable. Missing names will stay empty.

See examples below.

Example 1

3D array with defined dimension names. One of the dimension names is missing.

(arr.3d <- array( 1:24
                , dim=c(3, 4, 2)
                , dimnames=list( x=c('a', 'b', 'c')
                               ,   c('k', 'l', 'm', 'n')
                               , z=c('x', 'y')
                               )
                )
)

Print out:

, , z = x

x   k l m  n
  a 1 4 7 10
  b 2 5 8 11
  c 3 6 9 12

, , z = y

x    k  l  m  n
  a 13 16 19 22
  b 14 17 20 23
  c 15 18 21 24

In clipboard:

xzarr.3d
akx1
bkx2
ckx3
alx4
blx5
clx6
amx7
bmx8
cmx9
anx10
bnx11
cnx12
aky13
bky14
cky15
aly16
bly17
cly18
amy19
bmy20
cmy21
any22
bny23
cny24
Example 2

3D array without named dimensions.

(arr.3d <- array( 1:24
                , dim=c(3, 4, 2)
                , dimnames=list( c('a', 'b', 'c')
                               , c('k', 'l', 'm', 'n')
                               , c('x', 'y')
                               )
                )
)

Print out:

, , x

  k l m  n
a 1 4 7 10
b 2 5 8 11
c 3 6 9 12

, , y

   k  l  m  n
a 13 16 19 22
b 14 17 20 23
c 15 18 21 24

In clipboard:

akx1
bkx2
ckx3
alx4
blx5
clx6
amx7
bmx8
cmx9
anx10
bnx11
cnx12
aky13
bky14
cky15
aly16
bly17
cly18
amy19
bmy20
cmy21
any22
bny23
cny24
Example 3

Bare 3D array.

(arr.3d <- array(1:24, dim=c(3, 4, 2)))

Print out:

, , 1

     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12

, , 2

     [,1] [,2] [,3] [,4]
[1,]   13   16   19   22
[2,]   14   17   20   23
[3,]   15   18   21   24

In clipboard:

AAA1
BAA2
CAA3
ABA4
BBA5
CBA6
ACA7
BCA8
CCA9
ADA10
BDA11
CDA12
AAB13
BAB14
CAB15
ABB16
BBB17
CBB18
ACB19
BCB20
CCB21
ADB22
BDB23
CDB24

Tables

"1D" tables

"1D" tables act as vectors.

"2D" tables

"2D" tables act as matrices.

"3+D" tables

"3+D" tables act as 3+D arrays.

Collection of badges

Repository size Pending Pull-Requests downloads release version license