don't echo to R console - r

Is there a way to turn off/on echoing to the R console (without using source()?)
For example, let's say I have a long .R script and I wish to run only one line from it. Say that line is x <- 9.
In RStudio, I can go to the line in question and use the "Run Selected Line(s)" command from the "Code" menu (or keyboard shortcut ctrl-enter on my PC). What'll happen upon doing that is it the console will print x <- 9 (and then obviously, R will create a variable called x and assign it the value 9).
Is there a way to not have this line echoed in the console (but still create the variable)?
The reason I ask is that I have lengthy lines of code that just define functions and every time I want to update a function, it echoes the whole thing to the console, and that burns a lot of time.
Thanks.

Related

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

How to stop taking input from standard input in rstudio

I actually want to take input from R in script mode (i.e. I'll copy and run the program written in an RScript) and I am using the readLines function for that (since I've come to know that readline function is meant to be used only in interactive mode). When I run the following code (in rstudio),
k = as.integer(readLines("stdin",n=1))
x = c()
it starts taking input, but the problem is that it doesn't stop taking input. Even if I click on the red octagon, it is not stopping and neither am I able to quit the session. I've to restart the computer. Any help on how can we stop it?? (I'll also be very happy if you suggest some better function to take and process input in script mode)

My attempt to use a "connection" while trying to read in input causes R to freeze or crash

Sorry, but the terminology I use in the title may not be used correctly. Whenever I try to run this code, it seems like it is trying to run it but never completes the command. When I click the stop command sign (red), it doesn't do anything. I cannot close out of R. So why is this taking forever to run?
con <- file('stdin', open = 'r')
inputs <- readLines(con)
When working in RStudio, you need to use readLines(stdin()) rather than readLines(file('stdin')), though you can use either if running R in the terminal.
However, there is also an issue from not specifying the number of lines of input since you are using RStudio. When reading input from stdin, Ctrl+D signals the end of input. However, if you are doing this from RStudio rather than from the terminal Ctrl+D is unavailable, so without specifying the lines of input there is no way to terminate the reading from stdin.
So, if you are running R from the terminal, your code will work, and you signal the end of input via Ctrl+D. If you must work from RStudio, you can still use readLines(stdin()) if you know the number of lines of input; e.g.,
> readLines(stdin(), n=2)
Hello
World
[1] "Hello" "World"
An alternate workaround is to use scan(), e.g.:
> scan(,'')
1: Hello
2: World
3:
Read 2 items
[1] "Hello" "World"
(On the third line I just pressed Enter to terminate input). The advantage there is that you don't need to know the number of lines of input beforehand.
RStudio has a somewhat indirect connection to R (At least 4 years ago it redirected stdin to nowhere). It is probably, for our purposes, embedded. This is probably part of why stdin() can work when paired with readLines (It creates a terminal connection rather than a file connection). #duckmayr's scan() solution is quite nice and is documented to be the kind of thing that works in this situation...
the name of a file to read data values from. If the specified file is
"", then input is taken from the keyboard (or whatever stdin() reads
if input is redirected or R is embedded).
In case you want to consider a blank input 'okay', you can also loop over getting the data from a single line with some sentinel value, i.e. thing that makes the loop stop (here 'EOF').
input <- function() {
entry <- ''
while (!any(entry == "EOF")) {
entry <- c(readline(), entry)
}
return(entry[-1])
}

How to Run a Single Line in RStudio

I am used to using the standard R GUI but I'm trying to make the switch to RStudio. However, it's driving me nuts that I can't figure out how to run a single line with a keyboard shortcut. For instance, if I have the two lines,
c <- a +
b
I would like to press ^R to run the first line and then have to press ^R again to run the second line to complete the assignment. This is the default setting in the standard R GUI. Unfortunately, RStudio only seems to want to execute a statement to it's completion. Is there any way around this?
If you want this to be the default behavior, go to "Tools -> Global Options -> Code -> Editing" and uncheck the box "Execute all lines in a statement" under "Execution".
Select the first line (by clicking on it three times), then Ctrl-Enter will run the selection, not the statement.

Shortcut for running a single line of code in R

Does anyone know how I could create a keyboard shortcut or something similar to run a single line of diagnostic code in R Studio? i.e. if I wanted to do something simple, like checking the dimensions of a data frame, but I wanted to do it a lot throughout the day and didn't want to be continually typing dim(data), how could I get dim(data) into a keyboard shortcut or some other quick easy way to call that single line of code?
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).
The plain R terminal has a reverse incremental search function to make repetitive things easy. Hit Ctrl-R and start typing, it will match against your history. In this example, I've typed "di" and its enough to find the last "dim" call I did:
> x=matrix(1:12,3,4)
> dim(x)
[1] 3 4
> y=runif(100)
> dim(x)
[1] 3 4
# hit Ctrl-R at the prompt and type "d"... "i"....
(reverse-i-search)`di': dim(x)
I can hit return now, and it will do dim(x) for me. In fact it found it at the "d" because there was nothing else starting with a "d!" in the history.
There's a similar things in Emacs-ESS but I don't suppose you are using that. I don't know if this is implemented in RStudio, StatET, Architect, RCmdr or any of the other R interfaces that you might be using. I think RStudio might have a quick history search.
You could try using the snippet feature in RStudio (Tools -> Global Options... brings up the menu below). You can then add a snippet such as the code chunk below.
snippet d
dim(data)
Once the snippet is saved you can type the d (or whatever other string you defined after snippet). Then press tab and RStudio will give you the option to replace the shortcut string with the code listed in the snippet (here dim(data)).
There might be other options but for something as simple as a dim statement. There would likely be more effort than value add.

Resources