Learn R Programming

Utilities for developing R software

The {oeli} package offers a collection of handy functions that I found useful while developing R packages. Perhaps you’ll find them helpful too!

Installation

The released package version can be installed from CRAN via:

install.packages("oeli")

Demos

The package includes helpers for various tasks and objects. Some demos are shown below. Click the headings for reference pages with documentation on all available helpers in each category.

Distributions

The package has density and sampling functions for distributions not in base R, such as Dirichlet, multivariate normal, truncated normal, and Wishart.

ddirichlet(x = c(0.2, 0.3, 0.5), concentration = 1:3)
#> [1] 4.5
rdirichlet(concentration = 1:3)
#> [1] 0.1273171 0.5269401 0.3457428

For faster computation, Rcpp implementations are also available:

microbenchmark::microbenchmark(
  "R"    = rmvnorm(mean = c(0, 0, 0), Sigma = diag(3)),
  "Rcpp" = rmvnorm_cpp(mean = c(0, 0, 0), Sigma = diag(3))
)
#> Unit: microseconds
#>  expr   min     lq    mean median     uq    max neval
#>     R 200.5 208.25 263.396 217.10 234.35 2154.7   100
#>  Rcpp   2.7   2.90   5.386   4.05   4.40   72.0   100

Function helpers

Retrieving default arguments of a function:

f <- function(a, b = 1, c = "", ...) { }
function_defaults(f)
#> $b
#> [1] 1
#> 
#> $c
#> [1] ""

Indexing helpers

Create all possible permutations of vector elements:

permutations(LETTERS[1:3])
#> [[1]]
#> [1] "A" "B" "C"
#> 
#> [[2]]
#> [1] "A" "C" "B"
#> 
#> [[3]]
#> [1] "B" "A" "C"
#> 
#> [[4]]
#> [1] "B" "C" "A"
#> 
#> [[5]]
#> [1] "C" "A" "B"
#> 
#> [[6]]
#> [1] "C" "B" "A"

Package helpers

Quickly have a basic logo for your new package:

package_logo("my_package", brackets = TRUE, use_logo = FALSE)

How to print a matrix without filling up the entire console?

x <- matrix(rnorm(10000), ncol = 100, nrow = 100)
print_matrix(x, rowdots = 4, coldots = 4, digits = 2, label = "what a big matrix")
#> what a big matrix : 100 x 100 matrix of doubles 
#>         [,1]  [,2]  [,3] ... [,100]
#> [1,]    2.39   0.3 -0.48 ...   0.56
#> [2,]   -1.33  0.62  0.37 ...  -1.21
#> [3,]   -0.03 -0.43  1.71 ...   0.07
#> ...      ...   ...   ... ...    ...
#> [100,]  0.14 -0.16  2.49 ...  -1.58

And what about a data.frame?

x <- data.frame(x = rnorm(1000), y = LETTERS[1:10])
print_data.frame(x, rows = 7, digits = 0)
#>      x  y
#> 1     0 A
#> 2    -1 B
#> 3     0 C
#> 4    -1 D
#> < 993 rows hidden >
#>          
#> 998  -1 H
#> 999  -1 I
#> 1000  0 J

Simulation helpers

Let’s simulate a Markov chain:

Gamma <- sample_transition_probability_matrix(dim = 3)
simulate_markov_chain(Gamma = Gamma, T = 20)
#>  [1] 2 1 1 3 1 1 2 2 3 2 2 2 2 2 1 1 1 1 1 3

Transformation helpers

The group_data.frame() function groups a given data.frame based on the values in a specified column:

df <- data.frame("label" = c("A", "B"), "number" = 1:10)
group_data.frame(df = df, by = "label")
#> $A
#>   label number
#> 1     A      1
#> 3     A      3
#> 5     A      5
#> 7     A      7
#> 9     A      9
#> 
#> $B
#>    label number
#> 2      B      2
#> 4      B      4
#> 6      B      6
#> 8      B      8
#> 10     B     10

Validation helpers

Is my matrix a proper transition probability matrix?

matrix <- diag(4)
matrix[1, 2] <- 1
check_transition_probability_matrix(matrix)
#> [1] "Must have row sums equal to 1"

Copy Link

Version

Install

install.packages('oeli')

Monthly Downloads

854

Version

0.7.1

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Lennart Oelschläger

Last Published

November 27th, 2024

Functions in oeli (0.7.1)

function_body

Extract function body
function_defaults

Get default function arguments
map_indices

Map indices
oeli-package

oeli: Utilities for Developing Data Science Software
package_logo

Creating a basic logo for an R package
sample_transition_probability_matrix

Sample transition probability matrices
sample_correlation_matrix

Sample correlation matrix
timed

Interrupt long evaluations
identical_structure

Check if two objects have identical structure
insert_matrix_column

Insert column in matrix
insert_vector_entry

Insert entry in vector
print_data.frame

Print (abbreviated) data.frame
round_data.frame

Round numeric columns of a data.frame
input_check_response

Standardized response to input check
sample_covariance_matrix

Sample covariance matrix
system_information

General system level information
permutations

Build permutations
dwishart_cpp

Wishart distribution
variable_name

Determine variable name
user_confirm

User confirmation
match_arg

Argument matching
stationary_distribution

Stationary distribution
group_data.frame

Grouping of a data.frame
print_matrix

Print (abbreviated) matrix
subsets

Generate vector subsets
match_numerics

Best-possible match of two numeric vectors
quiet

Silence R code
vector_occurrence

Find the positions of first or last occurrence of unique vector elements
matrix_diagonal_indices

Get indices of matrix diagonal
unexpected_error

Handling of an unexpected error
try_silent

Try an expression silently
matrix_indices

Get matrix indices
simulate_markov_chain

Simulate Markov chain
merge_lists

Merge named lists
split_vector_at

Split a vector at positions
chunk_vector

Split a vector into chunks
Storage

Storage R6 Object
check_covariance_matrix

Check covariance matrix
function_arguments

Get function arguments
check_list_of_lists

Check list of lists
Dictionary

Dictionary R6 Object
check_correlation_matrix

Check correlation matrix
dmvnorm_cpp

Multivariate normal distribution
check_numeric_vector

Check numeric vector
diff_cov

Difference and un-difference covariance matrix
dtnorm_cpp

Truncated normal distribution
correlated_regressors

Simulate correlated regressor values
check_transition_probability_matrix

Check transition probability matrix
check_probability_vector

Check probability vector
delete_columns_data.frame

Deleting data.frame columns
cov_to_chol

Cholesky root of covariance matrix
ddirichlet_cpp

Dirichlet distribution
do.call_timed

Measure computation time