Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

utilities

Printing, reading and writing compiled function objects


Description

moveDLL moves the DLL used by a compiled function to a user defined location.

writeCFunc saves a CFunc object after the DLL has been moved to the desired location using moveDLL.

readCFunc reads a CFunc object that has been saved using writeCFunc.

The print and code methods respectively print the entire object or only the code parts.

Usage

moveDLL(x, ...)
## S4 method for signature 'CFunc'
moveDLL(x, name, directory, unload = FALSE, overwrite = FALSE, verbose = FALSE)

writeCFunc(x, file)
readCFunc(file)

## S4 method for signature 'CFunc'
print(x)
## S4 method for signature 'CFuncList'
print(x)

## S4 method for signature 'CFunc'
code(x, linenumbers = TRUE)
## S4 method for signature 'CFuncList'
code(x, linenumbers = TRUE)

Arguments

x

A CFunc or CFuncList object as created by cfunction

name

The base of the file name that the DLL should be moved to. The file name extension will depend on the operating system used

directory

The directory that the DLL should be written to

unload

In case the new path constructed from name and directory points to a loaded DLL, should we unload it?

overwrite

In case there is a file at the new path constructed from name and directory should we overwrite that file?

verbose

Should we print a message stating where the DLL was copied if the operation was successful?

file

The file path for writing and reading the object generated by cfunction. Consider using a file name extension like .rda or .RData to indicate that this is a serialized R object.

linenumbers

If TRUE all code lines will be numbered.

...

May be used in future methods

Details

If you move the DLL to a user defined location with moveDLL, this will prevent removal of the DLL at garbage collection and, if not written to the session tempdir, removal at session termination. However, saving and reloading an object will still loose the pointer to the DLL.

Only if their DLL has been moved, CFunc objects can be saved by writeCFunc and restored by readCFunc.

Value

Function readDynLib returns a CFunc object.

Function writeDynLib returns the name of the .CFunc file that was created.

Note

  • The code of a CFunc or CFuncList object x can be extracted (rather than printed), using:

    x@code.

  • To write the code to a file (here called "fn"), without the new-line character "\n":

    write (strsplit(x, "\n")[[1]], file = "fn")

Author(s)

Karline Soetaert and Johannes Ranke

See Also

Examples

x <- as.numeric(1:10)
n <- as.integer(10)

code <- "
      integer i
      do 1 i=1, n(1)
    1 x(i) = x(i)**3
"
cubefn <- cfunction(signature(n="integer", x="numeric"), code,
  convention=".Fortran")
code(cubefn)

cubefn(n, x)$x

moveDLL(cubefn, name = "cubefn", directory = tempdir())
path <- file.path(tempdir(), "cubefn.rda")
writeCFunc(cubefn, path)
rm(cubefn)

cfn <- readCFunc(path)
cfn(3, 1:3)$x

inline

Functions to Inline C, C++, Fortran Function Calls from R

v0.3.17
LGPL
Authors
Oleg Sklyar, Duncan Murdoch, Mike Smith, Dirk Eddelbuettel, Romain Francois, Karline Soetaert, Johannes Ranke
Initial release
2020-11-30

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.