0

I would like to include a 3D dynamic (i.e. one can change its perspective just by moving the plot) histogram widget in a R Shiny application.

Unfortunately I didn't find any until now.

So far the results of my searches: with threejs (e.g. here on CRAN and there on GitHub) one can use many different representations (scatterplots, surfaces, etc.) but no 3D histogram. plot3D and plot3Drgl don't have any R Shiny counterpart.

Unless something already exists my intention is to create an HTMLWidget from one of the sub-libraries of vis.js, namely graph3d.

What are your views on this issue?

Best regards,

Olivier

Olivier7121
  • 151
  • 1
  • 11

3 Answers3

1

It's possible with plot3Drgl. Here is an example.

library(plot3Drgl)
library(shiny)

options(rgl.useNULL = TRUE)

ui <- fluidPage(
  rglwidgetOutput("myWebGL")
)

server <- function(input, output) {
  save <- options(rgl.inShiny = TRUE)
  on.exit(options(save))
  output$myWebGL <- renderRglwidget({
    try(rgl.close())
    V <- volcano[seq(1, nrow(volcano), by = 5), 
                 seq(1, ncol(volcano), by = 5)]  # lower resolution
    hist3Drgl(z = V, col = "grey", border = "black", lighting = TRUE)
    rglwidget()
  })  
}

shinyApp(ui, server)
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • Many thanks, Stéphane. It works great. Nevertheless I think an alternative with vis.js-graph3d would be useful (e.g. for tooltips, etc.). – Olivier7121 Jun 21 '19 at 08:17
0

Many thanks, DSGym. I didn't know this library.

In my initial message (now amended) I actually forgot to mention the dynamic feature, i.e. the ability to change the perspective of the plot just by moving it with the mouse, like with vis.js-graph3d.

It seems plots from highcharter cannot do that, or am I mistaken?

[EDIT]: I just checked with Shiny: it is static.

Olivier7121
  • 151
  • 1
  • 11
0

My package graph3d is on CRAN now.

library(graph3d)

dat <- data.frame(x = c(1,1,2,2), y = c(1,2,1,2), z = c(1,2,3,4))
graph3d(dat, type = "bar", zMin = 0, tooltip = TRUE)

You can customize the tooltips:

graph3d(dat, type = "bar", zMin = 0,
        tooltip = JS(c("function(xyz){",
                       "  var x = 'X: ' + xyz.x.toFixed(2);",
                       "  var y = 'Y: ' + xyz.y.toFixed(2);",
                       "  var z = 'Z: ' + xyz.z.toFixed(2);",
                       "  return  x + '<br/>' + y + '<br/>' + z;",
                       "}"))
)

enter image description here

I realize I have to add an option to control the size of the axes labels...

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • Wow, what a nice surprise, Stéphane! Thanks for that! I can have a look and test your package with the data I initially thought of only at the end of next week. I will then let you know my feedback. – Olivier7121 Aug 13 '20 at 20:23
  • Many thanks again, @Stéphane Laurent, for your work, and apologies for getting back to you much later than announced. I finally tested your package and here is my very first feedback: – Olivier7121 Sep 16 '20 at 13:10
  • 1. The use of qualitative data on the axes (say, x and y - in my case countries and lines of business) doesn't seem to be easy/supported yet. 2. I didn't manage to assign colors to a qualitative factor (say, x - countries) with the `dataColor.fill` option. 3. I guess you didn't implement warning/error messages? – Olivier7121 Sep 16 '20 at 13:16
  • Regarding point #1 above, I guess passing a R vector to JavaScript and using the function `{x|y|z}ValueLabel` should work. But I agree an option to set the size of the axes labels would be nice^^ – Olivier7121 Sep 18 '20 at 09:30