1

I'm writing an R package which, among other things, defines a function which is actually a wrapper for a PHP script. Let's assume the PHP script offers some functionality that is considerably difficult to recreate in R and that my line of reasoning (wrapping a PHP script in R) makes sense.

I'm currently keeping the PHP script in a separate file, and running it via a system call.

My R function/wrapper looks like this:

wrapper <- function() {

  # I'm not entirely sure what the path to the PHP file should be
  php_file_name <- "magic_in.php"
  php_script_argument <- "hello, world"

  system_call <- sprintf('php -f "%s"', php_file_name, php_script_argument)
  system(system_call)

}

while the magic_in.php file is the following:

<?php

    print($argv[1]."\n")

?>

But this solution sucks - the system call works only if I have the PHP script in my current working directory.

I keep the wrapper in a ~/simple_package/R/wrapper.R file, but I'm not sure where to store the PHP script.

Am I supposed to keep the PHP file in a ~/simple_package/src directory and then call it using some dedicated R function (like .Call for C executables)?

ponadto
  • 702
  • 7
  • 17
  • Indeed, that's more suitable. – ponadto Feb 02 '16 at 13:52
  • It seems that the `scripts` directory (folder?) is NOT persistent in Windows. But the `exec` (as suggested by hute37 in his answer) directory/folder IS persistent. – ponadto Feb 08 '16 at 09:27
  • I tested that and it seems that no, not all secondary level directories are persistent, even if they aren't empty. – ponadto Feb 08 '16 at 12:14

2 Answers2

0

There is a number of ways of doing this. You could hard-code the function into the script, write it to a file, run it through a function wrapper with arguments (path is implicit when you create the file) and clean up afterwards.

A better approach would probably be to put the script in /data and call it via ?system.file.

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • It worked! The `data` directory is "persistent" (copied into the package's installation directory) unlike the `src` directory I tried initially. – ponadto Feb 02 '16 at 10:49
0

Probably /data is not the best option for package scripts, /exec could be a better choice.

See:

You may also consider using system2:

For some languages (Python), there is some special support in CRAN


An example (using system and system.file):

It is executed by an external R script command-line:

#!/bin/env Rscript

args <- commandArgs(TRUE)
if( length(args) < 2 ){
    stop( "usage : R CMD execute package script [parameters]\n" )
}
package <- args[1]
script <- args[2]
scriptfile <- file.path( system.file( "exec", script, package = package ) ) # <= path
if( !file.exists( scriptfile ) ){
    stop( sprintf( "file not found: '%s' ", scriptfile  ) )
}

trail <- if( length(args) > 2 ) paste( tail( args, -2 ), sep = " " ) else ""
cmd <- sprintf( '"%s" %s', scriptfile, trail )

system( cmd ) # <= or system2 ...
Community
  • 1
  • 1
hute37
  • 197
  • 2
  • 8