How to exit R in the middle of a command? - r

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

Related

R/Rstudio Frustration - Can't Stop Code Execution

I often include View() statements in my R scripts. If I accidentally forget the closing bracket at the end of the line, and then run the line of code from the script window using ctrl-enter, R just keeps trying to execute the remainder of my script. I don't know why it does that (rather than using the + symbol to prompt me to provide further input).
Moreover, I've tried to stop this by setting break points in my code - I can click on the LHS of the page and a little red circle appears. But the breakpoints don't seem to work - R just ignores them and keeps going.
The only way I can get out of it is by killing the process in the Windows task manager and then going back in afterwards. But it's wasting a lot of time.
Does anyone know how I can fix this please?
Thank you.
In effect, what your function is processing looks like that:
... %>% View(
lm(am~cyl, mtcars)
...
...
As R can't find the bracket for ) it includes remaining statements as input to View and searches for the bracket.
Solutions
Kind of depends on what you want to do with those scripts but if the intention is to run them in the background consider using callr. This package lets you run R from R and offers kill methods to kill the process you started that way.
On Windows pressing Esc should enable you to get back to the console but if it's a memory intense process it may be difficult.
You may try pressing Ctrl+c in order to kill the process.

Output/the next command prompt overwrites long command in R

I'm seeing a very strange issue with R (v 3.3.2, running on the command line of a CentOS 7 linux box). If I execute a command that goes over one line in the terminal, the output appears at the beginning of the second line of the command. This didn't happen with previous versions of R, though it does happen with later versions I've installed. I am using Putty as the terminal, but it also happens if I use xterm.
To describe it better, it looks like this:
> tmpvaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaal=1 [before hitting enter]
> tmpvaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
> aaaaaaaaaaaaaaaal=1 [after enter, typing overwrites the a's now]
If the multiline command outputs something, that output occurs on the second line and overwrites the a's. If there is an error, then the error message terminates properly and the next prompt follows the error message. So does anyone have any ideas? Thanks in advance!
Update: After some more experimenting, I've found another odd behavior. If I use CTRL-R to search for a previous multiline command, and then execute it, the output or subsequent prompt appears at the same location in the command as where the cursor was. Here's another example:
(reverse-i-search)`1':tmpvaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal=1[cursor on one before hitting enter]
[hit enter and then:]
>tmpvaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaal=1
aaaaaaaaal=>
Pretty strange. Hard to describe so its hard to find any evidence of other problems like this on stack-exchange or elsewhere. My feeling is it has to do with the readline library but it is strange because this doesn't happen in the shell command line, only in R as far as I can tell.
Update 2: I've found a potentially more revealing way to replicate this issue. If you manually add a bunch of newlines (by jamming CTRL-V CTRL-J repeatedly), then hit enter, the next prompt jumps right back up to underneath the last one. This is not what happens in the underlying shell (bash for me), or in previous versions of R that I can check.
Just to have the answer out there in case anyone else has this issue, the problem was that my system was using an old version of the readline library, 6.2, and the issue disappeared when updating it to the current version, 7.0. So it would seem that even though later versions of R will install with the outdated readline, they don't exactly work correctly.

paste single line command in R console without executing

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.

Extract what was printed deep in the R console

When I execute commands in R, the output is printed in the console. After some threshold (I guess, some maximum number of lines), the R console no longer shows the first commands and their output. I cannot scroll up that far because it is simply no longer there.
How can I access this "early" output if it has disappeared from the console?
I care mostly about error messages and messages generated by my own script. I do use a script file and save my results to a file, if anyone wonders, but this still does not help solve my problem.
(I have tried saving the R workspace and R history and then loading it again, but did not know what to do next and was not able to find what I needed...)

R unix terminal pager

I'm using R on OS X 10.6 and I prefer the unix console to the R.app for my work. If I issue a help command eg ?print the help content opens through a pager (i tried most and less), which however then hides out the content if I exit it bringing me back to the R input line.
What I really want is that the pager output stays on the screen even after I exit it back to R (hitting q).
I get this desired behaviour on other Readline-based Cli like psql for example, but not on R. Any hints on how this gets configured would be greatly appreciated.
The console pager that R uses can be set with the options function. With less, the -X option suppresses the terminal clearing at exit. So, if the less binary is located in "/bin" (not sure where it's located in OS X), this should work:
> options(pager="/bin/less -X")
If you want this to be the default behavior every time you start R, you can place the above command in your ~/.Rprofile file, which is run automatically at startup.
Alternatively, you can set a export LESS=-XF in ~/.bashrc and this will have an effect across all programs that use less as a pager. The F option would further exit straight away if the content is less than a page, which I find quite useful.

Resources