R “.Rexec” file will only run on one PC

I created a .Rexec file that runs a series of scripts. I followed the instructions on the following website to create the R script executable filetype on my PC: Automated Data Collection with R - Making R Files Executable (under Windows)

I'll also include the instruction from the webpage in-text as its fairly brief

 Although it is reasonable that R scripts get opened in edit mode by default, it would be even nicer (once in a while) to run them with a simple double-click. Well, here we go ...

Choosing a new file extension name (.Rexec)
First, we have to think about a new file extension name. While double-click to run is a nice-to-have, the default behaviour should not be overwritten. In the Windows universe one cannot simply attach two different behaviours to the same file extension but we can register new extensions and associate custom defaults to those. Therefore we need another, new file extension.

To make the file extension as self-explanatory as possible, I suggest using .Rexec for R scripts that should be executable while leaving the default system behaviour for .R files as is.

Associating a new file type with the .Rexec extension
In the next step, we tell Windows that the .Rexec file extension is associated with the RScriptExecutable file type. Furthermore, we inform Windows how these kind of files should be opened by default.

To do so, we need access to the command line interface, e.g., via cmd. Click Start and type cmd into the search bar. Instead of hitting enter right away, right click on the 'cmd.exe' search result, choose Run as administrator from the context menu, and click Yes on the following pop up window. The windows command line should pop up thereafter.

Within the command line, type first:

ASSOC .Rexec=RScriptExecutable
... then ...

FTYPE RScriptExecutable=C:\Program Files\R\R-3.1.2\bin\x64\Rscript.exe  %1 %*
... while making sure that the path used above really leads to your most recent/preferred RScript.exe.

Testing
To test if everything works as expected, create an R script and write the following lines:

message(getwd())
for(i in 1:100) {
  cat(".")
  Sys.sleep(0.01)
}
message("\nBye.")
Sys.sleep(3)
Save it as, e.g., 'test.Rexec' and double click on the file. Now a black box should pop up, informing you about the current working directory, and printing 100 dots on the screen and terminate itself after saying 'Bye'.

Et voilà.

One more thing (or two)
While you are now able to produce executable R script files, note that it is also very easy to transform those back by simply changing the file extension from .Rexec to .R and vice versa.

If you execute your R scripts from the command line, you might want to save yourself from having to add the file extension every time. Simply register .Rexec as a file extension that is executable. The PATHEXT environment variable stores all executable file types. Either go to: Start > Control Panel > System > Advanced System Settings > Environment Variables and search for the 'PATHEXT' entry under System Variables and add .Rexec to the end of the line like that: '.COM;.EXE;.BAT;.Rexec', or go to the command line again and type:

set PATHEXT=%PATHEXT%;.Rexec 

It seemed to work and I was able to run my executable without any issue. However, when I copied the scripts over to another computer for testing, it doesn't run. I did do the same thing to the other PC's (from the link above) to create the .Rexec filetype. Yet, >Rexec files won't run. Not really sure where to go from here, anyone have more experience in this?

On the other PCs there is also a working copy of the R software?

Yes, R v4.1.0 and Rstudio are all installed.

I think it is necessary (but one time only) to do the statements

ASSOC .Rexec=RScriptExecutable
... then ...

FTYPE RScriptExecutable=C:\Program Files\R\R-3.1.2\bin\x64\Rscript.exe  %1 %*
... while making sure that the path used above really leads to your most recent/preferred RScript.exe.

on each of the other PCs.

Did you do that?

Yup, did that on all the PC's before running.

Then you have to switch to a person more clever than myself :cry:

Damn.... this is the last hurdle to clear before I can say that my script is done.

The weird thing is that the test.Rexec file the tutorial asks you to make runs without issue on the other PC's, but my script won't.

I am still waiting for the person cleverer than myself.
I assume such person may exist :grinning:

So I try again:

  • all PCs are similar (all Windows) ?
  • all the packages that you use are installed on all PCs ?
  • the directory/folder structure is the same on all PCs (for the relevant part)
  • the environment variables and R-options are the same on all PCs (as far as relevant)
  • your testscript works. You say that your target script does not run. What does that mean? It does not start? Or does it run but without the expected outcome?
  • In the latter case , what happens if you change your target script by inserting at the start a command that writes a text line to a specific file. After finishing of the script, do you see the expected text line in the file.
    If that is the case move the command to a place further up in the source script to find out where things go wrong.

Good luck.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.