{r echo = FALSE, results = "hide"}
prepare <- function(code) {
file <- tempfile(fileext = ".c")
writeLines(code, file)
writeLines(c("
c", code, "```"))
file
}
This package provides a thin wrapper around `R CMD SHLIB`. It is aimed at people who need to create shared libraries programmatically from R, and may be particularly useful on Windows machines where R is not in the `PATH`. It is heavily influenced by [Gabor Csardi](https://github.com/gaborcsardi)'s excellent [rcmdcheck](https://github.com/MangoTheCat/rcmdcheck) package. Comapred with running `R CMD check` directly from the command line, or via `system`, `rcmdcheck` provides: * ability to suppress compiler output unless there are warnings or errors * detection of compilation failure, with sensible conversion into an R error * detection of compilation *warnings*, with conversion into R warnings * colour coded output on ANSI compatible terminals (via [crayon](https://github.com/gaborcsardi/crayon)) * an object that holds the results of compilation that can be inspected later The package provides only two functions. The `can_compile` function tries to compile a very simple program to determine if the compilation environment is feasible: ``` {r } rcmdshlib::can_compile()
It caches its result within a session so carries little cost to use.
The shlib
function takes a vector of filenmaes and creates a
shared library (.so
or .dll
) with them.
``` {r echo = FALSE, results = "asis"}
file <- prepare(c("#include
This file is stored in the temporary file `r file` ``` {r } file
Compile the file; this will print to the screen compilation output
unless you pass in verbose = FALSE
res <- rcmdshlib::shlib(file)
The res
object here is a list with two elements: $dll
contains
the full path to the created shared library:
res$dll
And $output
contains the compilation output (which is an object
of class compiler_output
which has custom format
and print
methods defined in the shlib
package).
res$output
If the compilation failed, then even when verbose = FALSE
you'll
get an error:
``` {r echo = FALSE, results = "asis"}
file_error <- prepare(c("#include
``` {r echo = TRUE, error = TRUE} res2 <- rcmdshlib::shlib(file_error, verbose = TRUE)
This is thrown as an R error so can be detected with try
, etc.
The shlib
function has options for controlling how compiler
output, warnings and errors are translated to R conditions:
stop_on_error
can be set to FALSE
to prevent shlib
throwing on
errorwarn_on_warning
can be set to FALSE
to prevent shlib
raising
warnings following compilation warningsverbose
can be set to FALSE
to prevent printing of any compilation
outputAdd the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.