'Include latex package option before documentclass in RMarkdown beamer presentation

I am trying to compile the following beamer presentation in rmarkdown. But when it tries to render rmarkdown document an error occurs

! LaTeX Error: Option clash for package xcolor. 

I searched Internet and found out that I need to include \PassOptionsToPackage{table}{xcolor} before the line \documentclass. Applying options to already loaded package and also see here

How do I do it in RMarkdown?

    ---
    title: "My report"
    output: 
    beamer_presentation:
        keep_tex: true
    header-includes: 
    - \usepackage{booktabs}
    - \usepackage{tabu}
    - \usepackage{threeparttable}
    - \usepackage{threeparttablex}
    - \usepackage[normalem]{ulem}  
    - \usepackage[table]{xcolor}
    ---


    ```{r setup, include=FALSE}
    library(knitr)
    library(kableExtra)
    knitr::opts_chunk$set(echo = FALSE)
    ```

    ```{r positioned_table}
    dt <- mtcars[1:5, 1:6]
    kable(dt) %>%
    kable_styling("striped", full_width = F) %>%
    column_spec(5:7, bold = T) %>%
    row_spec(3:5, bold = T, color = "white", background = "#D7261E")
    ```

The resulting .tex file is

    \PassOptionsToPackage{unicode=true}{hyperref} % options for packages loaded elsewhere
    \PassOptionsToPackage{hyphens}{url}
    %
    \documentclass[ignorenonframetext,]{beamer}
    \usepackage{pgfpages}
    \setbeamertemplate{caption}[numbered]
    \setbeamertemplate{caption label separator}{: }
    \setbeamercolor{caption name}{fg=normal text.fg}
    \beamertemplatenavigationsymbolsempty
    % Prevent slide breaks in the middle of a paragraph:
    \widowpenalties 1 10000
    \raggedbottom
    \setbeamertemplate{part page}{
    \centering
    \begin{beamercolorbox}[sep=16pt,center]{part title}
      \usebeamerfont{part title}\insertpart\par
    \end{beamercolorbox}
    }
    \setbeamertemplate{section page}{
    \centering
    \begin{beamercolorbox}[sep=12pt,center]{part title}
      \usebeamerfont{section title}\insertsection\par
    \end{beamercolorbox}
    }
    \setbeamertemplate{subsection page}{
    \centering
    \begin{beamercolorbox}[sep=8pt,center]{part title}
      \usebeamerfont{subsection title}\insertsubsection\par
    \end{beamercolorbox}
    }
    \AtBeginPart{
      \frame{\partpage}
    }
    \AtBeginSection{
      \ifbibliography
      \else
        \frame{\sectionpage}
      \fi
    }
    \AtBeginSubsection{
      \frame{\subsectionpage}
    }
    \usepackage{lmodern}
    \usepackage{amssymb,amsmath}
    \usepackage{ifxetex,ifluatex}
    \usepackage{fixltx2e} % provides \textsubscript
    \ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
      \usepackage[T1]{fontenc}
      \usepackage[utf8]{inputenc}
      \usepackage{textcomp} % provides euro and other symbols
    \else % if luatex or xelatex
      \usepackage{unicode-math}
      \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
    \fi
    % use upquote if available, for straight quotes in verbatim environments
    \IfFileExists{upquote.sty}{\usepackage{upquote}}{}
    % use microtype if available
    \IfFileExists{microtype.sty}{%
    \usepackage[]{microtype}
    \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
    }{}
    \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
    }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}
    }
    \usepackage{hyperref}
    \hypersetup{
                pdftitle={My report},
                pdfborder={0 0 0},
                breaklinks=true}
    \urlstyle{same}  % don't use monospace font for urls
    \newif\ifbibliography
    \setlength{\emergencystretch}{3em}  % prevent overfull lines
    \providecommand{\tightlist}{%
      \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
    \setcounter{secnumdepth}{0}

    % set default figure placement to htbp
    \makeatletter
    \def\fps@figure{htbp}
    \makeatother

    \usepackage{booktabs}
    \usepackage{tabu}
    \usepackage{threeparttable}
    \usepackage{threeparttablex}
    \usepackage[normalem]{ulem}
    \usepackage[table]{xcolor}

    \title{My report}
    \date{}

    \begin{document}
    \frame{\titlepage}

    \begin{frame}

    \begin{table}[H]
    \centering
    \begin{tabular}{l|r|r|r|>{\bfseries}r|>{\bfseries}r|>{\bfseries}r}
    \hline
      & mpg & cyl & disp & hp & drat & wt\\
    \hline
    Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
    \hline
    Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
    \hline
    \rowcolor[HTML]{D7261E}  \textcolor{white}{\textbf{Datsun 710}} & \textcolor{white}{\textbf{22.8}} & \textcolor{white}{\textbf{4}} & \textcolor{white}{\textbf{108}} & \textcolor{white}{\textbf{93}} & \textcolor{white}{\textbf{3.85}} & \textcolor{white}{\textbf{2.320}}\\
    \hline
    \rowcolor[HTML]{D7261E}  \textcolor{white}{\textbf{Hornet 4 Drive}} & \textcolor{white}{\textbf{21.4}} & \textcolor{white}{\textbf{6}} & \textcolor{white}{\textbf{258}} & \textcolor{white}{\textbf{110}} & \textcolor{white}{\textbf{3.08}} & \textcolor{white}{\textbf{3.215}}\\
    \hline
    \rowcolor[HTML]{D7261E}  \textcolor{white}{\textbf{Hornet Sportabout}} & \textcolor{white}{\textbf{18.7}} & \textcolor{white}{\textbf{8}} & \textcolor{white}{\textbf{360}} & \textcolor{white}{\textbf{175}} & \textcolor{white}{\textbf{3.15}} & \textcolor{white}{\textbf{3.440}}\\
    \hline
    \end{tabular}
    \end{table}

    \end{frame}

    \end{document}




Solution 1:[1]

Using very usefull comment from @samcarter, I found the exact answer. All I need is the following yaml header in my rmarkdown document:

    ---
    title: "My report"
    output: 
      beamer_presentation:
        keep_tex: true
    classoption: xcolor=table
    header-includes:
      - \usepackage{booktabs}
      - \usepackage{tabu}
      - \usepackage{threeparttable}
      - \usepackage{threeparttablex}
      - \usepackage[normalem]{ulem}
    ---

Solution 2:[2]

To use the table xcolor option with beamer on can pass this to the documentclass. In traditional latex

\documentclass[xcolor={table}]{beamer}

For markdown https://bookdown.org/yihui/rmarkdown/pdf-document.html#latex-options suggests that on can use

 classoption: xcolor={table}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alexander
Solution 2 samcarter_is_at_topanswers.xyz