knitr を使用してクラス/パッケージ/テーマオプションを R に渡す

knitr を使用してクラス/パッケージ/テーマオプションを R に渡す

私は「template.Rnw」というチャンクbeamerを含む文書を持っていてR、それをknitr「PDFをコンパイル」を使用してコンパイルします。RStudio. テーマ「mytheme.sty」を定義しました。テーマにオプションを追加してFrench、ドキュメントの先頭で次のように呼び出すようにします。

    \usetheme[French]{mytheme}

$1,000,000.00この「フランス語」テーマは、 として印刷されるなど、米国スタイルとは異なるグラフィックのラベル書式設定オプションを定義します1 000 000,00$

French == 1「mytheme.sty」内にブール値を作成し、それをR条件で使用するためにチャンクに渡すことができると考えましたif (French) { # set the French styles here

しかし、まず、LaTeXカウンタ値(例えば)をRチャンクに渡す方法がわかりません。また、おそらくもっと良い方法があるでしょう。もしそうなら、ぜひ知りたいです。以下は私のMWEです(警告:コンパイルされません!)

テンプレート.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}

マイテーマ

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

プロットRクラス外 で取得しますbeamer

ここに画像の説明を入力してください

答え1

LaTeX 変数を R に渡す方法はありません (少なくとも簡単な方法はありません)。その逆、つまり R 変数を使用して LaTeX を記述する方がはるかに簡単です。

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

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

\begin{document}

...

\end{document}

関連情報