R View(matrix) or View(dataframe) in RStudio - r

When using the command View(matrix) or View(dataframe) or by doubleclicking in Rstudio on the Data in the global environment, a screen shows up in which you can see your data. However, when scrolling down on that screen the column names dissappear. Is there a "freeze panes" option as in excel?
Thanks

I find gvisTable from package googleVis quite convenient when viewing data in RStudio.
"The gvisTable function reads a data.frame and creates text output referring to the Google Visualisation API, which can be included into a web page, or as a stand-alone page. The actual chart is rendered by the web browser."
gvisTable opens a viewer pane in RStudio (where local web content can be viewed). The headers are always visible and there are several options that allow you to customize the view. You can sort by variables by clicking on their header.
A simple example:
library(googleVis)
gt <- gvisTable(iris)
plot(gt)
gt <- gvisTable(iris, options = list(page = 'enable', height = 200))
plot(gt)

Related

R View gt table in window from script message box three buttons

Ultimate goal: Display gt table in pop-out window with 3 buttons to control script action
Minimum goal: Display a gt table in pop-out viewer window
Full detail:
I am creating a table using the gt package in R. I want to display the table with three options for how to proceed: OK (script continues), Edit (allow the user to make minor modifications to a specific column), Cancel (something has gone wrong and the process needs to be terminated). My plan is to allow a quick QC of the data before it gets saved. My issue is that I have no idea how to display a gt table in a viewer window or with tk_messageBox and that's about the only package I know of that might do this.
Any ideas?
Here's an example gt table:
table <- gt(mtcars) %>%
tab_header(
title = md("**2014 - 2019 Salary and Playoff Appearances**⚽"),
subtitle = "I am a boss")
you could achieve such an effect with shiny/miniUI specifically runGadget()
see this answer I gave yesterday which is an example where the user is shown two checkboxes to interact with that are returned as data into the calling environment
Is it possible to create a check box to record a user's input without resorting to Shiny in R?

How to save a plot as image on disk from Viewer in RStudio?

Summary: my ultimate goal is to use rCharts, and specifically Highcharts, as part of a ReporteRs PowerPoint report automation workflow. One of the charts I would like to use is rendered as html in the Viewer pane in Rstudio, and addPlot(function() print(myChart)) does not add it to the PowerPoint. As a workaround, I decided to try to save myChart to disk, from where I could just add it to the PowerPoint that way.
So my question is really, How do I get my html image into my ReporteRs workflow? Either getting it saved to a disk, or getting it to be readable by ReporteRs would solve my problem.
This question is really the same as this one, but I'm using rCharts, specifically the example found here:
#if the packages are not already installed
install.packages('devtools')
require(devtools)
install_github('rCharts', 'ramnathv')
#code creates a radar chart using Highcharts
library(rCharts)
#create dummy dataframe with number ranging from 0 to 1
df<-data.frame(id=c("a","b","c","d","e"),val1=runif(5,0,1),val2=runif(5,0,1))
#muliply number by 100 to get percentage
df[,-1]<-df[,-1]*100
myChart <- Highcharts$new()
myChart$chart(polar = TRUE, type = "line",height=500)
myChart$xAxis(categories=df$id, tickmarkPlacement= 'on', lineWidth= 0)
myChart$yAxis(gridLineInterpolation= 'circle', lineWidth= 0, min= 0,max=100,endOnTick=T,tickInterval=10)
myChart$series(data = df[,"val1"],name = "Series 1", pointPlacement="on")
myChart$series(data = df[,"val2"],name = "Series 2", pointPlacement="on")
myChart
So if I try
> png(filename="~/Documents/name.png")
> plot(myChart)
Error in as.double(y) :
cannot coerce type 'S4' to vector of type 'double'
> dev.off()
I get that error.
I've looked into Highcharts documentation, as well as many other potential solutions that seem to rely on Javascript and phantomjs. If your answer relies on phantomjs, please assume I have no idea how to use it. webshot is another package I found which is even so kind as to include an install_phantomjs() function, but from what I could find, it requires you to turn your output into a Shiny object first.
My question is really a duplicate of this one, which is not a duplicate of this one because that is how to embed the html output in Rmarkdown, not save it as a file on the hard drive.
I also found this unanswered question which is also basically the same.
edit: as noted by #hrbrmstr and scores of others, radar charts are not always the best visualization tools. I find myself required to make one for this report.
The answer turned out to be in the webshot package. #hrbrmstr provided the following code, which would be run at the end of the code I posted in the question:
# If necessary
install.packages("webshot")
library(webshot)
install_phantomjs()
# Main code
myChart$save("/tmp/rcharts.html")
webshot::webshot("/tmp/rcharts.html", file="/tmp/out.png", delay=2)
This saves the plot to the folder as an html, and then takes a picture of it, which is saved as a png.
I can then run the ReporteRs workflow by using addImage(mydoc, "/tmp/out.png").

