Testing Rmd files on Shiny with monit

Rodrigo Sousa Coutinho
Data Trekking
Published in
2 min readFeb 15, 2019

We are using monit to make sure our Shiny Server is running properly. We also want to make sure our applications and reports aren’t broken.

Testing “normal” Shiny applications can be achieved with check host, but Rmd files are trickier... they return a success page, and only then do they build the report, which later can lead to an undetected error.

Our solution was to use puppeteer to test these pages.

Installing puppeteer

This may differ from distribution to distribution. We are using CentOS, so the 1st thing was upgrading nodejs to a later version. This was done using nvm.

The commands look something like:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
source ~/.nvm/nvm.sh
nvm install 7
nvm use 7

Next, you can install puppeteer:

npm i puppeteer

The following script tests an Rmd file based on a URL:

Save the code to a file and run it using node test-Rmd.js <url>. You'll probably get an error like error while loading shared libraries, which means you're missing some dependencies... you can check which dependencies by running

ldd ~/node_modules/puppeteer/.local-chromium/linux-624492/chrome-linux/chrome  | grep not

For me, it was enough to install the following dependencies:

sudo yum install gtk3
sudo yum install libXScrnSaver

Note: The “ — no-sandbox” option launching puppeteer is not recommended, but since we’re using it to access our site only, it should be ok.

Once you have the script running, it’s time to configure monit.

Configuring monit

We’ll be using check program from monit which, unfortunately, doesn't take any arguments. So before we configure monit, we need to make a wrapper to our nodejs call.

You can do so by creating a shell script called test-Rmd.shwith something like:

#!/bin/sh
URLS=(http://<yourserver>/report.Rmd
http://<yourserver>/another_report.Rmd)
RES=0
for i in ${URLS[@]}
do
node /srv/shiny-server/ETLs/monit-validations/test-Rmd.js $i
if [ $? == 1 ]; then
RES=1
fi
done
exit $RES

Next, you just need to configure monit. Edit a file for the Rmd validation, e.g. sudo vi /etc/monit.d/rmdmonitor and call the script you just created.

check program RmdCheck with path /<path_to_script>/test-Rmd.sh
every 2 cycles
if status != 0 then alert

Reload monit (sudo monit reload) and you're good to go!

--

--

Rodrigo Sousa Coutinho
Data Trekking

Hi! I’m co-founder and Director of Data Science at OutSystems, with a passion for data, great products, and geeky stuff!