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.
Related
Is there any quick and easy way to create a simple carousel in an Rmarkdown doc?
What I know so far
I found slickr but run into errors setting options and knitting (the errors could be specific to me / mac - I am not sure at this point).
I believe it would be possible to hard code html/javascript into the RMarkdown doc i.e. the same way a carousel would be done in any other (regular) html document (i.e. using the html code here)- but I wonder if there's a native (R) way?
Example use
In my particular use case, I'm trying to display multiple complicated ggplots which are each sufficiently complex to make them require their own space (i.e. not faceted or grid.arrange as the size of each plot will get too small to read
Notes
Here is the slickr code I tried
library(texPreview)
library(slickR)
objpath <- file.path(getwd(),"slickr_files/figure-html")
if(!dir.exists(objpath)) { dir.create(objpath,recursive = TRUE) }
tex_opts$set(
fileDir = objpath, # path to save output
returnType = 'html', # return images ready for html
imgFormat = 'png' # return png images
)
knitr::kable(mtcars,'latex') %>%
texPreview::tex_preview(stem = 'kable-1')
# ! LaTeX Error: File `standalone.cls' not found.
A side note, if there's a better way of providing many (e.g. > 3) large, detailed plots that doesn't involve faceting, grid.arrange, or (my current preferred option) tabbing, please give a suggestion as a comment
The example works fine for me. Be sure to save your plots in the folder slickr_files/figure-html.
Then run:
```{r}
slickR::slickR(
list.files(objpath,full.names = TRUE,pattern = 'png'),
height = 200,
width = '95%')
```
I've created a scatter plot with ggplot2 and would like to link the data points with corresponding sound files. In a shiny app it should then be possible to click on or hover over a data point to play the sound file. While I manage to get the plot into a shiny app, I am struggling with the inclusion of tags$audio, as described here, into the plot.
library(shiny)
library(plotly)
ui <- fluidPage(
plotlyOutput('scatter')
)
server <- function(input, output) {
output$scatter <- renderPlotly({
ggplotly(p)
})
}
shinyApp(ui = ui, server = server)
The plotly plot displays a tooltip with the info per data point, among others also the URL for the sound file. See the working example here: https://chart-studio.plotly.com/~petergi/1.
The ggplot2 code contains the URL information in the aes 'text'.
p <- ggplot(without_outliers) +
aes(x = T2, y = T1, label = labels, col = next_label,
text = paste0("URL: ", "https://www.yyyy.zz/audio/", tolower(bundle), ".mp3"))
Any hint to how to bring an audio player to the scatter plot's data points in shiny would be highly appreciated.
In the meantime, I found a working solution in using the R package ggiraph, which enhances ggplot2 with tooltip and onclick functionalities, when used in a shiny application. In a nutshell, a geom_point_interative call can have an onClick argument, which in turn then is a call to a Javascript function to play the audio (without opening a new window or a player).
library(ggplot2)
library(ggiraph)
library(shiny)
p <- ggplot(mtcars, aes(wt, mpg))+
geom_point_interactive(onclick="var audio= new Audio(\"https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3\"); audio.play();")
x <- girafe(ggobj = p)
To allow for all different data points in a data frame to be associated with their corresponding audio files, the data frame should be modified to contain a new column with the individual javascript code to play the audio.
An example shiny app can be found here.
Thank you Peter, I managed to get this working with help from your pointers. I know this is a late response but I hope it helps if someone else runs into a similar issue. First, I recommend reviewing the examples shared above.
I managed to get something like this working. However, instead of defining the audio and playing it with the onclick argument, I created a separate javascript file and sourced it through the shiny applet.
I created a separate file called fileNames.js where I defined each variable. Using the example above with only one audio file, this would be: var audio= new Audio("https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3");.
In the R shiny applet, I changed the onclick argument in geom_point_interactive() to the javascript code to play the audio file. In your example, the string would be simply audio.play();. For whatever reason passing the multi-line javascript code to the onclick argument didn't work for me.
Early in the ui call, I included includeScript(path = "fileNames.js") to source the javascript into the shiny app.
Here's another helpful example in case someone stumbles upon this thread as I did: https://www.ardata.fr/en/post/2019/05/24/whats-up-ggiraph/
I am trying to create some R visuals in Power BI using the googleVis and/or plotly libraries, but no matter what I do, I can’t get Power BI to display anything. It always just says “No image was created. The R code didn’t result in creation of any visuals. Make sure your R script results in a plot to the R default device.” The issue occurs with plotly and googleVis libraries, so I think it may have something to do with the fact that they’re both browser-based outputs. Per Microsoft, plotly is supported in Power BI. I was hoping someone could tell me why I can’t get any of these example scripts to work in Power BI.
Example code which works in R, but not pbi.
plotly
library(plotly)
p <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
p
googleVis
df=data.frame(country=c("US", "GB", "BR"),
val1=c(10,13,14),
val2=c(23,12,32))
Line <- gvisLineChart(df)
plot(Line)
Now, we can create RHTML custom visual in power BI
1) we can use Ploty - to use it you have load you "midwest" data in power BI table
2) then drag it to R script so it will available to R Script .
3) then run R script , it will work
You can render charts created by plotly as PNG:
p <- plot_ly(x = dataset$period, y = dataset$mean, name = "spline", line = list(shape = "spline"))
plotly_IMAGE(p, format = "png", out_file = "out.png")
But the problem with this is that, though rendered by plotly, the visualizations will not be interactive since it's just a PNG image.
If you want to create interactive visualizations using plotly. The only way you can do so far is to create a custom Power BI visualization and import it to your report. See this post for a good introduction.
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)
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)