library(short)
The short
package gives several shortcuts to common functionality.
using
- easy loading packagesThe using
function is a simpler way of specifying multiple packages to
load. It additionally will attempt to download and install the packages
if not already installed.
table_1
- Table 1 Demographics and SummariesThe table_1
function can be used to generate a demographics and summary
of variables; i.e. the veritable "Table 1" for publications.
For each variable given, table_1
calls summarise_table_1
which
will summarize the variable for each value of a key, differentiating
variable, as well as overall. For character and factor variables,
the counts and percentages of each level is given.
For logical variables, the count of positives and the percentage is
given as the value. Numeric Variables are summarized by the minimum,
median, mean, maximum, and standard deviation.
Additional methods for table_1 can be added by adding methods to
summarise_table_1
library(tidyverse) mtcars %>% mutate( gear = ordered(paste(gear, 'Gears')) , cyl = ordered(paste(cyl, 'Cylinders')) ) %>% table_1( cyl , 'Automatic Transmission' = am == 0 , 'Number of gears' = gear , Horsepower = hp )
with_margins
- add marginals to and computation.The with_margins
function converts another function to operate over
all possible subsets of groups, including no groups.
mtcars %>% mutate( gear = ordered(paste(gear, 'Gears')) , cyl = ordered(paste(cyl, 'Cylinders')) ) %>% group_by(gear, cyl) %>% with_margins(summarise_at)( vars(mpg, disp, hp, wt) , funs(min, mean, max) )
spread_each
- Spread Multiple ValuesThe spread_each
function works similar to the tidyr::spread
function
but works for spreading out multiple values.
mtcars %>% count(gear, cyl) %>% group_by(gear) %>% mutate(pct = n/sum(n)) %>% spread_each(cyl, n, pct)
.T
- Simple text specificationSimilar to lazy loading of the tidyverse
,
the .T
function provides
a simple way to designate arguments should be interpreted as text.
The limiting factor is that the text must be syntactically correct,
however it come in very helpful.
.T(first, second, 'a third argument with spaces')
The short
package also provides many inline shortcuts.
%\\n%
- concatenate with a line break%<<%
- concatenate with a space%<<<%
- concatenate without space.%~%
- Matches the pattern on the left hand side.%!~%
- Does not match the pattern on the left hand side.%or%
- Similar to the rlang
or purrr
function %||%
but checks
on length
rather than is.null
so will give the right hand
side for length 0 vectors as well.%inherits%
- inline version of inherits
%is a%
- alternate inline version of inherits
There are two convenience function for filtering character vectors,
sift
and the filter out version seive
.
# limit to Mercs mtcars %>% rownames() %>% sift("^Merc") # Filter out everything starting with M mtcars %>% rownames() %>% sieve("^M")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.