I am using RStudio and recently noticed that when I use the "run" button it will not display comments in the command but just skips the line and goes to the next executable statement. I can run the entire script (with echo) and it displays the comments but any time I try and run a single line with the "run" button it will skip over comment lines. For example if I click the run button three times to execute these three lines of code:
4 + 4
#This is a comment
5 + 5
Console display:
>
> 4 + 4
[1] 8
> 5 + 5
[1] 10
This quirk just happened recently. I used the run button successfully on comments yesterday, is there a setting that might have changed or that I can change to continue to show comments on the console with the "run" button?
I've also noticed this evolution in the latest RStudio version (1.0.36).
Actually the run behavior has also changed in 2 other ways:
When the cursor is anywhere in a multi-line command, clicking "Run" (or pressing Crtl + Enter) now executes the entire command.
When the cursor is anywhere in a comment block, clicking "Run" executes the first command below it.
To come back to the old behavior, go into:
Tools > Global Options... > Code
and uncheck Execute all lines in a statement.
Related
Can it be done?
"Undo" only seems to work for blocks/lines of code within a cell
Cheers
Option 1
Edit > Undo Cell Operation (or using the shortcut Z in the command mode - to access the command mode press ESC)
or
Edit > Undo (or using the shortcut CTRL+Z)
Option 2
Alternatively, if one didn't restart the kernel, one can export the IPython history by running
%history -g # If one wants to give a name to the file add "-f NAME"
Or access the content of all cells (including deleted ones) with _ih such as
_ih[-10:]
Option 3
If one already restarted the kernel, File > Revert Notebook to Checkpoint
Checkpoints will be stored in .ipynb_checkpoints so one can look inside the files and see if the deleted cell is there.
I am using Sublime to work with R via Repl, all works fine...
But I wonder if its possible run commands lines from cursor until the begin of script without select lines above the cursor...
Because when I select lines above the cursor to run script, the cursor dont back to my last place that I was working and sometimes its dificult find my last place to continue with my work...
I am not expert programer and probably I miss a lot of tips to do it.
Below the cursor exist other command lines that I dont wanna run:
rm(list=ls())
library(dplyr)
library(readxl)
library(rtf)
library(ggplot2)
library(data.table)
df=mtcars
View(df)
| #Cursor here, only run codes above it
str(df)
summary(df)
names(df)
To select the lines, hold down Shift and either use ↑ to select all the above lines, or simply click at the beginning with your mouse. The keyboard shortcut Ctrl,, S (hit Ctrl+comma, release, and hit S) will eval the selection in SublimeREPL. Once you switch back to your code with the selection, simply hit → (right arrow) and your cursor will be back where it was at the end of the selection.
To answer your question: No, there is no way to evaluate all the content above your cursor in a REPL via a command or keyboard shortcut. You can evaluate the whole file, the current line, the current selection, or the current block of code.
I am trying to pause my code for a little while, time for me to observe the plots.
I tried:
print('A')
something = readline("Press Enter")
print('B')
print('C')
, then there is no pause, the line print('B') is fed to readline and get stored into something and therefore only A and C got printed on the screen. Note that if I add an empty line between Something = readline("Press Enter") and print("B"), then print("B") get printed on the screen but still the console doesn't allow the user to press enter before continuing.
And I tried:
print('A')
Sys.sleep(3)
print('B')
print('C')
The program waits 3 seconds before starting and then run "normally" without doing any pause between print('A') and print('B').
What do I missunderstand?
Here is my R version: R 3.1.1 GUI 1.65 Snow Leopard build (6784)
The problem with readline is that if you paste your script into an R console, or execute it from eg Rstudio, the redline function is read and then the next line of the script is read in as the console entry, which in your case sets the value of something to print('B).
An easy way to get around this is to stick your entire code in a function, then call the function to run it. So, in your case:
myscript = function(){
print('A')
something = readline(prompt = "Press Enter")
print('B')
print('C')
}
myscript()
The output of this for me (in Rstudio, with R version 3.1.1):
[1] "A"
Press Enter
[1] "B"
[1] "C"
This has always felt like a bit of a hack to me, but it's essentially what the readline documentation recommends in its example.
I've never used sleep in my code, so I can't help you there.
Edit to clarify based on comments: This will only work if myscript() is the very last line of your script, or if it is manually entered into the console after running the script to generate the function. Otherwise, you will run into the same problem as before- the next line of code will be automatically entered.
I'm using Atom with soft wrap turned on. In most simple editors such as gedit, Ctrl-Down would be used to skip ahead to the true next line, ignoring any wrapped lines below (same as j and k in Vim).
However in Atom this shortcut produces the result of moving the line itself around, which is less useful to me. I'd like to remap Ctrl-Up and Ctrl-Down to move the cursor up or down to the next true line, as described above.
I'm familiar with editing my keymap file, but I simply can't find any command that would be the equivalent of moving ahead one full line.
You could write a custom command in your init.coffee like this:
atom.workspaceView.command 'custom:move-next-buffer-line', ->
editor = atom.workspace.getActiveEditor()
editor.moveCursorToEndOfLine()
editor.moveCursorRight()
And then just reverse it for moving to the previous buffer line. You can then map the custom command in your keymap, which you said you're familiar with.
If you're using the vim-mode-plus package, then just modify your keymap.cson file by adding
# except insert
# -------------------------
'atom-text-editor.vim-mode-plus:not(.insert-mode)':
# Motions
# -------------------------
'k': 'vim-mode-plus:move-up-screen'
'j': 'vim-mode-plus:move-down-screen'
See for details https://github.com/t9md/atom-vim-mode-plus/blob/master/keymaps/vim-mode-plus.cson
I would like to retrieve the previous command in my R console that started with a certain character. For example, i can just press the up key to get the last command. However, I'd like the last command that started with xyz for example. Is there a way to do this?
if you're on a linux distro, you can press ctrl+R and start typing, and then ctrl+R to toggle through search matches.
you can also do, history(pattern="^xyz"), but this would require an additional copy.