How to show R graph from visual studio code - r

I installed extension for R language
https://marketplace.visualstudio.com/items?itemName=Ikuyadeu.r
When i run my R code in visual studio code with (ctrl + enter) i get empty window instead of graph image.
but in visual studio 2017 it works

Please try the latest vscode-R Session Watcher. Plot, htmlwidgets, shinyapps, View(data.frame), help documentation, etc. all can be shown in VSCode.

Now it is possible.
Step 1:
a) Install httpgd from CRAN or github. From CRAN,
From CRAN:
install.packages("httpgd")
From GitHub:
devtools::install_github("nx10/httpgd")
b) Install languageserver:
install.packages("languageserver")
Step 2:
Install R extension for vscode
Step 3:
From vscode setting, select R > Plot: Use httpgd to use for R plot.
They have done a amazing job with R extension. We can view data.frame and list in a separate data viewer in vscode.

It looks like its opening an external graphics window. Try putting
windows();plot(stuff)
This explicitly pushes the plot to an external window

Related

Show R graph in Visual Studio code, not generating a file Rplots.pdf

I could not find a solution to this exact problem on the web. I have R-4.2.2 and the latest vs code 1.75. Have installed all the required packages and extensions, and followed the instruction https://code.visualstudio.com/docs/languages/r, but no avail. The relevant packages and extensions I have installed are languageserver package, R extension, vscDebugger package and R debugger extension, radian package (also enabled radian in json settings, set bracketedpaste to true, point the r.rterm.windows to the radian path), httpgd package (also set "r.plot.useHttpgd": true in json settings). But everytime I ran the code using Code Runner, it outputs the plot to a file named Rplots.pdf, instead of showing it inside the vs code interface. Thanks in advance!

Using R in Visual Studio Code with conda environment

