简体   繁体   English

使用热键执行 R 脚本

[英]Execute R script with hotkey

Let's assume I have an R script located in C:\Users\user\myscript.R .假设我有一个 R 脚本位于C:\Users\user\myscript.R中。 How can I assign a hotkey (eg, F1 ) so that every time that I press that hotkey the R script will be executed in the background (ie, without opening Rstudio)?如何分配热键(例如F1 ),以便每次按下该热键时 R 脚本将在后台执行(即,无需打开 Rstudio)?

Note:笔记:
I use Windows 10 and have AutoHotkey installed which might help to bind the script to the key.我使用 Windows 10 并安装了 AutoHotkey,这可能有助于将脚本绑定到密钥。

This solution involves three steps:该解决方案包括三个步骤:

1) Create a .bat file that executes the R script (as suggested by Daniel O): 1) 创建一个执行 R 脚本的.bat文件(如 Daniel O 所建议):

runscript.bat (located in C:\Users\user\runscript.bat ) runscript.bat (位于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): 2) 使用开源软件AutoHotkey通过创建.ahk脚本将.bat脚本绑定到Home热键(如 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 ) bindscript.ahk (位于C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp所以它会在启动时自动加载,见这个

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

3) Set environment variable for R on windows 10 following this tutorial . 3) 按照本教程在 windows 10 上为 R 设置环境变量。

press win+r, type "shell:startup", and press enter.按win+r,输入“shell:startup”,然后回车。 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这将使触发脚本在启动时运行,因此每当您按下 f1 时,myscript.r 就会运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM