3

当我尝试“编织 HTML”或使用 LyX 生成 pdf 时,我遇到了环境问题。当我直接运行 R 代码(或使用 Rstudio 编译 HTML 笔记本)时,就可以了。我已经用 knitr@1.3.7 (dev version) 完成了测试。我对 knit@1.2 没有问题

在 (test.Rhtml > test.R > init.R > kCommon.R) 之后给出的示例中,返回的错误是:

Period: From

Error in eval(expr, envir, enclos) : objet 'kStartDate' introuvable

to
Error in eval(expr, envir, enclos) : objet 'kEndDate' introuvable

测试.R

## @knitr INIT
source("./init.R")
print(kStartDate)
print(kEndDate)

test.R 来源init.R

if (!require(xtable)){
  install.packages("xtable")
  library(xtable)
}
source("./kCommon.R")

init.R 来源kCommon.R

# Period
kStartDate   <- as.Date("2013-01-01", format="%Y-%m-%d")
kEndDate     <- as.Date("2013-06-30", format="%Y-%m-%d")

测试.Rhtml

<html>

<head>
<title>TEST</title>
</head>

<body>

<!--begin.rcode set-options, echo=FALSE
output <- "html"
read_chunk('test.R')
opts_chunk$set(fig.width=7, fig.height=4.5, fig.align='center', tidy=FALSE, comment=NA) 
end.rcode-->
<!--begin.rcode INIT, echo=FALSE, message=FALSE, warning=FALSE
end.rcode-->
<p>
Period: From <!--rinline as.character(kStartDate) --> to <!--rinline as.character(kEndDate) -->
</p>


<hr/>
<p>
This document has been generated with <!--rinline version$version.string -->
on <!--rinline version$platform -->.  <br/>
[<!--rinline date() -->]
</p>
<!--begin.rcode FIN, echo=FALSE, message=FALSE, warning=FALSE
end.rcode-->

</body>
</html>

会话信息

> library(knitr);sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] fr_FR.UTF-8/fr_FR.UTF-8/fr_FR.UTF-8/C/fr_FR.UTF-8/fr_FR.UTF-8

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

other attached packages:
[1] knitr_1.3.7

loaded via a namespace (and not attached):
[1] digest_0.6.3   evaluate_0.4.6 

formatR_0.9    stringr_0.6.2  tools_3.0.1   

结果(删除选项后)

output <- "html"
read_chunk("test.R")
opts_chunk$set(fig.width = 7, fig.height = 4.5, fig.align = "center", tidy = FALSE,
    comment = NA)
Period: From

Error in eval(expr, envir, enclos) : objet 'kStartDate' introuvable

to
Error in eval(expr, envir, enclos) : objet 'kEndDate' introuvable

This document has been generated with R version 3.0.1 (2013-05-16) on x86_64-apple-darwin10.8.0. 
[Thu Aug 8 10:35:22 2013]

#EOF#

日志

> require(knitr); knit('test.Rhtml', encoding='UTF-8');
Le chargement a nécessité le package : knitr


processing file: test.Rhtml
  |.........                                                        |  14%
  ordinary text without R code

  |...................                                              |  29%
label: set-options
  |............................                                     |  43%
  ordinary text without R code

  |.....................................                            |  57%
label: INIT
  |..............................................                   |  71%
   inline R code fragments

Error in eval(expr, envir, enclos) : objet 'kStartDate' introuvable
Error in eval(expr, envir, enclos) : objet 'kEndDate' introuvable
  |........................................................         |  86%
label: FIN
  |.................................................................| 100%
  ordinary text without R code


output file: test.html

[1] "test.html"
> 
4

2 回答 2

1

您在帖子中提供的示例代码test.R过于简化,您发送给我的脚本实际上是这样的:

##-------
## @knitr INIT
##-------
source("./init.R")

print(kStartDate)
print(kEndDate)

这会有所不同,因为外部代码块的新语法(在新闻中搜索----)。如果您只想在 RStudio 中折叠代码块,请参阅这个问题Code fold in external files with knitr and RStudio

于 2013-08-08T20:36:46.727 回答
0

解决此问题的一种方法是按按钮清除 knit 缓存或将此环境变量设置为 FALSE,请参见下文:

{r setup, include=TRUE, cache = FALSE}

干杯!

于 2017-03-01T04:08:57.423 回答