2
votes

Let's assume I have an R script located in C:\Users\user\myscript.R. How can I assign a hotkey (e.g., F1) so that every time that I press that hotkey the R script will be executed in the background (i.e., without opening Rstudio)?

Note:
I use Windows 10 and have AutoHotkey installed which might help to bind the script to the key.

2
Have AutoHotkey execute Rscript --vanilla filename? - Rui Barradas
I'm not sure, I only have a very basic knowledge of AutoHotKey, but I'll have a look. - mat
Maybe use Run - D. Pardal
I tried Run, C:\Program Files\R\R-4.0.0\bin\Rscript.exe, C:\Users\user\myscript.R without success. Maybe I'm missing something. - mat
Run a .bat file with "...\Rscript.exe" "...\myscript.R" written in it. - Daniel O

2 Answers

2
votes

This solution involves three steps:

1) Create a .bat file that executes the R script (as suggested by Daniel O):

runscript.bat (located in C:\Users\user\runscript.bat)

"C:\Program Files\R\R-4.0.0\bin\R.exe" CMD BATCH "C:\Users\user\myscript.R"

2) Bind the .bat script to the Home hotkey with the open-source software AutoHotkey by creating a .ahk script (as suggested by D. Pardal):

bindscript.ahk (located in C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp so it will automatically load-up on start-up, see this)

Home::Run, runscript.bat, C:\Users\user

3) Set environment variable for R on windows 10 following this tutorial.

0
votes

press win+r, type "shell:startup", and press enter. It should bring up file explorer. There, you can create a different script and write this inside it:

F1:: 
Run, C:\Users\user\myscript.R

This will make the trigger script run at startup, so that whenever you press f1, myscript.r will run