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

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!

Related

How to show R graph from visual studio code

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

Python bindings not loaded issue in Rstudio

I want to use Tensorflow package for R in Windows.
Done with Python 3.5.x installation from python.org and have installed the Tensorflow R package from devtools::install_github("rstudio/tensorflow")as per the official source https://rstudio.github.io/tensorflow/
I know I am not setting the environment variable in the right way in the windows and/or in the sys.setenv function of R.
Above link says it should be set to Sys.setenv(TENSORFLOW_PYTHON="/usr/local/bin/python").
See below the location of my Python35 folder which includes all the python stuff including the tensorflow library downloaded from the python side:
Python35 folder location:C:\Users\rgupta6\AppData\Local\Programs\Python\Python35
tensorflow folder location: C:\Users\rgupta6\AppData\Local\Programs\Python\Python35\Lib\site-packages\tensorflow
Code I used:
Sys.setenv(TENSORFLOW_PYTHON="C:\\Users\\rgupta6\\AppData\\Local\\Programs\\Python\\Python35")
Sys.setenv(TENSORFLOW_PYTHON="C:\\Users\\rgupta6\\AppData\\Local\\Programs\\Python\\Python35\\Lib\\site-packages\\tensorflow")
I use library(tensorflow) and get no error.
Then I use sess = tf$Session() and get an error:
Error in initialize_python(required_module) : Installation of Python not found, Python bindings not loaded*.
What should I do to make it work?
If you are getting errors such as:
Error in initialize_python(required_module) : Installation of Python not found, Python bindings not loaded
Error: Installation of TensorFlow not found
Python environments searched for 'tensorflow' package:
C:\Users\rgupta6\AppData\Local\Programs\Python\Python35\python.exe
Some error related to file does not exist
While trying to make Tensorflow package to work in Rstudio, the problem is with your environment variable in Windows.
What you need to do is:
From the desktop, right click the Computer icon.
Choose Properties from the context menu.
Click the Advanced system settings link.
Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit.
A new pop up will open. Variable name will remain Path. We will change the Variable value to the location of the folder where your tensorflow folder is located. Find it.
For e.g. I changed its value to:
C:\Users\rgupta6\AppData\Local\Programs\Python\Python35\Lib\site-packages\tensorflow
Close all remaining windows. Open Rstudio, and run your "Hello World" program to see if your tensorflow works fine in R:
library(tensorflow)
sess = tf$Session()
hello <- tf$constant('Hello, TensorFlow!')
sess$run(hello)

R: instructions for unbundling and using a packrat snapshot

