Run R script returns: guild: error running r: [WinError 193] %1 is not a valid Win32 application

I tried to run R script. I have guild.yml file inside my root directory (R project). Ia m not sure if guild can work for plain R scripts inside R project?

Here is my guild.yml:

r:
  description: Backtesting with BackCUSUM estimation of volatility structural breaks
  # exec: Rscript .guild/sourcecode/train.r ${flag_args}
  exec: C:/Users/Mislav/Documents/GitHub/alphar/R/volatilityR.R ${flag_args}
  flags:
    contract: 'SPY5'
    upsample: FALSE
    std_window: 30
    backcusum_rolling_window: 100
    backcusum_type: 'bq'
  output-scalars:
    cumulatice_return: 'cumulative_return: (\value)'
    sharpe_ratio: 'sharpe_ratio: (\value)'

When I execute the script with guild run r I get the error:

(base) PS C:\Users\Mislav\Documents\GitHub\alphar> guild run r
You are about to run r
  backcusum_rolling_window: 100
  backcusum_type: bq
  contract: SPY5
  std_window: 30
  upsample: no
Continue? (Y/n) y
guild: error running r: [WinError 193] %1 is not a valid Win32 application

The commented out exec I think is a better starting point. You want to run Rscript as your executable. The exec attr is different from main in that it is the actual command that Guild runs. The main attr is a Python specific implementation that knows about Python scripts. exec doesn’t know anything about R scripts.

You can see what Guild runs for any operation using the --print-cmd option:

guild run r --print-cmd

If you try to run that command directly in Windows, you should see the same error message.

Assuming that guild.yml is in <home>/GitHub/alphar/R I think this is what you want:

r:
  exec: Rscript .guild/sourcecode/volatilityR.R

Note the important relative path for the R script. You don’t want to use absolute paths for two reasons:

  • You lose the isolation benefits of strictly running the code copied to the run dir. You don’t want to load/run any code from your project. By restricting your runs to the run directory, you are assured that the run source code is what actually ran. If you run code from your project directory, you enter a race condition where changes to your project code can accidentally leak into the operation.

  • It makes your projects non-portable.

The relative path to .guild/sourcecode/... points to the default location where Guild copies project soure code. If you want to change that location, you can use the dest attribute of the operation sourcecode config.

r:
  sourcecode:
    dest: .
  exec: Rscript volatilityR.R

Personally I would recommend against this as I like to see only the input and outputs for a run in the run directory root. I present this example though to show the relationship between the source code arguments used in exec and the location of source code files relative to the run directory.

Maybe I need to go one step back and explain my intentions.

I would like to set up R project and use guild functionality. If I get it right, guild should be language agnostic. So I thought I can use R scripts in a similar way as I used python scripts. I know I won’t be able to use some functionalities in guid that are specific to python and I am OK with that.

Now, R projects usually have the following structure (figure 4.1): https://r-pkgs.org/package-structure-state.html

The file I want to run is in <home>/GitHub/alphar/R and the project root is <home>/GitHub/alphar.

I thought I can use guild by just making guild.yml in project root. and run files with guild run. I am not sure if this is true.

The alternative would be to developed a python project and use R functions inside it. My plan is to use both R and python functions inside my project and thought guild would be a great tool to use both.

I know it is recommended to set up guild project when I use python, but what if I use R project?

All of this is possible. I’ll reiterate a few key points from my previous post:

  • Use exec to execute any OS process. In the case of R, you want to run Rscript with the path to the applicable script.

  • Use the run source code rather than hard-code any paths to your project files.

You can put the Guild file in the project root. This will make it easier to spot and run. It will look something like this for an operation:

# guild.yml located in <home>/GitHub/alphar

op:
  exec: Rscript .guild/sourcecode/R/op.R

Where op.R is the R script you want to run, assuming it’s located in the R subdirectory.

When you try this, what are you seeing?

If I use your approach it returns:

You are about to run r
  backcusum_rolling_window: 100
  backcusum_type: bq
  contract: SPY5
  std_window: 30
  upsample: no
Continue? (Y/n)
guild: error running r: [WinError 2] The system cannot find the file specified

Additionally, the guild run r --print-cmd returns:

(base) PS C:\Users\Mislav\Documents\GitHub\alphar> guild run r --print-cmd
Rscript .guild/sourcecode/R/volatilityR.R --backcusum_rolling_window 100 --backcusum_type bq --contract SPY5 --std_window 30 --upsample ''

I have to say I don’t have .guild folder in my root. IS hat automatically added by guild run?

Rscript is the program that runs R scripts. You should verify that you can run this at all:

Rscript --version

If that runs without error, the issue is path related. If it doesn’t run, you need to make sure it’s installed and available on the path.

The .guild directory is in the run directory — not your project directory. Guild operations run within a newly created directory. You can see that directory when you run guild runs info. If you want to see all of the files associated with a run, use guild ls -a (-a is used there to list all files).

I suggest running the r operation in the Guild examples. That’s as simple as it gets. You’ll need to install the argparser library from R to run that.

Start R:

R

From the R shell:

> install.packages("argparser")

Then run the Guild example:

cd <guild src repo>/examples/languages
guild run r -y

The op should run without errors.

Once you have that working, you can iterate with your project. If you run into any other issues, please include the specific error messages with details, etc. here as you’ve been doing.

R bin folder was not in the path :see_no_evil:

It works now. Thanks.

Great! I’ll be curious to hear how your experience goes with R. I’m not aware of a lot of Guild users running R but I’d like to make this a smoother experience for those who do.