2

我有以下结构的文档。

\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}

当我在 R 中运行以下命令时,出现以下错误:

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

我不理解这个错误,并且似乎无法始终如一地复制该问题。我从一个空白文档开始,添加了一些工作正常的 R 代码。然后,我到达了它停止正确编译的地步。然后似乎我可以删除最近添加的代码块,但我仍然得到同样的错误。

会话信息:

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   

有什么建议么?如果需要更多信息,请告诉我。谢谢你。

4

1 回答 1

4

您的问题并非完全可复制,但它确实可能表明正在发生的事情。您有一个file.name传递给pdf()命令的变量,但 knitr 找不到它。在尝试运行您的代码时我也不能,因为它从未在 knitr 文档中定义。

当你编译一个 knitr 文件时,R 会从一个全新的空环境开始。如果您之前file.name在工作区中设置过,knitr 不会自动加载它。您需要先将其设置为一个块,然后才能使用它:

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

如果你正在加载一个文件,你可能不得不使用绝对路径或setwd()让它工作。

更新:

您并没有尝试实际评估text,因此 undefinedfile.name不是问题。我认为主要问题是您正在重命名内置pdf()函数。使用时出现错误pdf <- readPDF(PdftotextOptions = '-layout'),但当我使用不同的变量名(如pdf.asdf <- readPDF(PdftotextOptions = '-layout').

于 2013-05-07T16:38:51.767 回答