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

Dear Monks, When I try to open/create a file while using Statistics::R, attempts to create a file with open or sysopen fails when they are after startR(). Thus in the example below, test2.txt and test3.txt aren't created (but test1.txt is).
#!/usr/bin/perl use warnings; use strict; use Statistics::R; my $R = Statistics::R->new(); open FH, ">", "test1.txt" or die $!; $R->startR; open FH, ">", "test2.txt" or die $!; $R->stopR; open FH, ">", "test3.txt" or die $!;
Can I create files after startR? Should I do this in another way?

Replies are listed 'Best First'.
Re: in Statistics::R, startR breaks open
by syphilis (Archbishop) on Aug 19, 2009 at 01:03 UTC
    Not really set up to do any testing (and haven't looked at the module), but I wondered whether choosing different filehandles might make a difference - ie instead of specifying FH each time, specify FH1, FH2, FH3, etc.

    Cheers,
    Rob
      Thank you for your response. You are right that if I needed to use the file, different filehandles would be useful. However, unique filehandles aren't essential, and this problem persists with unique filehandles.
        Ok, it looks like startR changes the relative file path. So
        ... $R->startR; open FH, ">", "c:/scripts/test2.txt" or die $!; ...
        works fine (for me, the relative path goes to the directory "C:/Program Files/R/R-2.8.1/Statistics-R/"). I think relative path issues are discussed elsewhere. My workaround is:
        #!/usr/bin/perl use warnings; use strict; use Cwd; use Statistics::R; my $currentdir = cwd(); my $R = Statistics::R->new(); open FH, ">", "test1.txt" or die $!; $R->startR; $R->stopR; chdir($currentdir); open FH, ">", "test2.txt" or die $!;
        Note: I didn't actually need to open a file while communicating with R, but that wouldn't be hard to do. Thank you again.