0

Premise: I know (a bit of) Python.

I'm working on a a R Sweave (R + Latex) document.

To compile and execute it from command line I need to execute the following commands from console:

R CMD Sweaver fn.Rnw
pdflatex fn.tex
okular fn.pdf

Where

fn

varies from time to time (but is the same in all of the instructions).

Since I'm doing this many times, I'd like to make the process automatic and as simple as writing something like:

script <fn>

where fn is the parameters used in the 3 single commands.

I think this is somehow possible in Python or Perl, but I don't know where to begin.

Thanks in advance

1
  • 2
    Perl is overkill for this. Do it in a shell script. Better yet, learn make, which is the right tool for the job. Commented Feb 27, 2014 at 14:31

2 Answers 2

2

Here's an example Perl script (untested):

#!/usr/bin/perl -w

use strict;
use warnings;

my $fn = shift or die "Usage: $0 function_name\n";

# I assume this needs to exist?
-e "$fn.Rnw" or die "$fn.Rnw does not exist.\n";

0 == system( 'R', 'CMD', 'Sweaver', "$fn.Rnw" ) or die "R failed.\n";
0 == system( 'pdflatex ', "$fn.tex" ) or die "pdflatex failed.\n";
0 == system( 'okular ', "$fn.pdf" ) or die "R failed.\n";

exit 0;

Write that to a file, make it executable with chmod +x, and you should be set.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried the script. Error " /usr/lib/R/bin/Rcmd: 62: exec: Sweaver: not found " . I assume 0 == system ('R' , ...) it's different than "R CMD Sweaver fn.Rwn". Can you explain me the system (...) row, please?
For e.g. the first system() call, it simply executes the 'R' command and supplies all other values as parameters. It's interesting that you get that error - is "Sweaver" another script or command? Is it a file that you own?
Sweaver is a library of R statistical software. I assume it's no more than a string which is internally handled to run a program (written according to R syntax)
Try using the full path to the binary R
1

You could write a script containing:

R CMD Sweaver "$1.Rnw"
pdflatex "$1.tex"
okular "$1.pdf"

If you name it foo.sh, you could invoke it by saying:

sh foo.sh fn

or

bash foo.sh fn

and the shell would use the positional parameter that was passed in order to perform the variable substitution.

1 Comment

Yes, shell script will be the way for this. here $1 will be the argument after <file-name>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.