首页 > 解决方案 > knitr sanitize_fn 警告通过 knit2pdf 将绘图合并到乳胶中

问题描述

我使用调用 knit2pdf 的驱动程序脚本在 R 中动态生成 pdf 报告。我的报告来源是乳胶,在 .Rnw 文件中,调用如下:

knit2pdf("source.Rnw",output=paste0(fname,".tex"),quiet=T)

fname 不包含任何点。

source.Rnw 包含:

<<setup, echo=FALSE >>=
opts_chunk$set(fig.path=tempfile(tmpdir="work",pattern=fname,fileext=".pdf"))
@
<<custom-dev, echo=FALSE >>=
my_pdf<-function(file,width,height) {
  pdf(file,width=5,height=2)
}
@
<<plot, echo=FALSE, results="asis", dev="my_pdf", fig.ext="pdf">>=
# A ggplot chart
print(g) 
@

报告很好,但 knitr 的 sanitize_fn 会生成以下警告:

图路径中的点替换为 _ ("work/fname_pdfplot")

很明显,违规。来自 opts_chunk 中的 fileext。但是,如果我将该 fileext 更改为“_pdf”,我根本不会在报告中得到绘图,并且 Latex 会引发关于找不到文件 (fname_pdfplot-1) 的错误。

关于如何(a)正确执行此操作以便没有警告,或(b)按照我正在执行此操作但抑制此特定警告的想法?

编辑1:

这是一个不使用 fileext 的 source.Rnw 的工作示例。这似乎更接近,因为现在由于将 work\fname... 放在 includegraphics 而不是 work/fname... 中,它会因错误而中断,如果我将反斜杠更改为正确的斜杠,它会干净地编译。

tempfile 正在返回 work\fname...,所以也许我的解决方法只是重新转义那些反斜杠(或用正斜杠替换它们)。这是我应该知道的事情吗?

\documentclass[titlepage]{article}
\usepackage[utf8]{inputenc}
\usepackage[headheight=36pt, foot=24pt, top=1in, bottom=1in, left=1in, right=1in, landscape]{geometry}
\usepackage{hyperref}
\usepackage{bookmark}
\usepackage{fancyhdr}
\usepackage{longtable}
\usepackage{multirow}
\usepackage{float}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{microtype}
\usepackage{libertine}
\usepackage{parskip}
\usepackage{environ}
\usepackage{preview}
\usepackage[labelformat=empty]{caption}
\usepackage{amssymb}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{picture}
\usepackage{needspace}
\usepackage{adjustbox}
\usepackage{graphicx}
\pagestyle{fancy}
\raggedbottom
\renewcommand\familydefault{\sfdefault}
\newcommand{\helv}{%
\fontfamily{phv}\fontseries{m}\fontsize{8}{10}\selectfont}
\newcommand{\mycopyright}{\helv Copyright.}
\cfoot{\mycopyright}
\rhead{\textbf{\Sexpr{firstname} \Sexpr{lastname}} \\ \Sexpr{oafr} to \Sexpr{eoafr} \\ Page \thepage}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
\fancypagestyle{fancytitlepage}
{
\fancyhf{}
\cfoot{\mycopyright}
\rhead{}
\renewcommand{\headrulewidth}{0pt}
}
\linespread{1.2}
\usepackage{sectsty}
\allsectionsfont{\sffamily}
\partfont{\centering}

\makeatletter
\newcommand{\sectbox}[1]{%
\noindent\protect\colorbox{gray!40}{%
\@tempdima=\hsize
\advance\@tempdima by-2\fboxsep
\advance\@tempdima by-2\fboxrule
\protect\parbox{\@tempdima}{%
\smallskip
% extra commands here
\centering
#1\smallskip
}}}
\newcommand{\subsectbox}[1]{%
\noindent\protect\colorbox{gray!20}{%
\@tempdima=\hsize
\advance\@tempdima by-2\fboxsep
\advance\@tempdima by-2\fboxrule
\protect\parbox{\@tempdima}{%
\smallskip
% extra commands here
#1\smallskip
}}}
\makeatother
\sectionfont{\sectbox}
\subsubsectionfont{\subsectbox}
\makeatletter
\newcommand\cellwidth{\TX@col@width}
\makeatother


\newlength\foo
\NewEnviron{recipe}{%
\begin{adjustbox}{minipage=\linewidth,gstore totalheight=\foo, gobble}
\BODY
\end{adjustbox}
\needspace{\foo}
\BODY%
}


<<setup, echo=FALSE >>=
opts_chunk$set(fig.path = tempfile(tmpdir="work",pattern=fname))
@


\hyphenpenalty=100000

\begin{document}
\raggedbottom
\setlength{\parskip}{0pt}


<<custom-dev,echo=FALSE>>=
wkld_pdf<-function(file,width,height) {
  pdf(file,width=5,height=2)
}
@

<<wkld, echo=FALSE, results='asis',fig.align="center",dev="wkld_pdf",fig.ext="pdf">>=
if (!is.na(wkld.team) | !is.na(wkld.res)) {
  g<-pltr$workload.chart(wkld.team,wkld.res,firstname)
  print(g)
}
@


\end{document}

在上面的例子中,文件 work\fname61c28cd1a0awkld-1.pdf 是正确创建的,但是生成的 tex 有:

{\centering \includegraphics[width=\maxwidth]{work\fname61c28cd1a0awkld-1} 

}

因此没有找到它。

标签: rknitrsweavernw

解决方案


似乎省略 fileext 工作(并且可能将其设置为 _pdf 也是如此)以删除警告。

还需要用 / 替换 tempfile 生成的 \ 以防止生成的 includegraphics 调用发出另一个警告,因为在链中的某处, \ 被评估为 . 这有效:

opts_chunk$set(fig.path = gsub('\\\\','/',tempfile(tmpdir="work",pattern=fname)))

谢谢你帮我追查。


推荐阅读