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

Sokol Serguei @ergue|@@oko| @end|ng |rom gm@||@com
Thu Feb 17 10:04:54 CET 2022


Le 16/02/2022 à 16:53, Peter Green a écrit :
> 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?

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.

>
> 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
>
>
> ______________________________________________
> R-package-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel



More information about the R-package-devel mailing list