paste single line command in R console without executing - r

Since I have started using R I have noticed inconsistent behaviours when pasting a complete single command line in the R GUI console. Sometimes the command is executed, sometime is not. I tried to use the "paste commands only" but the command is still executed and the option does not seem to have any effect. I want to past a complete command and not execute it.
How do I control what happens when I paste a single line?
EDIT: i think the issue is that I am also copying from notepad the "enter" character, so that's what make the command run right away. Is there a way to avoid this?

If you do not copy a newline character at the end of the command, it will not be executed when you paste it. You will need to enter one manually.
Rather than copy/paste, you might want to use an editor where you can highlight the code you want to run and then send it to the R console via a button or shortcut key. The Windows R GUI has this feature, as does Rstudio.

Related

How to run Python, R, Octave or Julia using BowPad?

I try using a very light-weighted editor BowPad to edit and run codes.
Take running R as an example, which has been added to Windows PATH Environment Variable D:\R\R-3.6.2\bin\x64
I try to take some following steps:
I built a test.r writing some testing line codes as 1+2; plot(1:10)
Menu --> Run --> Configure custom commands
Command Name: run R
Command Line: D:\R\R-3.6.2\bin\R.exe $(SEL_TEXT)
$(LINE) The line where the cursor is
$(POS) the position where the cursor is
$(TAB_PATH) the path to the file of the active tab
$(TAB_NAME) the file name without extension of the current tab
$(TAB_EXT) the file extension of the current tab
$(TAB_DIR) the directory of the file of the active tab
$(SEL_TEXT) the selected text or the word at the cursor position
$(SEL_TEXT_ESCAPED) like $(SEL_TEXT) but escaped, useful for urls
When I press to choose run R to run the code, something wrong happens!
It will open a Rterm(64-bit) window each time when I run R!
it shows RGUMENT '1+2' __ignored__ or ARGUMENT 'plot(1:10)' __ignored__
Did I missing something ? BTW, can we set some shortcuts keys for running Python, R, Octave or Julia?
Also, when I reach the homepage of BowPad for more information, little usage of this software can be found!
R.exe refers to Rterm. You can use Rgui.exe instead but that won't automatically process arguments. I think what you likely want is to send the selected text to a running R session rather than starting up a new R session each time.
To do that
download this file and optionally place it on your path https://raw.githubusercontent.com/ggrothendieck/batchfiles/master/clip2r.js
in the Bowpad Run | Configure window configure a Run R command as the following (or if clip2r.js is not on your path use the entire pathname). It should be just like this with no arguments.
clip2r.js
From the Windows cmd line if Rgui is running we see that tasklist | findstr Rgui finds Rgui as the R gui process and you can create additional js scripts by editing clip2r.js replacing Rgui in the js script with whatever is the appropriate word to locate python, julia, octave or other R front end assuming again that they are running.
Now to invoke it from within Bowpad:
ensure that your Rgui session is already running and
from within Bowpad select the code you want to run and copy it to the clipboard and then invoke Run R. For example, to run everything use ctrl A ctrl C ctrl R 0 assuming that Run R is in position 0 in the configure menu. Alternately use the mouse with the Ribbon.
Regarding the comment about difficulty finding help, it looks like Bowpad is based on Scintilla so the documentation for it and the related SciTE editor likely apply to Bowpad too. You can also check the Bowpad source code on github.

How to exit R in the middle of a command?

I am trying to run code in R and I have repeatedly run into the very frustrating issue that, unlike in Terminal when you can just press control-C on the keyboard, or Python when you can just enter exit(), in R it does not appear to be possible to exit a command you are currently typing to get back to the basic command line-type interface. That is, when I'm typing some code in R, I have a bunch of plus signs to the left of what I'm typing rather than > symbols, and, rather than execute what I have typed, I want to exit it and return to having > symbols and thus be able to type a whole new command while keeping the R window and workspace open, I find it impossible to do so. Instead I end up having to just close the entire R window, save the workspace, and reopen it which is way more steps than I'm sure are necessary. I already looked around online trying to find a way to exit R commands in the situation I described, and I found:
1. "stopifnot("something that is not true")", which worked the first time I tried it but not the second (why? no idea)
2. "stop("string")", which did nothing when I tried it
3. "quit()", which also did nothing

Use VBA to send a command to RStudio's Console

I want to do these steps in a macro:
Open Rstudio if it's not open yet.
Activate Rstudio
Put a command into the Rstudio's console.
Hit enter, telling Rstudio to run the given command.
I can do (1) and I can theoretically do (3) with Sendkeys if I can do (2), but (2) isn't working.
These two lines are the basic idea but using notepad instead:
PID = Shell("C:\Windows\system32\notepad.exe", vbNormalFocus)
SendKeys "source('C:/Users/Athena/Documents/helloworld.r')", True
Here a step (2) isn't even necessary since VBA sends the string in sendkeys directly into notepad like how I want. But if I try the same code with RStudio, the string gets pasted into the VBA code instead (or into the excel book if I used a button to activate the macro).
So in my mind, I want to force VBA to put focus on RStudio, then sendkeys will work as intended, and then I can do (4) somehow, telling RStudio to run the code I sent it.

Is there any way in Linux to show what's going on about the freezing code on the R session?

I am running a set of selected code on R. Like
source("/tmp/r-plugin-honli/Rsource-2704-quantmod.R")
There is no output. Only the prompt '>' flickered there.
I use 'killall' to kill the R session. But I don't know where is wrong on the code. Because R did not give any output. How could I know what's going on about the code.
I'd try two things:
Run the code interactively. As in, open the Rsource-2704 file and run its lines one by one.
If that doesn't replicate the problem or is not possible, you can take Joshua Ulrich's suggestion or use:
R CMD BATCH --vanilla Rsource-2704-quantmod.R out.log
Which will run the code in a batch mode and output the usual console lines to a file called out.log (you can name it whatever you like).
Instead of using print statements, you could also take a look at the browser() command. This drops you into an interactive session at the point where the command is put. This works particularly well when trying to figure out what is happening inside a function, although I don't know if your script contains them.

Preventing what was/were previously copied on to my clipboard from appearing on R Console when using R

I am having some problems with my clipboard contents when using R. When I run my scripts/commands in tinn-r, very often I would get something that I had ran earlier pasted onto my R Console instead of the command that I have just selected.
To get over this, I would go to my clipboard and delete its content(text/syntax). However, the same text/syntax that I had just deleted would re-appear on my clipboard and would again appear on my R Console when trying to run a different syntax from my tinn-r.
Good luck. Tinn-R seems to be a particularly buggy way of interacting with R. Though it takes some effort to set up, the StatET plugin interface from Eclipse has been working way better for me than Tinn-R ever did.

Resources