ラテックスifFileExist

ラテックスifFileExist
\subsection{Checksum: Factor, Specific, and Total Variance}
Perhaps the most basic test of a regression model is if the factor and specific variances sum to equal the total variance.  Here, we 
present this diagnostic as an xy-plot, with the sum of factor and specific variance on the x-axis and the total variance on the y-axis.  We 
expect the plotted points to line up tightly along the identity line.

\clearpage
\begin{figure}[!h]

\centering

\includegraphics{\Sexpr{image_dir}xyplotFacSpecTotVarTest.pdf}

\caption{Total Variance vs. Sum of Factor and Specific Variance, All Instruments Passing Exposure Model.}

\end{figure}

質問:

「xyplotFacSpecTotVarTest.pdf」が存在しない状況をどのように処理すればよいでしょうか。ファイルはテーブルの内容から生成されますが、データが含まれている場合と含まれていない場合があります。「xyplotFacSpecTotVarTest.pdf」がない場合、最終的な PDF 生成プロセスでエラーが発生します。このシナリオをどのように処理すればよいでしょうか。/IfFileExist を試しましたが、機能していないようです。次の実装を参照してください。

\IfFileExists{{image_dir}{xyplotFacSpecTotVarTest.pdf}} { 

\clearpage

\begin{figure}[!h]

\centering

\includegraphics{\Sexpr{image_dir}{xyplotFacSpecTotVarTest.pdf}}

\typeout{File Exist: xyplotFacSpecTotVarTest.pdf}

\caption{Total Variance vs. Sum of Factor and Specific Variance, All Instruments Passing Exposure Model.}

\end{figure} } 

{\typeout{File DOESNT Exist: xyplotFacSpecTotVarTest.pdf}}

答え1

タグを見ると、 を使用していると思いますknitr。ファイルが存在するかどうかを R 自体に (黙って) 判断させないのはなぜでしょうか?

<<chunkname,results='asis',echo=FALSE>>=
if (file.exists(file.path(image_dir, "xyplotFacSpecTotVarTest.pdf"))) {
   cat("\\clearpage\n")
   cat("\\begin{figure}[!h]\n")
   # and so on
}
else {
  cat("\\textbf{File does not exist.}\n\n")
}
@

chunk オプションに注意してくださいresults='asis'。'd された各文字列は、そのままファイルcatに出力されます(オプションの名前の由来)。言い換えると、 'd された各文字列は、コンパイルされる LaTeX コマンドになります。.texcat

たとえば、 という高度な R 関数を作成して上記のコードを一般化しinclude_if_exists(filename)、それを多くのチャンクで再利用することもできます。

関連情報