Pasar una opción de clase/paquete/tema a R con knitr

Pasar una opción de clase/paquete/tema a R con knitr

Tengo un beamerdocumento con Rfragmentos, digamos 'template.Rnw', con el que compilo usando knitr'compilar PDF' enRStudio. He definido un tema 'mytheme.sty'. Me gustaría tener una opción para el tema llamado Frenchy llamarlo así en la parte superior del documento:

    \usetheme[French]{mytheme}

Este tema 'francés' define opciones de formato de etiquetas para gráficos que difieren del estilo estadounidense, por ejemplo, $1,000,000.00impresos como 1 000 000,00$.

Pensé que podría crear un valor booleano French == 1dentro de 'mytheme.sty' y luego pasarlo a un Rfragmento para usarlo en un formato condicional if (French) { # set the French styles here.

Pero, para empezar, no sé cómo pasar un LaTeXvalor de contador (digamos) a un Rfragmento. Además, quizás haya un enfoque mejor. Si es así, me encantaría saberlo. A continuación se muestra mi MWE (Advertencia:no compila!)

plantilla.Rnw

\documentclass{beamer}
\usetheme{mytheme}
%\usetheme[French]{mytheme}

<<'setup', include=FALSE>>=
library(knitr)
library(ggplot2)
library(scales)
@

% I'd want to hide this chunk inside mytheme.sty or similar
<<'formats', include=FALSE>>=
# Create euro currency symbol in plot labels with library(scales)
euro <- function(x) {
paste0("€", format(x, big.mark = ",", decimal.mark = ".", trim = TRUE,
    scientific = FALSE))
} 
# French style formatting for euro labels
euroFrench <- function(x) {
paste0(format(x, big.mark = " ", decimal.mark = ",", trim = TRUE,
    scientific = FALSE), " €")
} 
# French style formatting for dollar labels
dollarFrench <- function(x) {
paste0(format(x, big.mark = " ", decimal.mark = ",", trim = TRUE,
    scientific = FALSE), " $")
} 
#  Toggle On/Off to set formats to French or US
if (French) {  # Here reads the boolean French set by the option [French]
  euro <- euroFrench      # set the custom 'euro' style to custom 'euroFrench'
  dollar <- dollarFrench  # overwrite the default 'dollar' styles of library(scales)
}
@

\begin{document}
\begin{frame}
\frametitle{One day I will be a dual-axis euro/dollar plot}
<<'plot', out.width='1\\linewidth'>>=
df <- data.frame(x = c(0, .0001, .0002, .0003), y = c(0, 1000000, 2000000, 3000000))
ggplot(data = df, aes(x = x, y = y)) + geom_line() + theme_classic(30) + scale_x_continuous(labels = dollar) + scale_y_continuous(labels = euro)
@
\end{frame}
\end{document}

mitema.sty

\newcounter{French}
\newif\if@themeFrench%
\@themeFrenchfalse%
\DeclareOption{French}{\@themeFrenchtrue}%
\ProcessOptions%
\if@themeFrench% 
\setcounter{French}{1}
@
\fi

Trama obtenidos con Rfuera de la beamerclase.

ingrese la descripción de la imagen aquí

Respuesta1

No hay manera (al menos no una manera sencilla) de pasar variables LaTeX a R. Lo contrario es mucho más fácil, es decir, usar variables R para escribir LaTeX.

\documentclass{article}
<<include=FALSE>>=
French <- 1
@

<<results='asis'>>=
if (French == 1) cat('\\usepackage[French]{mytheme}')
@

\begin{document}

...

\end{document}

información relacionada