Passe uma opção de classe/pacote/tema para R com knitr

Passe uma opção de classe/pacote/tema para R com knitr

Eu tenho um beamerdocumento com Rpedaços, digamos 'template.Rnw', que eu compilo usando knitr'compile PDF' emRStudio. Eu defini um tema 'mytheme.sty'. Gostaria de ter uma opção para o tema chamado Frenche chamá-lo assim no topo do documento:

    \usetheme[French]{mytheme}

Este tema 'Francês' define opções de formatação de etiquetas para gráficos que diferem do estilo dos EUA, por exemplo, $1,000,000.00impressos como 1 000 000,00$.

Eu pensei que poderia criar um booleano French == 1dentro de 'mytheme.sty' e depois passá-lo para um Rpedaço para uso condicional, if (French) { # set the French styles here.

Mas, para começar, não sei como passar um LaTeXvalor de contador (digamos) para um Rpedaço. Além disso, talvez haja uma abordagem melhor. Se sim, eu adoraria saber. Abaixo está meu MWE (Aviso:não compila!)

modelo.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}

meutema.sty

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

Trama obtido Rfora da beameraula.

insira a descrição da imagem aqui

Responder1

Não há nenhuma maneira (pelo menos não direta) de passar variáveis ​​LaTeX para R. O oposto é muito mais fácil, ou seja, usar variáveis ​​R para escrever LaTeX.

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

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

\begin{document}

...

\end{document}

informação relacionada