I would like to use the R environment I installed with conda inside Visual Studio Code (on Macos). First I installed R with conda.
But how do I use/activate the environment in Visual Studio Code? In the settings I can't find the equivalent to "Python: Select Interpreter" or "python.venvPath"
Thanks!
R support in VSCode is handled by a 3rd party extension. The most popular one is R by Yuki Ueda and there is also R Tools by Mikhail Arkhipov
For both of these, you can change the R interpreter to use in the settings.
However, there is no built-in support for Anaconda, mostly because it isn't that popular or necessary in the R community. Most people use the standard R installation instead and most help resources are written for that type of installation: https://cloud.r-project.org/bin/macosx/
It has been 2 years since this entry and the extension still doesn't support conda environments.
For my configuration (I've R installed in a conda environment), I found a pretty painless work around:
open 'vscode'
install the extension and configure it as suggested using the conda paths for both R and, if you have it installed, radian
close 'vscode'
open a terminal
activate your conda environment
start vscode from your terminal using code
After this, everything seems to be up and running correctly. You can start an R terminal using the command palette and, as you run your code, you should be able to see all the information about the environment and namespaces as well as your plots.

"Debug location is approximate because the source is not available" in R 4.0.0 + RStudio

I've updated to R 4.0.0 and RStudio version 1.2.5042.
I'm developing a package and I've regularly used the following workflow:
Build the package from within RStudio by clicking the "Install & Restart" button (including the --with-keep.source R CMD INSTALL option).
Set a breakpoint in the .R file of the function I want to debug.
Call the function and wait for the debugger to stop exactly where I set the breakpoint.
This used to work nicely. Now, however, I always get:
Debug location is approximate because the source is not available
This is annyoing because I can still debug, but I'm not in the actual function anymore.
Any hints/ideas why this is?
Edit (11.05.2020)
It seems like this is an issue with RStudio and R 4.0.0. Here is why I think so. I used another machine running Windows 10 and did the following steps (in this order; should be reproducible for Windows users). At the outset I had R 3.6.1 and RStudio 1.2.5042 installed (at the time of writing 1.2.5042 was the current version; tried also with the preview version 1.3.957 of RStudio).
Clone the package I'm developing from Github:
git clone "https://github.com/M-E-Rademaker/cSEM"
Go to the package root and open cSEM.Rproj
Run devtools::install_github("M-E-Rademaker/cSEM") to automatically install the package and download all packages that cSEM depends on.
In RStudio: click on "Build" install and "Install and Restart"
Now naviagte to a source file. For example: R/00_csem.R and set a breakpoint in e.g. line 321.
Now run the following piece of code:
model <- "
# Structural model
eta2 ~ eta1
eta3 ~ eta1 + eta2
# Measurement
eta1 =~ y11 + y12 + y13
eta2 =~ y21 + y22 + y23
eta3 =~ y31 + y32 + y33
"
res <- csem(threecommonfactors, model)
The debugger should start and take you right to line 321. You should be able to alter code and you should NOT get the "debug-location-is-approximate-because-the-source-is-not-availabe" warning IF you are on an R version below 4.0.0.
Go to R/00_csem.R and remove the breakpoint in line 321
Close Rstudio and update R to 4.0.0 (using e.g. installr::updateR())
IMPORTANT: also get the newest version of Rtools and follow the steps on the website concerning the part where you need to add rtools to PATH. https://cran.r-project.org/bin/windows/Rtools/
Since you need to reinstall all packages: open cSEM.Rproj again; run install.packages("devtools")
Now repeat steps 3, 4, 5, and 6.
The debugger should stop at line 321 again, however, this time you do get the "debug-location-is-approximate-because-the-source-is-not-availabe" warning.
Why is that? Any ideas/hints/suggestions are welcome!!
Other things I tried:
Deleting the package from win-library and reinstalling
Reinstalling the package from GitHub using devtools::install_github()
Checked package root
Updated all dependency packages
Repeated the procedure on different machines
I develop another package which you can clone from GitHub here: https://github.com/M-E-Rademaker/cSEM.DGP. You can run the same procedure with this package, however, here I dont get the warning when using 4.0.0...which is extremly odd I think.
I investigated this using your package and discovered that what you're seeing here is a bug fairly deep in the RStudio / R interface, caused by some subtle changes R made in R 4.0 around source references.
The problem happens whenever there's a backslash (\) in a function's code. When that happens, R is escaping it, which causes RStudio to think that the copy of the function you're looking at is different than the one in the file, which in turn causes it to show you a copy in a code browser instead of opening the file itself.
Since your csem() function contains a backslash it triggers the issue. I've written this up on our issue tracker here:
https://github.com/rstudio/rstudio/issues/6854

R plot doesn't work on my Windows, but plot.default does

I'm taking a course in R and I've hit a road block on day one.
Typing:
demo(graphics)
...should start a series of graphs being displayed on screen. On my Windows 10 (64 bit) new Dell laptop, I get a persistent error:
The execution is stopped after the red lines, seemingly because the demo(graphics) call is being made with incompatible parameters. In blue I make the same call but use the default function and an empty plot is displayed.
I have tried uninstalling and reinstalling everything, installing through Anaconda and from the CRAN website. The same error persists. I've even tried with mro instead of basic R.
I don't see any similar error messages in my class or on Google. My guess is that I need to install a particular version of R and RStudio make it work but I'm guessing.
Can anybody offer some expertise?
I'm running R version 3.6.1 (2019-07-05) (which is executing code correct, just not the demo call) and RStudio Version
Running the basic Rgui software from the command line with the --vanilla parameter like so:
Rgui --vanilla
Opened Rgui and the
demo(graphics)
...call worked.
This meant that the issue was in the .Rprofile file. This is found in the etc folder of the basic R installation and when I checked, it looked absolutely fine (entirely commented out actually).
I wanted to use RStudio though, so I investigated the RStudio docs and saw that each project is given its own .Rprofile style initialization file when a new project is created.
So I created a new project with RStudio and by creating a new project whatever configuration issue was causing the error was completely fixed.
Credits to G. Grothendieck for the answer in the comments.

Running the R console from Sublime Text 2

I am quite new with Sublime Text 2. I would like to write my script in ST2 and run/send it to the R console. I don't want to use SublimeREPL (most of the forums deal with this) because I want to have my R console open on the side.
I tried to install "R Tools" and the installation of this package seems to work. However, when I open my script.r file and try to run it, nothing happens. I also tried to specify the path to go to R in TOols>Build systems> new build systems... withou being successful.
Can someone give me a trick to solve this?
Thanks a lot!
The easiest way is to install the Enhanced-R package via the Package Manager in Sublime:
Install the Package Control here
Access the Package Manager within Sublime (on Windows: Ctrl+Shift+P)
Type Install Package and then Enhanced-R
Send the highlighted code to R console (on Windows: Ctr+Enter)
You can see the Enhanced-R package description and relevant key bindings here
I just stumbled upon this post because I had a similar problem using Sublime and R. Maybe this helps someone who wants to have a similar setup. Hopefully there will be more and more R users considering Sublime. I figured out the following solution using the Sublime build system in an external xterm terminal (for Linux, but it should be very similar to a setup on Win/Mac systems):
Sublime Text 3 build system: keep console running

Resources