我正在尝试从PHP运行R脚本。我创建了带壳的代码版本,产生了相同的错误消息。我正在VSCode,PHP版本7.3.9和R-3.6.2中运行。下面是我们的代码版本,它们演示了我的问题。
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)
[当我在VSCode中通过PHP运行命令时,结果变量接收到以下内容...
"Error in library(plyr) : there is no package called 'plyr'
Calls: suppressMessages -> withCallingHandlers -> library
Execution halted
"
但是,如果我在命令提示符中手动运行该命令,它将起作用。
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>
我只是超级困惑为什么从VSCode / PHP中运行时无法识别软件包
贷项转到Rscript: There is no package called ...?
但总而言之,我做了以下工作1)在命令提示符下启动R.exe,然后键入以下命令以获取软件包的安装位置Sys.getenv('R_LIBS_USER')
2)将以下行粘贴到R脚本的顶部,以便Rscript.exe可以引用
.libPaths(c(.libPaths(),pathFromStepOne))