简体   繁体   English

R,Knitr和Latex(winedt和RStudio)的编译和外部化

[英]R, Knitr and Latex (winedt and RStudio) compilation and externalization

I am not new to Latex but to R, RStudio and Knitr which are quite powerful tools actually. 我不是Latex的新手,而是R,RStudio和Knitr,它们实际上是非常强大的工具。 I am working on a reasonably big R script which output several graphs. 我正在研究一个相当大的R脚本,该脚本可以输出多个图形。 My intent is to setup a latex document using Knitr so that i can continue working on my R script, and when I do want the report I jump on the .rnw and compile, and i am sure it will have the latest generated data since it would read the .r script directly. 我的意图是使用Knitr设置乳胶文档,以便我可以继续使用R脚本,并且当我确实想要报告时,我跳至.rnw并进行编译,并且我确信它会包含最新生成的数据将直接读取.r脚本。

First I'd like to ask some guidance on what would be the best practice to do that ? 首先,我想问一些指导,以寻求最佳做法是什么? I've been thinking about ## ---- preambule and do that for each relevant plot ? 我一直在思考## ---- preambule ,每个相关情节都这样做吗? Set of plots ? 套剧情? and place a read_chunk at the beginning to load the data and do all the preliminary stuff. 并在开始处放置一个read_chunk来加载数据并进行所有初步的准备工作。

Let's say this a good way of doing what I'm trying to do and move on to the problem I have. 假设这是一种很好的方式来做我要做的事情,然后继续解决我遇到的问题。

My script is running just fine in R without one single warning, however when read_chunk(file.R) is called with Echo=TRUE , i do not see the same outputs. 我的脚本在R中运行得很好,没有一个警告,但是当使用Echo=TRUE调用read_chunk(file.R)时,我看不到相同的输出。 First of all it does get through to the PDF but does generate the .tex that I can compile. 首先,它确实进入了PDF,但是确实生成了我可以编译的.tex。 The outputs that I see are much shorter as if it stops reading at some points. 我看到的输出要短得多,好像它在某些时候停止读取了。

The error I am getting is 我得到的错误是

processing file: minimal.Rnw

  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |.......                                                          |  11%
  ordinary text without R code


  |                                                                       
  |..............                                                   |  22%
label: external-code (with options) 
List of 1
 $ echo: logi FALSE


  |                                                                       
  |......................                                           |  33%
  ordinary text without R code


  |                                                                       
  |.............................                                    |  44%
label: preambule (with options) 
List of 1
 $ echo: logi FALSE


  |                                                                       
  |....................................                             |  56%
  ordinary text without R code


  |                                                                       
  |...........................................                      |  67%
label: boring-random

  |                                                                       
  |...................................................              |  78%
  ordinary text without R code


  |                                                                       
  |..........................................................       |  89%
label: boring-plots (with options) 
List of 3
 $ fig.width : num 4
 $ fig.height: num 4
 $ out.width : chr ".4\\linewidth"


  |                                                                       
  |.................................................................| 100%
   inline R code fragments


output file: minimal.tex

Error in regexpr("\\.([[:alnum:]]+)$", x) : object 'filename' not found
Calls: file_ext -> regexpr
Execution halted

However the script does not contain anything fancy. 但是该脚本不包含任何花哨的内容。 Just loading data, creating a few functions like SaveImage and that kind of things... 只需加载数据,创建诸如SaveImage类的一些功能,诸如此类...

I have to idea what in my script is using a regex... 我必须知道我的脚本中正在使用正则表达式...

Maybe it is not relevant but this is the output I get when echo=TRUE 也许无关紧要,但这是我在echo=TRUE时得到的输出

## Loading required package: methods
## Loading required package: MASS
## Loading required package: modeest
##
## This is package 'modeest' written by P. PONCET.
## For a complete list of functions, use 'library(help = "modeest")' or 'help.start()'.
##
## Loading required package: car
##
## Attaching package: 'lubridate'
##
## The following object is masked from 'package:plyr':
##
## here

And the next line if I run the same chunk in RStudio of the output is 如果我在输出的RStudio中运行相同的块,则下一行是

Attaching package: ‘lubridate’

The following object is masked from ‘package:plyr’:

    here

> library(knitr)
> 

I am probably not tackling this in the right away. 我可能不会立即解决这个问题。 All i need to do is setup a nice document using Latex that would compile bits and pieces from my R script which I would edit without taking care of the .tex doc, if that makes sense. 我需要做的就是使用Latex设置一个不错的文档,该文档将从我的R脚本中编译出一些片段,而这在我不需要照顾.tex doc的情况下就可以编辑,如果可以的话。 Except adding the right header ( ## ---- preambule etc). 除了添加正确的标题( ## ---- preambule等)。

So to summarize, I don't know where the problem with the regex is coming from, I still create a .tex that is ok for compilation, and by doing that I see that I am not getting the same outputs as in R directly running the script, as if it gets stuck somewhere... My naive thinking would tend to a library loading issue. 因此,总而言之,我不知道正则表达式的问题是从哪里来的,我仍然创建一个可以编译的.tex,通过这样做,我看到我没有得到与直接运行R相同的输出脚本,好像它卡在某处...我天真的想法将倾向于库加载问题。

Thanks for helping me, and best regards. 多谢您的协助,并致以诚挚的问候。

Thanks Paul, I have a way to do just what I want : 谢谢Paul,我有办法做我想做的事:

I use source(file.R) in the first chunk. 我在第一块中使用source(file.R)。 I use then read_chunk(file.R) in the second chunk and that is giving me the possibility to execute the script by portion using ## ---- portion 然后在第二个块中使用read_chunk(file.R),这使我可以使用## ---- portion按部分执行脚本

And then by putting several sections everywhere in my R script, I can just call the section I want by calling the chunk with the name of the section in the R script. 然后,通过在我的R脚本中到处放置几个节,我可以通过在R脚本中调用带有该节名称的块来调用想要的节。 Hope this is clear. 希望这很清楚。

However this is confusing that for instance fig.width is in inch and out.width can be in inch, cm or whatever but has to use the quote....... what a loose of time to find that out. 但是,这令人困惑,例如fig.width以英寸为单位,out.width可以以英寸,cm或其他任何形式使用,但是必须使用引号.......找到它的时间太少了。

Thanks again Paul, and i hope this will help other users. 再次感谢Paul,我希望这会对其他用户有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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