knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(rmdhelp)

Disclaimer

The basic features to convert vectors and matrices from R to Rmarkdown are described in this document.

Motivation and Background

Vectors and matrices are data objects which are used very frequently. My own use-case comes from teaching. I teach courses in Applied Statistical Methods In Animal Science and in Livestock Breeding and Genomics. The material of these courses often contain descriptions of linear models which are written in matrix-vector notation. Therefore it is important to be able to convert vectors and matrices in R efficiently into their representations ini Rmarkdown.

Vectors and Matrices in R

Vectors in R can be defined in different ways one of which is by the use of the concatenate function c(). With this a vector v can defined as

v <- c(1,0,0)

The vector v can be converted to RMarkdown format and displayed in the form of a column vector, using

cat(bcolumn_vector(pvec = v, ps_name = "v", ps_env = "$$"), "\n")

The equivalent row vector in RMarkdown is obtained by

cat(brow_vector(pvec = v, ps_name = "v^T", ps_env = "$$"), "\n")

Matrices are constructed using the matrix() function. For the conversion to RMarkdown the function bmatrix() can be used.

n_nr_row <- 12
n_nr_col <- 10
n_start_row <- 12
n_start_col <- 10
for (n_row_idx in n_start_row:n_nr_row){

  #n_row_idx <- 1
  for (n_col_idx in n_start_col:n_nr_col){
    #n_col_idx <- 1
    mat_A <- matrix(round(runif(n_row_idx*n_col_idx), digits = 4), 
                    nrow = n_row_idx, ncol = n_col_idx)
    cat(rmdhelp::bmatrix(pmat = mat_A, 
                         ps_name = paste0("A_{", n_row_idx, ",", n_col_idx, "}",
                                          collapse = ""), ps_env = "$$"), "\n")

  }
}

The maximum number of columns that can be rendered to a pdf-document seams to be r n_nr_col. The question is whether, we can get more columns when reducing the size of the font using some LaTeX options.

s_format <- "html"
if (knitr::is_latex_output()){
  s_format <- "latex"
  #cat("\\tiny \n")
}
n_nr_row <- 12
n_nr_col <- 12
n_start_row <- 12
n_start_col <- 12
for (n_row_idx in n_start_row:n_nr_row){

  #n_row_idx <- 1
  for (n_col_idx in n_start_col:n_nr_col){
    #n_col_idx <- 1
    mat_A <- matrix(round(runif(n_row_idx*n_col_idx), digits = 4), 
                    nrow = n_row_idx, ncol = n_col_idx)
    cat(rmdhelp::bmatrix(pmat = mat_A, 
                         ps_name = paste0("A_{", n_row_idx, ",", n_col_idx, "}",
                                          collapse = ""), 
                         ps_env = "$$",
                         ps_out_format = s_format), "\n")

  }
}
#if (knitr::is_latex_output()) cat("\\normalsize \n")

The above shows the result of reducing the output to a smaller font size. The conversion to pdf when using the 'bmatrix' output type only works when the max. counter of matrix columns is adjusted. See also at https://tex.stackexchange.com/questions/3519/how-to-use-more-than-10-tab-stops-in-bmatrix-or-other-amsmath-matrix-environment for this problem.

This is implemented in rmdhelp starting from version '0.2.11'.



charlotte-ngs/rmdhelp documentation built on Oct. 31, 2023, 10:21 a.m.