I am dynamically generating pdf reports in R using a driver script that calls knit2pdf. My report source is latex, in a .Rnw file, and the call is like this:
knit2pdf("source.Rnw",output=paste0(fname,".tex"),quiet=T)
fname does not contain any dots.
source.Rnw contains:
<<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)
@
The reports are fine, but the following warning is generated from knitr's sanitize_fn:
dots in figure paths replaced with _ ("work/fname_pdfplot")
Clearly, the offending . is coming from the fileext in opts_chunk. However, if I change that fileext to "_pdf", I don't get the plot in the report at all, and latex throws an error about the file (fname_pdfplot-1) not being found.
Ideas on how to (a) do this right so there's no warning, or (b) do this as I'm doing it but suppress this particular warning?
Edit 1:
Here is a working example of source.Rnw without using fileext. This does seem to be closer, because now it breaks with an error due to putting work\fname... in includegraphics rather than work/fname..., and if I change the backslash to a proper slash, it compiles cleanly.
tempfile is returning work\fname..., so perhaps my fix is just to re-escape those backslashes (or replace them with a forward slash). Is this something I should have known to do already?
\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}
In the above example, the file work\fname61c28cd1a0awkld-1.pdf is correctly created, but the tex generated has:
{\centering \includegraphics[width=\maxwidth]{work\fname61c28cd1a0awkld-1}
}
and thus doesn't find it.
fileext
to "_pdf" and the pdf compiles fine with no error (or warning) messages. Can you provide more information? What is the filename of the plot pdf that was generated? What is the filename that\includegraphics
in the tex file is pointing to? – Weihuang Wongfig.path
is supposed to contain the prefix to the filename, not the whole name, so I'd just leavefileext
out of thetempfile()
call. I can't check this, since you didn't post a reproducible example. – user2554330