乳膠 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")
}
@

注意results='asis'區塊選項。每個'd 字串都將原樣cat輸出到檔案(因此是該選項的名稱)。.tex換句話說,每個cat'd 字串都將是一個要編譯的 LaTeX 指令。

您甚至可以透過建立一個奇特的 R 函數(例如)來概括上述程式碼include_if_exists(filename),並在多個區塊中重複使用它。

相關內容