Question

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

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top