knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(texblocks)
library(texPreview)
tex_opts$set(returnType = 'html')
tex_opts$append(list(cleanup='tex'))

Recasting from raw tex

Original tex Code

can be with or without tabular wrapper

x <- '\\begin{tabular}{l|cccccccccc}
number of flips& 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 \\\\
number of heads& 8  &  8 &  9 & 10 & 10 & 10 & 10 & 10 & 10 & 11 \\\\\\hline
proportion& .73 & .67 & .69 & .71 & .67 &  .63 & .59 & .56 & .53 & .55
\\end{tabular}'
x <- 'number of flips& 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 \\\\
number of heads& 8  &  8 &  9 & 10 & 10 & 10 & 10 & 10 & 10 & 11 \\\\\\hline
proportion& .73 & .67 & .69 & .71 & .67 &  .63 & .59 & .56 & .53 & .55'

Recast to texblock

x <- x%>%as.tb()

Recast to data.frame

x%>%
  as.data.frame()

Use type.convert() to set column class in matrix

x%>%
  as.data.frame(convert = TRUE)

manipulations

gather the columns using tidyr

x%>%
  as.data.frame(convert = TRUE)%>%
  tidyr::gather(col,val,-1)

transponse as a texblock then convert to a data.frame

x%>%
  t()%>%
  as.data.frame()

Preview table using texPreview

x%>%
  t()%>%
  hline()%>%
  tabular(align = '|c|c|c|')%>%
  texPreview::tex_preview()

Recasting to and from a Sparse Matrix

Create Block Matrix

mat <- Matrix::bdiag(
  Matrix::Diagonal(2), 
  matrix(1:3, 3,4), 
  diag(3:2)
)

mat

Convert a matrix to a texblock

x <- mat %>% as.tb()

x

crop

extract subset of the texblock by index

x%>%crop(1:2,1:2) # extract smaller tb from rows 1:2 columns 1:2
x%>%crop(3:5,3:6) # extract smaller tb from rows 3:5 columns 3:6
x%>%crop(6:7,7:8) # extract smaller tb from rows 6:7 columns 7:8

harvest

extract subsets of the texblock using lists , i.e. blocks of tb's by blocks of the matrix

x1 <- x%>%
  harvest(
    list(1:2,3:5,6:7),
    list(1:2,3:6,7:8)
  )

x1

Back to the original Sparse Matrix

x1%>%
  purrr::map(as.matrix)%>% #convert back to list of matrices
  Matrix::bdiag() # convert back to original block matrix


metrumresearchgroup/texblocks documentation built on July 18, 2020, 1:45 a.m.