0

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"))
```
Cœur
  • 32,421
  • 21
  • 173
  • 232
  • 2
    Please post the top of markdown script instead of telling us as this does appear to be library issues. Try running an Rscript.exe from command line (not RStudio) that prints names of installed packages: `print(installed.packages()[,"Package"])` – Parfait Aug 28 '18 at 21:00
  • @Parfait I've edited the post to include the top of the markdown script. I've also run `Rscript.exe -e print(installed.packages()[,"Package"])` and it returned `object 'Package' not found` . I've run the same command without the `[,"Package"]` section and found rmarkdown in the list tho. – Itay Gurvich Aug 29 '18 at 15:20

2 Answers2

1

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)) 
Parfait
  • 87,576
  • 16
  • 87
  • 105
  • I've added `library(rmarkdown);` like you did in and I get `Error in library(rmarkdown): there is no package called 'rmarkdown'`. Also thanks for the Popen advice. I've waste too many hours working with os calls and fighting off format issues. The PATH env variable is like that because of access control limitations, so I can't set it. – Itay Gurvich Aug 29 '18 at 17:10
  • 1
    Check ALL your R library directories and see if `rmarkdown` is an included folder. If so, specify it in library call: `library(rmarkdown, lib.loc='/path/to/library')`. – Parfait Aug 29 '18 at 17:19
0

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.