Running an R Script Using a Windows Shortcut

Running an R script using a Windows shortcut

Read the help file ?Startup for details on what R goes through in the startup process and how you can automate running code.

I have done things like this for clients where I create a GUI for a specific demonstration (I use tcltk, but all should work the same). I created a desktop shortcut for them and modified the shortcut to start in a specific folder (but run the standard Rgui program), then in that folder I save a .Rdata file with all the code and data that the demo needs along with a function named .First that starts the demo.

Then the client does not need to know anything about R, just double click the shortcut and R starts and my demo starts automatically for them, they enter some numbers, click some options, slide some sliders, etc. and click on "OK" to see a graph or other output customized to their situation.

How to create shortcut to Rscript on Windows 7

You probably want

"C:\Program Files\R\R-3.0.2\bin\x64\Rscript.exe" --vanilla C:\Users\Moo\Desktop\CharCalendar.r

as your target. No -e; that specifies an expression to run, not a script file.

How to run an entire R script at once

Assuming your script is named "myScript.r", you can use something like source("myScript.r", echo = TRUE) to run the entire script.

Changing Cntrl + R shortcut for Running scripts in R Windows GUI

The short answer is:

"No, there are no [built-in] ways to alter the menu shortcuts in the R Console"

I'm however gathering here -community wiki style- some of suggestions posted as remarks to this questions.

One approach may be to download the R source, hack it (see circa line 625 of src/gnuwin32/editor.c: ), and build the R binary anew (see the R for Windows FAQ for the tools you need to build from source). This seems to be a rather radical approach for the mere convenience of using an alternate keystroke sequence...

A similar approach may be to create an automatic patcher program which would patch the R executable, by locating the byte patterns surrounding the compiled logic of editor.c mentioned above and replacing it with a byte sequence for the desired keystroke. This solution may be sensitive to changes in the binaries, but also avoids the build process altogether...

An easier way to achieve this is probably by using an external text editor. Most modern editors have macros or configs that can be used, for example, to execute a source command in R for the selected text.

Shortcut for running a single line of code in R

R itself can’d do that. Your editor may be able to, though (I know that Vim + Vim-R can do something like this).

What you can do in R is bind a function to an active binding. That way, whenever you invoke the binding, it executes your piece of code. To illustrate:

makeActiveBinding('x', function () dim(data), globalenv())

Now whenever you enter x in the R console, it executes dim(data).

R Studio Keyboard Shortcuts to Execute Specific Code

You can use the following keyboard shortcuts:

Ctrl+Alt+B runs the code from the beginning of the document up to that line. (Question #1)

Ctrl+Alt+T to run the code in each section (in the current section your cursor is). (Question #2)

https://support.rstudio.com/hc/en-us/articles/200711853-Keyboard-Shortcuts

Can I open a file to make R run the code within?

An alternative to using a batch file is to do it entirely in a Windows shortcut.
For instance, on my Windows system:

  1. Create a shortcut to c:/path/to/bin/Rscript.exe.

    For me, Windows had a "wizard" help me do this, which forced me to just identify the executable. From here, either Alt-Enter on the link or right-click and select Properties, then continue:

  2. Change "Start in" to the appropriate directory. If you are relying on specific paths for dependencies, then make sure you set this (and/or use setwd(...) within your R script).

  3. Add the filename to the command line in "Target", perhaps something like C:\R\R-3.3.3\bin\Rscript.exe myscript.R. In this example, myscript.R must exist in the directory in "Start in", though there's nothing stopping you from hard-coding the full path.

Using the link in @neilfws's comment or information here (How can I read command line parameters from an R script?, both Marek's and Dirk's answers), you can easily make it react to run-time arguments. For example, if your script includes:

opts <- commandArgs()

then opts will be a character vector with

c("C:\\R\\R-3.3.3\\bin\\x64\\Rterm.exe", "--slave", "--no-restore", "--file=myscript.R", "--args", "C:\\path\\to\\dragged_file")

and your script can "react" to the file (or directory) dragged onto the icon.

One issue with all of this is that it is entirely non-interactive. If you need popups, graphics, or other "discourse" with the user, you'll need something a little different.

How to run code section by section in RStudio?

Check out R Notebooks in recent versions of RStudio. Then you can put your code in different chunks and run them as you please.

An R Notebook is an R Markdown document with chunks that can be executed independently and interactively, with output visible immediately beneath the input.

If you must use an R script, I usually just highlight the multiple lines of code I want to run at once and do a Cmd + Enter or Ctrl + r, depending on what OS I'm using.



Related Topics



Leave a reply



Submit