coerce objects into a knitr_kable class object with a latex format

as.kable(x)

Arguments

x

object, can be tex character, object return by returnType = "input", or a path to a tex file.

Value

an object of class knitr_kable

Examples


tex <- '\\begin{tabular}{llr}
\\hline
\\multicolumn{2}{c}{Item} \\\\
\\cline{1-2}
Animal    & Description & Price (\\$) \\\\
\\hline
Gnat      & per gram    & 13.65      \\\\
& each        & 0.01       \\\\
Gnu       & stuffed     & 92.50      \\\\
Emu       & stuffed     & 33.33      \\\\
Armadillo & frozen      & 8.99       \\\\
\\hline
\\end{tabular}'

ktex <- as.kable(tex)

class(ktex)
#> [1] "knitr_kable"

attributes(ktex)
#> $class
#> [1] "knitr_kable"
#> 
#> $format
#> [1] "latex"
#> 

ktex
#> \begin{tabular}{llr}
#> \hline
#> \multicolumn{2}{c}{Item} \\
#> \cline{1-2}
#> Animal    & Description & Price (\$) \\
#> \hline
#> Gnat      & per gram    & 13.65      \\
#> & each        & 0.01       \\
#> Gnu       & stuffed     & 92.50      \\
#> Emu       & stuffed     & 33.33      \\
#> Armadillo & frozen      & 8.99       \\
#> \hline
#> \end{tabular}

# file path

toy <- system.file('examples/toy/toy.tex',package = 'texPreview')

ktex_path <- as.kable(toy)

class(ktex_path)
#> [1] "knitr_kable"

attributes(ktex_path)
#> $class
#> [1] "knitr_kable"
#> 
#> $format
#> [1] "latex"
#> 

ktex_path
#> \begin{tabular}{cccc}
#> \toprule
#> C1 & C2 & C3 & C4\\
#> \midrule
#> a & c & 1 & 1\\
#> a & c & 2 & 1\\
#> a & c & 3 & 1\\
#> a & c & 4 & 0\\
#> a & c & 5 & 0\\
#> \addlinespace
#> a & c & 6 & 1\\
#> a & c & 7 & 1\\
#> a & d & 8 & 0\\
#> a & d & 9 & 0\\
#> a & d & 10 & 0\\
#> \addlinespace
#> b & c & 11 & 0\\
#> b & c & 12 & 0\\
#> b & d & 13 & 1\\
#> b & d & 14 & 1\\
#> b & d & 15 & 0\\
#> \bottomrule
#> \end{tabular}

# texpreview_input class
  # this is the same output class as one would get with
  # tex_preview(tex,returnType = 'input')

toy_input <- structure(sprintf('\\input{%s}',toy),class = 'texpreview_input')

toy_input
#> \input{/Users/runner/work/_temp/Library/texPreview/examples/toy/toy.tex}

as.kable(toy_input)
#> \begin{tabular}{cccc}
#> \toprule
#> C1 & C2 & C3 & C4\\
#> \midrule
#> a & c & 1 & 1\\
#> a & c & 2 & 1\\
#> a & c & 3 & 1\\
#> a & c & 4 & 0\\
#> a & c & 5 & 0\\
#> \addlinespace
#> a & c & 6 & 1\\
#> a & c & 7 & 1\\
#> a & d & 8 & 0\\
#> a & d & 9 & 0\\
#> a & d & 10 & 0\\
#> \addlinespace
#> b & c & 11 & 0\\
#> b & c & 12 & 0\\
#> b & d & 13 & 1\\
#> b & d & 14 & 1\\
#> b & d & 15 & 0\\
#> \bottomrule
#> \end{tabular}