Hi all,
I am working on a small visualization module with rgl package and rglwidgetOutput().
I'm trying to make rgl output shown in a full-page mode, which could work with
rglwidgetOutput("id", width = "100%", height = WINDOW_HEIGHT)
or
rglwidgetOutput("id", width = WINDOW_WIDTH, height = WINDOW_HEIGHT)
I was trying with shinyjs to detect browser window height and input it as rglwidgetOutput(). This, however, did not work due to the fact that ui.R is load before server.r where the WINDOW_HEIGHT variable is created.
Has anyone tried it or know how to overcome it?
Here is the code I have:
library(shiny)
library(shinyjs)
library(rgl)
ui <- fluidPage(
useShinyjs(),
navbarPage(
title = "test",
collapsible = TRUE,
inverse = TRUE,
position = "fixed-top",
id = "innavbar",
selected = "Home",
tags$head(tags$script('
var dimension = [0, 0];
$(document).on("shiny:connected", function(e) {
dimension[0] = window.innerWidth;
dimension[1] = window.innerHeight;
Shiny.onInputChange("dimension", dimension);
});
$(window).resize(function(e) {
dimension[0] = window.innerWidth;
dimension[1] = window.innerHeight;
Shiny.onInputChange("dimension", dimension);
});
')),
tabPanel(
title = "Get Started",
value = "Home",
column(
2,
offset = 0,
tags$div(
style = "margin: 10px; padding-top:51px",
sliderInput("MT_NO", "Number of MT to visualize", value = 1, min = 1, max = 100)
)
),
column(
10,
offset = 0, style = "padding:0; padding-top:51px",
rglwidgetOutput("wdg", width = "100%", height = WINDOW_HEIGHT) %>% withSpinner()
)
)
)
)
server <- function(input, output) {
observe({
req(input$dimension)
assign("WINDOW_HEIGHT",
paste(as.numeric(input$dimension[2] - 51), "px", sep = ""),
envir = .GlobalEnv
)
})
observeEvent(input$`MT_NO`, {
assign("MT_NO_IMPUT",
as.numeric(input[["MT_NO"]]),
envir = .GlobalEnv
)
output$wdg <- renderRglwidget({
open3d()
rgl.bg(color = "black", fogtype = "none")
rgl.light(
diffuse = "gray75",
specular = "gray75", viewpoint.rel = FALSE
)
for (i in 1:MT_NO_IMPUT) {
MT <- tibble(
x = rnorm(10, 0, 5),
y = rnorm(10, 0, 3),
z = rnorm(10, 0, 8)
)
lines3d(MT, col = "red")
}
scene <- scene3d()
rgl.close()
rglwidget(scene, reuse = TRUE)
})
})
}