How to force R Studio to use Viewer Pane for GoogleVis

Trying to use gvisTimeline from googleVis, but I'd like the plots to be shown in the Viewer Pane.
From what I can gauge from here (http://www.r-bloggers.com/display-googlevis-charts-within-rstudio/) it looks like the Viewer pane should be the default, but mine is showing up in the browser.
I did find this support article (https://support.rstudio.com/hc/en-us/articles/202133558-Extending-RStudio-with-the-Viewer-Pane) but am unclear how to apply it to my specific need of using gvisTimeline.
ex:
library(googleVis)
dat <- data.frame(Stn=device.positions$StationName,Idnt=device.positions$Ident,start=as.POSIXct(device.positions$Start),end=as.POSIXct(device.positions$End))
timeline <- gvisTimeline(data=dat, rowlabel="Stn", barlabel="Idnt", start="start", end="end")
plot(timeline)
where device.positions is a basic data table
When you plot the googleViz object you need to pass this as an option
plot(object, browser=rstudio::viewer)

Export the graph generated by rCharts in shiny application

I want to be able to export the charts generated in my shiny application using rCharts to Image and PDF formats. Is there any provision in the rCharts library for that?
I have earlier used ggvis, It gives an option for resizing the chart in the browser and also an option to download the chart in HTML or PNG format. Anything similar to that?
Edit 1:
I'm using nvd3 and polyCharts as my charting libraries currently.
To download as image or pdf you can use a$exporting(enabled = T), assuming your chart is called a.
library(rCharts)
a <- hPlot(Pulse ~ Height, data = MASS::survey, type = "scatter", group = "Exer")
a$exporting(enabled = T)
a
To follow up on my comment above, I was a bit too quick to respond, as the htmlwidget::saveWidget() function is meant for widgets developed under the htmlwidgets.org framework. However, rCharts has a similar function:
library(rCharts)
a <- nPlot(Pulse ~ Height, data = MASS::survey, type = "scatterChart", group = "Exer")
a$save("demo.html", standalone=TRUE)
Where 'demo.html' is standalone html file. Creating a png is as simple as taking a screenshot. Note that you can also call this function in a shiny app.

Is it possible to download googleVis plots from R?

Follow up from :
https://groups.google.com/forum/#!topic/shiny-discuss/u7gwXc8_vyY
I have the exact same R Shiny structured program as the user in the post, except I am using the googleVis Shiny package for my plots. For example, here is a plot of a gVis table:
output$gvisTable <- renderGvis( {
if (is.null(dataset))
return(NULL)
gvisTable(dataset)
})
EDIT:
My code to save ggplots:
server.R
name <- paste0(input$filename, ".png")
print(p)
if(input$savePlot) {
ggsave(name, p, type="cairo-png")
}
ui.R (in sidePanel)
wellPanel(
textInput('filename', "Filename"),
checkboxInput('savePlot', "Check to save")
)
This is what I am using to try to save gVis plots:
name <- paste0(input$filename, ".png")
if(input$savePlot) {
png(name, *INPUT GVIS PLOT HERE*, type="cairo-png")
dev.off()
}
This does not work: I get the error: 'non-numeric argument to binary operator'
I can't find a way to allow the user to download a gVis plot. I can't use the method in the linked post because you cannot 'print' a gVis plot.
Also, the files are locally saved to my R working directory, but I plan to upload this app to the web. Where would the files be saved for the user? Ideas?
I see no png method documented for gvis objects.
There are two ways to "print" documented in the help page linked from the googleVis main Index page for print.gvis. The default method (when the tag is NULL or "html") is to send a Java script page to your browser. The other (when you set tag="chart" is to construct an html file with the name you give it. I suppose you could arrange something with system commands sent to a running instance of a particular browser, but for that you should use different SO tags so that you attract the interest of people using the same software as you. (I'm using a Mac with Firefox and having no difficulties seeing the "printed" output.) If you plan to "upload it to the web", then you need to have a server. Is my impression this is a bit new for you correct?
print(GTM, tag="chart", file="test.html")
#created in my working directory

Resources