I am trying to adapt for my own application the last example in this documentation of creating valueBoxes with "showcase"d sparklines made interactive via plotly. The example does not go as far as rendering within a shiny app and the bslib
package does not include render/output functions.
I have gotten something sort of working via the renderUI
/uiOutput
functions but the result does not respect proportioning and positioning between the value and the showcased sparkline within the bs4Dash
(or any other framework). Running the code interactively in RStudio shows the desired result in the Viewer pane. I am looking for help to match the rendered output in my shiny app to the article linked above
MRE below
## app.R ##
library(shiny)
library(plotly)
library(dplyr)
library(bs4Dash)
library(bslib)
ui <- bs4DashPage(
dashboardHeader(title = "Test Dash"),
bs4DashSidebar(
sidebarMenu(id = "tab",
menuItem("Test 1", tabName = "t1", icon = icon("dashboard")),
menuItem("Test 2", tabName = "t2", icon = icon("triangle-exclamation"))
)
),
bs4DashBody(
tabItems(
tabItem(tabName = "t1",
fluidRow(
box(width = 3,
uiOutput("papq_vbox_quote")
)
)
),
tabItem(tabName = "t2"
)
)
)
)
server <- function(input, output) {
dat <- tibble(Date = seq(Sys.Date()-59, Sys.Date(), by = 1),
measure = rnorm(length(Date), 20 + (Date - min(Date)), 5))
output$papq_vbox_quote <- renderUI({
sparkline <- plot_ly(dat) %>%
add_lines(
x = ~Date, y = ~measure,
color = I("white"), span = I(1),
fill = 'tozeroy', alpha = 0.2
) %>%
layout(
xaxis = list(visible = F, showgrid = F, title = ""),
yaxis = list(visible = F, showgrid = F, title = ""),
hovermode = "x",
margin = list(t = 0, r = 0, l = 0, b = 0),
font = list(color = "white"),
paper_bgcolor = "transparent",
plot_bgcolor = "transparent"
) %>%
config(displayModeBar = F) %>%
htmlwidgets::onRender(
"function(el) {
var ro = new ResizeObserver(function() {
var visible = el.offsetHeight > 200;
Plotly.relayout(el, {'xaxis.visible': visible});
});
ro.observe(el);
}"
)
value_box("Series Data",
value = formatC(mean(dat$measure), format = "d", big.mark = ","),
showcase = sparkline,
showcase_layout = showcase_left_center(),
full_screen = TRUE,
# height = "100px",
# width = .2,
# max_height = "100px",
theme_color = "success"
) %>%
return()
})
}
options(shiny.host = '0.0.0.0')
options(shiny.port = 8080)
shinyApp(ui, server)