Preview edits made in .Rnw file

15 views
Skip to first unread message

Ross Ahmed

unread,
Dec 31, 2014, 4:17:35 AM12/31/14
to kn...@googlegroups.com
I frequently make minor edits within .Rnw files. This editing includes formatting using LaTeX code and small changes to inline R code. To see what the edits look like in the PDF outputted by knitr, I need to run knit() and then convert the .tex file. If I need to make minor changes several times in quick succession, it quickly becomes tedious having to run knit() and then convert to PDF several times in quick succession.

Is there any way to preview what editing of the .Rnw file will look like in the PDF without having to compile? Ideally what I would like is for the PDF to automatically update as I write!

Jeff Newmiller

unread,
Dec 31, 2014, 4:21:58 AM12/31/14
to Ross Ahmed, knitr

Perhaps use LyX? Not my thing so don't pepper me about it, but there is an example on the knitr website.

On Dec 31, 2014 1:17 AM, "Ross Ahmed" <ross...@googlemail.com> wrote:
I frequently make minor edits within .Rnw files. This editing includes formatting using LaTeX code and small changes to inline R code. To see what the edits look like in the PDF outputted by knitr, I need to run knit() and then convert the .tex file. If I need to make minor changes several times in quick succession, it quickly becomes tedious having to run knit() and then convert to PDF several times in quick succession.

Is there any way to preview what editing of the .Rnw file will look like in the PDF without having to compile? Ideally what I would like is for the PDF to automatically update as I write!

--
You received this message because you are subscribed to the Google Groups "knitr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knitr+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yihui Xie

unread,
Dec 31, 2014, 12:06:50 PM12/31/14
to Jeff Newmiller, Ross Ahmed, knitr
If you use LyX, you still need to click the preview button over and over again.

This problem can be easily solved by using three tools: a PDF viewer
that does not suck in terms of locking the pdf file (i.e. anything but
not Adobe Reader), a shell script, and GNU make. Something like

#!/bin/sh
while true;
do make && sleep 1;
done

For make, you define how to compile .Rnw to .pdf (cf
http://kbroman.org/minimal_make/ if you are not familiar with make).
If you are using Windows, there must be something similar to the shell
script I typed above, and you need to figure it by yourself, or wait
for other people to help you.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name

Ross Ahmed

unread,
Dec 31, 2014, 12:33:22 PM12/31/14
to Yihui Xie, Jeff Newmiller, knitr
This sounds like what I need. How quickly does the PDF update—as soon as anything is edited?

Ross Ahmed
Ecologist
--
07875533906
Twitter: @RossAhmed
LinkedIn: Ross Ahmed

Yihui Xie

unread,
Dec 31, 2014, 12:35:25 PM12/31/14
to Ross Ahmed, Jeff Newmiller, knitr
You can tune the frequency. In my example, I used 1 second, i.e. sleep 1.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name


Ross Ahmed

unread,
Apr 8, 2015, 12:56:18 PM4/8/15
to kn...@googlegroups.com
Yihui - I now have a Make script that can convert an Rnw -> tex -> PDF.

You said that I should run something like the below to automatically have the PDF update.

#!/bin/sh 
while true; 
  do make && sleep 1; 
done

Is this a shell script? I have no experience with shell scripts - where exactly do I run/store the above shell script?

Yihui Xie

unread,
Apr 8, 2015, 1:03:43 PM4/8/15
to Ross Ahmed, knitr
Yes, it is a shell script (you need to run it via sh on *nix). It just
shows you the idea, and you can use any language to implement it, e.g.
in R, it can be

while (TRUE) {
system2("make")
Sys.sleep(1)
}

Press Ctrl + C or Esc (depending on your platform) to stop it.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name


Message has been deleted

Ross Ahmed

unread,
Apr 9, 2015, 10:05:18 AM4/9/15
to kn...@googlegroups.com
The while() loop is now also working. So I can now edit my .Rnw and see those edits in the PDF without having to manually compile the PDF. But...

...the time between making a change in my .Rnw file and seeing that change appear in the PDF is still taking more time than I would like. This is because my Rnw file contains a lot of R code, some of which is quite slow.

Yihui -  I have edited the while() loop you posted, so this is what I'm currently using:

while(TRUE) {
knit(foo.Rnw)
system(make foo.pdf)
Sys.sleep(1)
}

Note that knit() is converting Rnw -> tex outside of Make, and make is simply converting tex -> PDF.

Is there a way to do one of these things:

1. Only run knitr on R code that has changed in the Rnw file
2. Get Make to do entire process (Rnw -> tex -> PDF)

Yihui Xie

unread,
Apr 9, 2015, 10:22:09 AM4/9/15
to Ross Ahmed, knitr
Of course, make can do anything, but you have to learn it in the first
place. As I mentioned early on, you may read this short tutorial:
http://kbroman.org/minimal_make/

Regarding the slow code in Rnw, you may use the chunk option cache = TRUE.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Web: http://yihui.name


On Thu, Apr 9, 2015 at 9:04 AM, Ross Ahmed <ross...@googlemail.com> wrote:
> The while() loop is now also working. So I can now edit my .Rnw and see
> those edits in the PDF without having to manually compile the PDF. But...
Reply all
Reply to author
Forward
0 new messages