Skip to content

[R-pkg-devel] (How) can you use R file i/o within Fortran code in an R package?

4 messages · Peter Green, Serguei Sokol, Peter Sørensen +1 more

#
I am developing a package based around several thousand lines of legacy 
Fortran code front-ended with a fairly minimal R function to format 
input and results. In its basic form it is on CRAN, but now I want to 
extend it in a way that will generate copious output over the course of 
the running time of the Fortran code, and I would prefer if possible to 
write this output to (several) files rather than return it through 
arguments to .Fortran.

I have a version using Fortran open, write and close (naturally avoiding 
stderr and stdout), which runs perfectly on several platforms, but 
generates a NOTE in R-hub and Winbuilder:

 > * checking compiled code ... NOTE
 > File ?Nmix/libs/Nmix.so?:
 >?? Found ?_gfortran_st_close?, possibly from ?close? (Fortran)
 >???? Object: ?Nmix-sub3z.o?
 >?? Found ?_gfortran_st_open?, possibly from ?open? (Fortran)
 >???? Object: ?Nmix-sub3z.o?
 >?? Found ?_gfortran_st_write?, possibly from ?write? (Fortran), ?print?
 >???? (Fortran)
 >???? Object: ?Nmix-sub3z.o?
 >
 > Compiled code should not call entry points which might terminate R nor
 > write to stdout/stderr instead of to the console, nor use Fortran I/O
 > nor system RNGs.
that will presumably cause it to fail submission to CRAN, for reasons I 
understand.

Is there a way that I can use R file i/o from within the Fortran code, 
possibly mediated through a C or C++ wrapper? As I am not fluent in 
these languages or all the concepts involved, if it is possible a recipe 
or template would be most appreciated. To fix ideas, (how) can I write 
100000 integers, separated by white space, to a text file?

My apologies if this is a well-known question already answered, possibly 
in the negative, but my search through the archives failed to find 
anything. Thank you.

Peter Green
#
Le 16/02/2022 ? 16:53, Peter Green a ?crit?:
I don't have an example in hand, just few pointers which could be useful.
First, how to print from Rcpp 
https://teuder.github.io/rcpp4everyone_en/060_printing_massages.html
This example use NumericVector type for an entry parameter which you 
won't need. Use just classical C types (double, int, ...) and pointers 
on them. You don't need either '//[Rcpp::export]' as you wont call this 
function from R but from Fortran.
Second, how to call C from Fortran, e.g. 
https://riptutorial.com/fortran/example/7149/calling-c-from-fortran
The fact that your printing function will be in .cpp file does not 
matter. C interface will work if you limit your parameters to C types.

Hoping it helps,
Best,
Serguei.
#
I have created a fortran interface to a few c functions (fopen, fwrite, fread, fseek) via iso_c_binding that is used in my fortran code. The code can be found here: qgg/bigreml.f90 at master ? psoerensen/qgg (github.com)<https://github.com/psoerensen/qgg/blob/master/src/bigreml.f90>
Perhaps this is useful in some way.
Kind regards
Peter
________________________________
Fra: R-package-devel <r-package-devel-bounces at r-project.org> p? vegne af Sokol Serguei <serguei.sokol at gmail.com>
Sendt: 17. februar 2022 10:04
Til: Peter Green <mapjg at bristol.ac.uk>; r-package-devel at r-project.org <r-package-devel at r-project.org>
Emne: Re: [R-pkg-devel] (How) can you use R file i/o within Fortran code in an R package?

Le 16/02/2022 ? 16:53, Peter Green a ?crit :
I don't have an example in hand, just few pointers which could be useful.
First, how to print from Rcpp
https://teuder.github.io/rcpp4everyone_en/060_printing_massages.html
This example use NumericVector type for an entry parameter which you
won't need. Use just classical C types (double, int, ...) and pointers
on them. You don't need either '//[Rcpp::export]' as you wont call this
function from R but from Fortran.
Second, how to call C from Fortran, e.g.
https://riptutorial.com/fortran/example/7149/calling-c-from-fortran
The fact that your printing function will be in .cpp file does not
matter. C interface will work if you limit your parameters to C types.

Hoping it helps,
Best,
Serguei.
______________________________________________
R-package-devel at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel
#
An example and test package:

https://bnaras.github.io/ftest/articles/frcall.html

-Naras
On 2/16/22 7:53 AM, Peter Green wrote: