简体   繁体   中英

LaTeX and knitr - Error inserting R Code

I have a document in the following structure.

\documentclass[11pt]{article}

\begin{document}
\title{Something}
\author{Andreas}
\date{May 8th, 2013} \maketitle

\section{Introduction}
\paragraph{...}

<<>>=
2+2
@

<<>>=
require('tm')
@

\begin{itemize}
  \item{asdf}
\end{itemize}

some text here.

\section{Intro}

More text.

\section{Other Stuff}

<<>>=
pdf <- readPDF(PdftotextOptions = '-layout')
@

<<eval=FALSE>>=
text <- pdf(elem = list(uri = file.name),
            language = 'en',
            id = 'id1')
@

\end{document}

When I run the following command in R, I get the following error:

knit('test.rnw')

processing file: test.rnw
  |.......                                                          |  11%
  ordinary text without R code

  |..............                                                   |  22%
label: unnamed-chunk-1
Quitting from lines 12-13 (test.rnw) 
Error in pdf_doc(file, cache = FALSE) : 
  'file' must be a character string or a file/raw connection

I don't understand this error and cannot seem to consistently replicate the problem. I have started from a blank document and tacked on pieces of R code which work fine. Then, I get to a point where it stops compiling properly. It then seems that I can delete the recently added chunks of code, but I still get the same error.

Session Info:

R version 3.0.0 (2013-04-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tm_0.5-8.3 knitr_1.2 

loaded via a namespace (and not attached):
[1] digest_0.6.3   evaluate_0.4.3 formatR_0.7    slam_0.1-28    stringr_0.6.2 
[6] tools_3.0.0   

Any suggestions? Please let me know if more information is necessary. Thank you.

Your question isn't entirely replicable, but it does potentially indicate what's going on. You have a variable file.name that gets passed to the pdf() command, but knitr can't find it. And neither can I when trying to run your code, since it's never defined in the knitr document.

When you compile a knitr file, R starts with a brand new empty environment. If you had previously set file.name in your workspace before, knitr won't load it automatically. You'll need to set it in a chunk before it can be used:

<<>>=
require('tm')
file.name <- #something#
@

If you're loading a file, you may have to play around with absolute paths or setwd() to get it to work.

Update:

You're not trying to actually evaluate text , so the undefined file.name isn't an issue. I think the main issue is that you're renaming the built-in pdf() function. I get the error when using pdf <- readPDF(PdftotextOptions = '-layout') , but it compiles consistently when I use a different variable name, like pdf.asdf <- readPDF(PdftotextOptions = '-layout') .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM