Shell Script -> R Script works in Terminal & Fails in KM

Hi everyone - I have a shell script that automatically runs an R script who's output is several RMD files, a CSV, an HTML file, and a screenshot of the rendered HTML file.

The R script uses the webshot2 package to open the HTML file in a headless chromium browser and capture the rendered output. When I run the shell file from the terminal everything works fine - but when I run it through KM everything works except that the screenshot file never appears.

Is there some KM permission item that I am missing? I am having trouble figuring out why I am getting this behavior.

Edit: I am not getting any errors when this runs...just not the desired output.

Tricky to tell without seeing the macro, but 99% of the time an "it works in Terminal but not in a KM 'Execute Shell Script' action" is because the $PATH environment variable is not the same in each (unless you change it yourself).

Simple fix -- use explicit paths. Run which webshot2 in Terminal and use the output in place of webshot2 in your KM shell script.

If you are going to use the "Execute Shell Script" action you should read the Wiki page carefully, especially the bits about using KM variables and paths.

The Shell script only calls the R script and the paths are all read from the R environment, or explicitly stated in the R script. When executed by KM the R script has no issues with the path to anything else (other files, package dependencies, functions, etc...) it is only for the webshot2 command.

Here is what is in my shell script:

#! /bin/bash
cd /Users/Brandon/Documents/R_Projects/DingerStats/
/usr/local/bin/Rscript ~/Documents/R_Projects/DingerStats/run_simulations_script.R

Here is the macro:

Execute a Shell Script Action (v10.2)

Execute a Shell Script.kmactions (817 B)

Keyboard Maestro Export

And there are no R-related environment variables in bash/zsh?

How are you calling webshot in your R script?

That's an R package, so I suspect @bstrain's environment has been altered by R.

Here's a macro to check for differences in the user's Terminal $PATH and their KM $PATH:

1 Like

Yes! That fixed the issue. Thank you!!! :grin:

1 Like

Can you elaborate on how you fixed it? In case it ever crops up again (or I suddenly get asked to use R for something at work!).

It's working as expected for me, but I suspect that's because I already have /usr/local/bin in KM's $PATH...

Used the macro that @ccstone shared to determine terminal $PATH and KM $PATH. Spoiler: they are different.

I added PATH= to the top of my shell script.

Tested to see if the script worked running from terminal (it worked)

Tested to see if the script worked running from KM (it worked)

I assume the webshot2 package was not available via the KM path but the other packages were, hence the reason I was getting all outputs except for the image created by webshot.

Can you remember/find the changes you made? Because I'm trying to break it by resetting $PATH to the default and it's still working!

cd ~/Desktop
/usr/local/bin/Rscript ./rscript.txt

...where rscript.txt is:

webshot("", "r5.png")

Here are the two paths.

Screenshot 2023-03-18 at 3.33.28 PM

Worth noting:

  1. all of my cd's are written in the R script (using R's setwd() function) not sure if that matters.
  2. I am webshotting a local html file that the R script spits out - not a webpage.
  3. I installed webshot via RStudio's package manager feature - are you using RStudio or another editor? I saw some forum posts with the same issue when folks tried to change from RStudio to vscode.