When I open a .R file and highlight a line and press Shift + Enter, VS code sends the line to R, but it mangles the text. For example, if I try to run
library(data.table)
Here is what I get back from R:
> 00~library(data.table)01~
Error: unexpected numeric constant in "00~library(data.table)01"
Strangest thing. VS Code works fine for me for Go, Typescript, Haskell, Python, but it doesn't like R. Any suggestions?
It looks like bracketed paste mode is on. Try adding this to your settings.json:
"r.bracketedPaste": false
Bracketed paste mode should usually be on if using the radian console but off if using the standard R console.
Related
When I select several lines of codes in a R script, and run it, RStudio will "smoothly" run all of the codes, even though there are some warnings and errors in the middle part. As a result, I have to carefully check the "Console" window, and to see whether there is any red lines. This is really time consuming and I may miss the errors. Are there some ways to make the running stop, when error or warning occurs?
RStudio currently works by pasting the selected text into the console. It doesn't care if there are errors in it. A better approach would be to get the text and source it.
You can get the selected text using
selected <- rstudioapi::getSourceEditorContext()$selection[[1]]$text
If you source this text instead of pasting it, it will stop at the first error. Do that using
source(exprs = parse(text = selected), echo = TRUE)
Another way to go would be to copy the text into the clipboard, then sourcing it from there. I don't think RStudio currently has a way to do that, but you can add one.
This function reads from the clipboard on Windows and MacOS; I'm not sure if pbpaste is generally available on Linux, but there should be some equivalent there:
readClipboard <- function() {
if (.Platform$OS.type == "windows")
lines <- readLines("clipboard")
else
lines <- system("pbpaste", intern=TRUE)
lines
}
This code sources the text from the clipboard:
source(exprs = parse(text = readClipboard()), echo = TRUE)
You could put either of these actions on a hot key in RStudio as an add-in. Instructions are here: https://rstudio.github.io/rstudioaddins/.
The advice above only stops on errors. If you want to also stop on warnings, use options(warn = 2) as #FransRodenburg said.
There are many ways you can force your script to stop when you encounter an error:
Save your script and run it using source(yourscript.R);
Wrap your script in a function and try and use the function;
Work in an Rmarkdown file and try and execute a chunk containing all the code you want to run (or try knitting for that matter);
If you really want to stop your script when a warning occurs, you could force warnings to be errors by using options(warn = 2) at the beginning of your script. If you just want to get rid of the red (lol), you can also suppress harmless warnings you have already checked using suppressWarnings(), or suppress all warnings for your script with options(warn = -1).
Be careful using options() outside a saved script though, lest you forget you have globally disabled warnings, or turned them into errors.
Depending on how large your script is, you may also just want to run it bit by bit using CTRL+Enter, rather than selecting lines.
In RStudio, I know that if I press F2 while the caret is on a function name I will be shown the source code for that function as if it was an open R script, which syntax highlighting and everything.
How do I do the same for operators like %in% and names like if? These things don't work:
Typing %in% into the script or console and pressing F2/Go To Definition.
Typing `%in%` (surrounded by backticks) into the script or console and pressing F2/Go To Definition.
The only way I can view their source code is by running `%in%` in the console, where it shows me an unhighlighted version.
Thanks!
You can see them by using View() with backticks around the argument:
View(`%in%`)
View(`if`)
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.
I would like to test code parts in my R Markdown code without leaving Sublime Text.
For instance:
Multiplying the grades with two solves the unreliability problem:
```{r}
chisq.test(2*grades)
```
In the above example, I would like to select the line that has the code "chisq.test(2*grades)", press my key combination, and have it ran in SublimeREPL as R code.
However, when I try this, I get the following error from SublimeREPL:
Cannot find REPL for 'HTML.markdown.rmarkdown'
When I change the syntax through view menu to "R" (rather than R Markdown), the code runs fine. This is a workaround though, and it is undesirable because it costs me R Markdown syntax highlighting.
I suspect the solution is simply copy-pasting a few lines of SublimeREPL package code and repurposing them for R Markdown, but I was unable to achieve any results yet. I'd appreciate any help.
From this answer :
Open the file SublimeREPL/config/R/Main.sublime-menu. Its default position depends on your system
Linux: ~/.config/sublime-text-3/Packages
Mac: ~/Library/Application Support/Sublime Text 3/Packages (untested)
Windows: %APPDATA%/Sublime Text 3/Packages (untested)
Add your scode to the option "additional_scopes":
"additional_scopes": ["HTML.markdown.rmarkdown","tex.latex.knitr"],
Save the file, close the REPL tab, restart sublime, and open a new REPL instance.
I am trying to run a Confirmatory Factor Analysis (part of the SEM package) in R but part of the syntax consists of using (<->) double-sided arrows. However, when I write "<->" in R, the program does not run and I get this: "Error: unexpected '>' in "amsex1<->" Thanks for your help!
library(SEM)
data<-read.csv("C:/Users/cgonzal6/Desktop/CYRUS/pilot-2-measurement13.csv")
factor<-data.frame(cbind(amsex1,amsex2))
cov.matrix<-cov(na.omit(factor))
cfa.model<-specifyModel()
EXTERNAL->amsex1,external0
Introjected -> amsex2, introjected0
amsex1<-> amsex1, error1
amsex2<-> amsex2, error2
The specifyModel() function reads the user input from the command line. It must be running in interactive mode to work. The <-> is not R syntax and should not be run as R code; that's just how specifyModel() wants' the model to be described in a text format. You can interpret everything after the specifyModel() to the next blank line as a big long character variable.
I assume you're trying to source() this script or run it from the command line? In a non-interactive mode, you can save the model specification in a file and read it with specifyModel(file="filename.txt"). That should also work in interactive mode as well.