简体   繁体   中英

“there is no package called 'rmarkdown” when calling R script from PHP

I have a WAMP server 3.1.3 running a HTML main page that takes a 'start' date, 'end' date and 'organization' text inputs. The submit button than passes the info to a following php script:

<?php
  $from = strtotime($_POST["fromdate"]);
  $to = strtotime($_POST["todate"]);
  $org = $_POST["org"];
  $python = "C:\Python27\python.exe ";
  $pyscript = "C:\wamp64\www\DMARC\Sample_Reports\GetOutlookAttachments.py $from $to $org";
  echo $python, $pyscript;
  chdir("C:\wamp64\www\DMARC\Sample_Reports");
  exec("$python $pyscript");
?>

The script takes the data, converts it to the proper format and passes it to a python 2.7 script. The data the python script receives is used to filter some logs. Once the logs are filtered the python script calls an R-markdown script to produce an HTML report. The Python code that calls the rmarkdown script:

    cmd = '"C:/PROGRA~1/R/R-3.5.1/bin/x64/Rscript.exe -e \"Sys.setenv(RSTUDIO_PANDOC=\'C:/Program Files (x86)/Pandoc\'); rmarkdown::render(\'../Filter\ Tool/report.Rmd\')\""'
    os.system(cmd) 

Before, I used to run the python script from cmd, giving it the arguments it needs and everything worked fine. The filtering done by python was correct, rmarkdown analyzed the logs and produced HTML reports. Now that I try running it from a PHP call I constantly get the following in apache_error.log:

Error in loadNamespace(name): there is no package called 'rmarkdown'

I have made sure that R uses the proper paths for libraries, stated the path in the top of the rmarkdown script. I made sure that what goes into exec() works by pasting it's value into cmd and it runs. Anything else I can try?

EDIT:

As requested here is the top of the markdown script:

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r include=FALSE}
library(XML)
library(methods)
library(readbulk)
library(zoo)
library(tidyr)
library(stringr)
library(data.table)
library(ggplot2)
library(plyr)
library(plotly)
library(IPtoCountry)
library(rworldmap)
library(knitr)
library(rmarkdown)
.libPaths(c("C:/Users/username/Documents/R/win-library/3.5", "C:/Program Files/R/R-3.5.1/library"))
```

Because rmarkdown is not among the default packages loaded with each R session such as utils , base , and stats , you need to call library(rmarkdown) line in your Python command call which runs outside your R script.

Consider also using Python's subprocss.Popen , a better handler for command line calls, to even capture console and error output with better quote handling (and even add Rscript to PATH environment variable and avoid specifying the full directory to the executable).

from subprocess import Popen, PIPE

# COMMAND WITH THREE ARGUMENTS
cmd = ["C:/PROGRA~1/R/R-3.5.1/bin/x64/Rscript.exe", "-e", 
       "Sys.setenv(RSTUDIO_PANDOC='C:/Program Files (x86)/Pandoc'); library(rmarkdown); rmarkdown::render('../Filter Tool/report.Rmd')"

p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)            
output, error = p.communicate()

if p.returncode == 0:            
    print('R OUTPUT:\n {0}'.format(output))            
else:                
    print('R ERROR:\n {0}'.format(error)) 

I feel like an idiot. The solution was to place .libPaths(c("C:/Users/username/Documents/R/win-library/3.5", "C:/Program Files/R/R-3.5.1/library")) at the top of the markdown script before all the libraries are loaded to indicate where to load them from. I was placing it at the bottom.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM