[R] Using .Fortran in R - how can I use file i/o flexibly OR arrays of variable length as arguments?

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Sat Feb 5 09:09:25 CET 2022


On Fri, 4 Feb 2022 10:21:11 +0000
Peter Green <mapjg using bristol.ac.uk> wrote:

> the output I want to return to R is copious, and variable in length,
> the length being computed within the Fortran code (it is a
> variable-dimension simulation)

Is there a way to separate the calculation of the output size from the
rest of the subroutine? If yes, the simplest option would be to pass
pre-allocated vectors to the .Fortran() call.

Unfortunately, .Fortran() is not expressive enough for more complicated
use cases. Memory that you intend to return to R must be allocated
using its own allocator/garbage collector system, which (like any other
call into R from user code) may longjmp() away from your functions on
interrupt, error or allocation failure, abandoning any resources the
garbage collector doesn't know about. In turn, this requires the use of
C and the PROTECT() macro. In theory, you could use Fortran 2003 "C
interoperability" to call C entry points, but I expect that to be very
inconvenient (at least due to the lack of CPP macros).

Since you mention CRAN checks, this might be a better question for
<r-package-devel using r-project.org>, not R-help.

-- 
Best regards,
Ivan



More information about the R-help mailing list