Show plot on R-Studio terminal - plot

do you guys know how to display an R plot in a terminal/console instead of showing it on plot viewers?
I've recently working to integrate R with external tools, and somehow the only function works is to returns all the value shown up in the console result. Thus, I need some sort of workaround to be able to post a plot in the external tools.
Thanks in advance!

No. That is not possible. The reason is that the plot needs a graphic device.
The terminal (and R console) is a text-based device.
What you can do, is use the image-format files as graphic device. Here, you have a multitude of choices, such as PNG, BMP, JPEG. In R, look up ?png.

Related

Save viewer object rendered in RStudio as image

I created an object that is viewed in the RStudio viewer, such as chart in plotly or highcharter or a map in leaflet. I would like to save that object as a png. There are a number of ways to do this. For example, there's this method that uses webshot. It's possible to use orca. There are several methods. The problem with all of these is that the output image is slightly different than the version seen in the viewer. This is a problem for me because I need both the interactive version of the chart as well as a static version and I don't want to have to create two versions and write all the code associated with producing both to make sure each one comes out just as I need.
However, RStudio has a built-in method that can be accessed by clicking "Export -> Save as Image" in the viewer pane. I would like to use this method because it preserves the layout exactly as seen in the viewer.
I tried using R's built-in plot saving method of png, plot and devoff, but that appears to only work for plot objects, not viewer objects.
There has to be a way to reproduce the physical steps of saving as an image in RStudio programmatically, right? Doing it manually would be really time-consuming.
Here's an sample chart in plotly that shows up in the viewer.
plot_ly(z = ~volcano, type = "surface")

How do you delete the current (but not all) plots in the RStudio plotting device?

How do you delete the current (but not all) plots in the RStudio plotting device?
dev.off() will remove all plots, but what if I just want to remove one? I don't want to have to press that red 'x' button because I want to remove one plot without pressing a button.
In R, you would just use dev.new() before each plot, so you dev.off() to only clear the last plot.
In RStudio, you can use x11(), windows() or quartz() (depending on your device) before each plot. Then call dev.off() to clear last plot. You can also use dev.set() to choose specific plots that way.
If your question is specifically asking to delete the last plot within the same RStudio window (rather than making new windows), not sure if it's possible, since RStudio treats that window as one device. An idea would be to look at a way to call the C++ function removePlot() in the RStudio project.
I found in the Github repository for RStudio the C++ code:
display.removePlot(display.activePlotIndex());
You could output the plots and manage the files that way.

Is there a way to create interactive plots (a la D3) in IPython notebooks?

I found those two answers:
Animated graphs in ipython notebook ,
How to grab matplotlib plot as html in ipython notebook?
But they don't address interactivity. I would like to display a plot with 2-3 curves and let the user hover those curves to receive more details or control the parameters that are used to generate those curves from a combo-box
check the following answer at iPython: Manipulate-like command.
as well as the two following directions:
IPython interact/ive as motivated in IPython - The Attributes ofSoftware and How They Affect Our Work - Brian Granger near 32m:12s; and available at IPython's github
Altair provides a powerful and succinct syntax, based on vega-lite; see the gallery
MPLD3: Bringing Matplotlib to the Browser
I don't believe the standard matplotlib plots are capable of the interactivity you want. Chaco can do interactive graphs, but not in an ipython notebook, AFAICT. Your best bet is probably Bokeh. It outputs to Javascript/HTML, can do interactive graphs, and has an example of using it from ipython.
You may want to check out Plotly. They render interactive D3 plots inside IPython Notebooks (examples here. You can export as static images as well, and zoom, hover, pan, and see text on the hover. Interactivity is one of the pitches they're emphasizing. For more, you can also use IPython widgets inside the Notebook, which gives you more control and options. For example:
Like #AFoglia, I'm going to suggest other libraries that might be what you're looking for: Tangle, Mathematica Notebooks, IPython / Bokeh, RactiveJS, or ReactJS.
Another solution might be the Charts library. It enables you to use the excellent Highcharts javascript library to make beautiful and interactive plots out of the box.
Some features:
Vector plots which you can download in .png, .jpg and .svg formats so you will never run into resolution problems.
Interactive charts (zoom, slide, hover over points, ...)
Usable in an IPython notebook
Explore hundreds of data structures at the same time using the asynchronous plotting capabilities.
Disclaimer: I'm the developer of the library
You can run javascript straight from IPython notebooks (%%html and %%javascript), so it should be possible to run D3 code. Check this video. The exact way in which this would be implemented is not set in stone yet, as this is a feature for IPython 2.0. There are many hacks available as of now, as well as projects that try to port some characteristics of D3 to Python (such as bokeh and python-nvd3).
I have not yet tested any of these. My own bias would be to try the example below first, which is static, but uses D3 directly (so it should theoretically work for interactive plots):
gist
notebook
Other answeres here seem to be outdated. IPython 2.0 has support for interactive widgets. Read the docs including some examples, but note that the nbviewer.ipython.org does not show the interactive widgets. Copy & pasting the examples to try.jupyter.org works.

Publishing a rgl interactive 3d plot to the web

I'm looking to see if something is possible, or some ideas if it isn't...
I've used the rgl package to generate an interactive 3d plot that you can rotate to look at the data from different angles.
I would like to be able to somehow publish this (or something similar) to the web to allow other to log on to the site and interact with the graph. Is this sort of thing possible?
I was playing around with the gWidgetsWWW package before which lets you use R as the back end of a website, but I'm not sure if this would be able to embed something as advanced as an RGL plot.
Ideally this would be on the web, but it doesn't have to - i.e. if it was possible to package the actual plot in some format and send it on to people.
Are there any ways to achieve this or can anyone think of a better approach? (Are there any non-R solutions I could look into that would give the same sort of output as rgl?)
Let me know if you need any more info. Thanks!
Looks like vrmlgen might help, but you probably need to have a VRML viewer application in your web browser. VRML was the new hotness about 15 years ago, but vrmlgen is only a few years old at most...
http://www.jstatsoft.org/v36/i08/paper
Some of the answers to this SO Q might help too:
3D visualization in browser

Is there a way to replace the legend or axis without redrawing the whole plot?

Sometimes I will place a legend at a particular location on the plot, let's say topright, and then I see that it covers an important part of the plot. Is there a way I can switch it to topleft or some other place without having to run all the other commands first? Sometimes I have the same problem with the axes, I misspell a word and then I have to issue all the commands again.
I eventually place everything in an R script which means this becomes less of a problem, but sometimes I want to quickly test something in console. Please tell me I have overlooked a basic command that does this.
There is no such thing, but you're on the right track - a script is definitely the way to go. I would also recommend R Studio, a free R IDE, which gives you several displays, one for scripts, one for the console, one for your plots - it's great! It makes working with scripts as easy as interacting with the traditional R console.
As Gabriel told you, a script is the best way. However, the following link could help you:
Using Inkscape to Post-edit Labels in R Graphs

Resources