It fails, however, using rmarkdown::render("toy_ex.Rmd") from the console, producing this error at the end of attempted compliation:
! Undefined control sequence.
l.121 \toprule
Error: LaTeX failed to compile toy_ex.tex.
This seems to be some problem with the intermediate .tex file that the call to kable() is generating, but why woud it knit successfully from the Rstudio IDE and not from the console?
I've followed Yihui's debugging steps here; updated packages, reinstalled tinytex, etc.
processing file: toy_ex.Rmd
|.......................................... | 50%
ordinary text without R code
|...................................................................................| 100%
label: cars
output file: toy_ex.knit.md
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS toy_ex.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output toy_ex.tex --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --lua-filter "C:/Users/john/Documents/R/win-library/4.0/rmarkdown/rmd/lua/pagebreak.lua" --lua-filter "C:/Users/john/Documents/R/win-library/4.0/rmarkdown/rmd/lua/latex-div.lua" --variable "geometry:margin=1in"
! Undefined control sequence.
l.117 \toprule
Error: LaTeX failed to compile toy_ex.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See toy_ex.log for more info.
Comparing the two calls to pandoc, there was only one difference. The RStudio Knit button version included --include-in-header "C:\Users\john\AppData\Local\Temp\RtmpOObyVC\rmarkdown-strcc20ea115d.html".
However, after I restarted my R session and re-ran rmarkdown::render("toy_ex.Rmd"), it suddenly worked. And it additionally included the --include-in-header argument to pandoc. Thus my guess is that somehow the recently installed TeX files weren't immediately available to the R console, and thus required a restart first.
After you restart the session (Cmd-Shift-F10 on macOS), do you still see the error with rmarkdown::render()?
In case it could be helpful, here's my session information:
I think this is something due to the way kableExtra tries to help the user by automatically adding the required latex dependencies.
In your case you need booktabs. It seems that kableExtra only manage to pass the information when first use in a clean session. (so first rendering at console - with knit button it is always a clean session)
If you pass the required Tex package yourself, it is working fine I think