haozhu233 / kableExtra

Construct Complex Table with knitr::kable() + pipe.
https://haozhu233.github.io/kableExtra/
Other
689 stars 147 forks source link

loading kableExtra breaks inline R code in YAML header #16

Closed sjpierce closed 7 years ago

sjpierce commented 7 years ago

I've been using a YAML header in my R markdown document (*.Rmd) that inserts headers and footers. My left footer uses inline R code to display the path and filename of the PDF file that will result when the file is knitted in R studio. I discovered today that loading the kableExtra package breaks that inline code and causes the code itself to display rather than the result of the code. Commenting out the library(kableExtra) line in the code below corrects the footer to show the path instead of the inline code that supposed to generate it.

Code for reproducing the problem is below.

---
output:
  pdf_document:
    latex_engine: xelatex
header-includes:
- \usepackage{fancyhdr}
- \usepackage[yyyymmdd,hhmmss]{datetime}
- \usepackage{lastpage}
- \usepackage{fontspec}
- \pagestyle{fancy}
- \lhead{Left header}
- \rhead{\today\ \currenttime}
- \cfoot{ }
- \fancyfoot[RE,RO]{\thepage\ of \pageref*{LastPage}}
- \renewcommand{\headrulewidth}{0.4pt}
- \renewcommand{\footrulewidth}{0.4pt}
- \fancypagestyle{plain}{\pagestyle{fancy}}
- \lfoot{\texttt{\small \detokenize{`r sub(".Rmd", ".pdf", knitr:::current_input(dir=TRUE))`}}}
---

# Setup 
``` {r global_options}
# Global chunk options (must be a stand-alone initial R chunk). 
knitr::opts_chunk$set(include  = TRUE, echo = TRUE, error = TRUE, 
                      message = TRUE, warning = TRUE)
library(knitr)            # for kable().
library(kableExtra)       # For formatting tables made by kable().
haozhu233 commented 7 years ago

Hi, @sjpierce, thanks for reporting. There is a bug in current CRAN version of rmarkdown that prevents users from loading latex packages from 2 sources. You need to update your rmarkdown to the latest dev version by devtools::install_github("rstudio/rmarkdown").

You can see #5 & #13 for details.

I put a note in the limitation section but I guess it's not that obvious. I'm going to highlight it in the first section of my README.

sjpierce commented 7 years ago

I think this may be distinct from those prior issues. I've already installed the dev version of rmarkdown: that's what allows me to even get a footer to show up at all when kableExtra is loaded. The issue is that it is not being displayed correctly. Run the example code above with and without the library(kableExtra) line commented out and compare the contents of the left footer.

By the way, kableExtra looks like a promising package. The ability to group rows & columns in tables should provide quite useful.

haozhu233 commented 7 years ago

I see. I reproduced the bug. It seems that the previous fix in rmarkdown hasn't entirely solved the problem. I'll look into that. Thanks for letting me know

haozhu233 commented 7 years ago

I don't think the "problem" in rmarkdown can be easily fixed. In short, if you use kableextra (or packages using similar mechanisms (maybe like huxtable), contents defined in header-includes will be read in through includes(http://rmarkdown.rstudio.com/pdf_document_format.html#includes), which is the more official way to reading tex heading into rmarkdown. The downside of that is that it does not accept inline R code anymore.

I guess for now, if you really want to use kableextra, maybe you will have to just find a better place for the document directory (like in a paragraph?) I'm also thinking about if I should just add a global option that will prevent kableExtra from loading latex package by itself for this kind of cases so users can load latex packages by themselves.