I used packrat (v 0.4.8.-1) to to create a snapshot and bundle of the R package dependencies that go along with the corresponding R code. I want to provide the R code and packrat bundle to others to make the work I am doing (including the R environment) fully reproducible.
I tested unbundling using a different computer from the one I used to write R code and create the bundle. I opened an R code file in R studio, and called library(packrat) to load packrat (also v 0.4.8-1). I then called packrat::unbundle(bundle = "directory", where = "directory"), which unbundled successfully. But subsequently calling packrat::restore() gave me the error "This project has not yet been packified. Run 'packrat::init()' to init packrat". It seems like init() should not be necessary because I am not trying to create a new snapshot, but rather utilize the one in the bundle. The packrat page (https://rstudio.github.io/packrat/) and CRAN provide very little documentation about unbundling to help troubleshoot this, or that I could point users of my code to for instructions (who likely will be familiar with R, but may not have used packrat).
So, can someone please provide clear step-by-step instructions for how users of a bundled snapshot should unbundle, and then use that saved snapshot to run a R code file?
After some experimenting, I found an approach that seems to have worked so far.
I have provided users with three files:
-tar.gz (packrat bundle file)
-unbundle.R (R code file that includes a library statement to load
the packrat library, and the unbundle command for the tar.gz file)
-unbundle_readme.txt
The readme file includes instructions similar to those below, and so far users have been able to run R code using the package dependencies. The readme file tells users about requirements (R, R studio, packrat, R package development prerequisites (Rtools for Windows, XCode for Mac)), and includes output of sessionInfo() to document R package versions that the R code should use after instructions are followed. In the example below 'code_folder' refers to a folder within the tar.gz file that contains R. code and associated input files.
Example unbundle instructions:
Step 1
Save, but do not expand/unzip, the tar file to a directory.
Problems with accessing the saved package dependencies
are more likely when a program other than R or R studio
is used to unbundle the tar file.
If the tar file has already been expanded, re-save the
tar file to a new directory, which should not be a the same
directory as the expanded tar file, or a subdirectory of
the expanded tar file.
Step 2
Save unbundle.R in the same directory as the tar file
Step 3
Open unbundle.R using R studio
Step 4
Execute unbundle.R
(This will create a subfolder ‘code_folder’.
Please note that this step may take 5-15 minutes to run.)
Step 5
Close R studio
Step 6
Navigate to the subfolder ‘cold_folder’
Step 7
Open a R script using R studio
(The package library should correspond to that listed below.
This will indicate R studio is accessing the saved package
dependencies.)
Step 8
Execute the R code, which will utilize the project package library.
After the package library has been loaded using the above
steps, it is not necessary to re-load the package library for each
script. R studio will continue to access the package dependencies
for each script you open within the R studio session. If you
subsequently close R-studio, and then open scripts from within
the unbundle directory, R studio should still access the
dependencies without requiring re-loading of the saved package
snapshot.

R package development - old version of function used in project

I am developing a package locally with devtools in RStudio. After modifying a function, when I try to call it from a project, R keeps using the old version of the function.
My workflow is to:
Modify the function and save
Call Build & Reload
Test the function with some example code in the package development
project (I often run another Build & Reload after that)
Go to the project I want to use the function in
call library(my_library)
But the modification I just did would not be effective. What is wrong with this workflow?
?devtools::build:
Building converts a package source directory into a single bundled file. If binary = FALSE this creates a tar.gz package that can be installed on any platform, provided they have a full development environment (although packages without source code can typically be install out of the box). If binary = TRUE, the package will have a platform specific extension (e.g. .zip for windows), and will only be installable on the current platform, but no development environment is needed.
My reading of this is that you still need to devtools::install() your package. Building just creates the binary, it doesn't install the new version.

Importing mgcv fails because Rlapack.dll cannot be found

I want to link to the R statistical package in IronPython by using the R.NET library. It's been working fine, but now I need to use R's mgcv library.
Importing mgcv fails (import is done with the command rdn.r.EagerEvaluate("library(mgcv)"), where rdn is an IronPython object that wraps the R.NET library). When the import fails, Windows opens a dialog box that says: "The program can't start because Rlapack.dll is missing from your computer. Try reinstalling the program to fix this problem."
Of course, R never would have worked in the first place if Rlapack.dll was missing, so what is going on?
I inspected the dependencies of the mgcv package; they include the base package Matrix. As it turns out, Matrix has been compiled to a dll (found at ${R_HOME}/library/Matrix/libs/i386/Matrix.dll) by the R development team. That dll needs to link to Rlapack.dll, which for some reason it cannot find when R is called from R.NET in IronPython.
The solution was to drop a copy of Rlapack.dll (which you can find in ${R_HOME}/bin/i386/) into the same directory as Matrix.dll. Now every day is Sunday.
I had the same problem with compositions.dll.
So instead of copying the Rlapack.dll to the directory where compositions.dll exists, I added the bin directory to the PATH.
string rhome = System.Environment.GetEnvironmentVariable("R_HOME");
if (string.IsNullOrEmpty(rhome))
rhome = #"C:\Program Files\R\R-2.14.0";
System.Environment.SetEnvironmentVariable("R_HOME", rhome);
System.Environment.SetEnvironmentVariable("PATH", System.Environment.GetEnvironmentVariable("PATH") + ";" + rhome + #"\bin\i386");

Resources