Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Calling R codes from PERL using system coomand

by anikng (Initiate)
on Jul 21, 2014 at 13:07 UTC ( [id://1094486]=perlquestion: print w/replies, xml ) Need Help??

anikng has asked for the wisdom of the Perl Monks concerning the following question:

Hi Members... I am in confusion that how to integrate R codes in PERL program using system command (not through any additional packages). Specifically my PERL code generate microarray data (in .csv format) and save the data in a file "array_data.csv". I wanted following R code to generate a microarray heatmap. I have attached R scrip to generate hetamap. I am working on Ubuntu 14.04 Desktop. Perl V 5.18. If someone can just show an example, it would be helpful for me. Thanks, anikng, ROK

>library(gplots) >library(RColorBrewer) >all.data <- read.csv("C:/Users/Anil/Desktop/sam.csv") >row.names(all.data) <- all.data$sample >all.data <- all.data[, -1] >data.prop <- all.data/rowSums(all.data) >scaleyellowred <- colorRampPalette(c("lightyellow", "red"), space = " +rgb")(100) >heatmap(as.matrix(data.prop), Rowv = NA, Colv = NA, col = scaleyellow +red)

Replies are listed 'Best First'.
Re: Calling R codes from PERL using system coomand
by AppleFritter (Vicar) on Jul 21, 2014 at 13:34 UTC

    Howdy anikng, and welcome to the Monastery!

    I'm not familiar with R, but if you can't use non-core modules, this page suggests that you can invoke R like this to execute commands from a file:

    R CMD BATCH /path/to/file.R

    Alternatively, there is a module, Statistics::R, to interface R with Perl. So if installing that is an option, you could also do this:

    use Statistics::R; my $R = Statistics::R->new(); my $csvfile = "C:/Users/Anil/Desktop/sam.csv"; my @R_commands = ( 'library(gplots)', 'library(RColorBrewer)', "all.data <- read.csv($csvfile)", 'row.names(all.data) <- all.data$sample' 'all.data <- all.data[, -1]' 'data.prop <- all.data/rowSums(all.data)', 'scaleyellowred <- colorRampPalette(c("lightyellow", "red"), space + = "rgb")(100)', 'heatmap(as.matrix(data.prop), Rowv = NA, Colv = NA, col = scaleye +llowred)' ); $R->run(@R_commands); $R->stop();

    Totally untested, though.

    There's also a ->run_from_file() method if you'd rather read commands from an external file than embedding them in your Perl script.

      Thank u very much AppleFritter.. I will go on with Statistics::R. Your code gave me the idea!!! anikng, ROK

Re: Calling R codes from PERL using system coomand
by Anonymous Monk on Jul 21, 2014 at 13:55 UTC
    not through any additional packages

    You can install modules even if you're not an administrator / root, see for example local::lib. (Sometimes, there are legitimate reasons for not being able to use a module, but those are rare!) You really should try to install modules that will help you, such as Statistics::R from AppleFritter's answer.

    Running external commands is easier via helper modules, my two go-to modules are IPC::System::Simple as a drop-in replacement for system or IPC::Run3 for manipulating STDIN, STDOUT and STDERR.

    Having said that, how to use system is explained in its documentation. In your case it would look something like this (untested):

    system("R", "CMD", "BATCH", $R_FILENAME) == 0 or die "calling R failed: $?";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1094486]
Approved by AppleFritter
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-25 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found