简体   繁体   中英

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. I am working on a reasonably big R script which output several graphs. 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.

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 ? Set of plots ? and place a read_chunk at the beginning to load the data and do all the preliminary stuff.

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. First of all it does get through to the PDF but does generate the .tex that I can compile. 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...

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

## 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

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. Except adding the right header ( ## ---- preambule etc).

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.

Thanks for helping me, and best regards.

Thanks Paul, I have a way to do just what I want :

I use source(file.R) in the first chunk. 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

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. 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.

Thanks again Paul, and i hope this will help other users.

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