1

I am attempting to run a R script from PHP. I have created a shelled out version of my code that produces the same error message. I am running in VSCode, PHP Version 7.3.9, and R-3.6.2. Below are shelled our versions of my code that demonstrate my issue.

index.php

<?php

$R = '"C:/Program Files/R/R-3.6.2/bin/Rscript.exe"';
$testScript = "C:/xampp/htdocs/rtest/testscript.r";

$command = "$R $testScript testingArguement 2>&1";

$result = shell_exec($command);

echo $result;

?>

testscript.r

## Define all libraries
suppressMessages(library(plyr))
suppressMessages(library(dplyr))
suppressMessages(library(tidyr))
suppressMessages(library(tidyselect))
suppressMessages(library(tidyverse))
suppressMessages(library(data.table))

## Suppress Warning Messages
options(warn=-1)

args = commandArgs(trailingOnly=TRUE)

testValue = args[1]

cat("The test value is",testValue)

When I run the command via PHP in VSCode, the result variable receives the following...

"Error in library(plyr) : there is no package called 'plyr'
Calls: suppressMessages -> withCallingHandlers -> library
Execution halted
"

However, if I run the command manually in Command Prompt it works.

C:\Users\Garrett>"C:/Program Files/R/R-3.6.2/bin/Rscript.exe" C:/xampp/htdocs/rtest/testscript.r testingArguement 2>&1
The test value is testingArguement
C:\Users\Garrett>

I am just super confused to why the packages aren't getting recognized when running from within VSCode/PHP

Garrett
  • 107
  • 10

1 Answers1

1

credit goes to Rscript: There is no package called ...?

but to summarize, I did the following
1) Start R.exe in command prompt, and type the following to get the location of where packages are installed

Sys.getenv('R_LIBS_USER')

2) Paste the following line at the top of your R script so that Rscript.exe can reference

.libPaths(c(.libPaths(),pathFromStepOne))
Garrett
  • 107
  • 10