Shiny中sunburstR渲染图:移除图例及图例复选框方法咨询
Hey Geoff, let's work through your two sunburstR problems in Shiny—removing the legend/checkboxes entirely, and fixing the lingering "Legend" label/checkbox when you thought you'd hidden them.
1. How to Remove the Legend and Checkboxes Completely
The easiest approaches depend on your sunburstR version, but here are the most reliable methods:
Method 1: Use the Built-in legend Parameter (Simplest)
If you're using a recent version of sunburstR, the sunburst() function has a legend parameter that hides both the legend and associated checkboxes when set to FALSE:
library(sunburstR) # In your Shiny server or UI sunburst(your_data, legend = FALSE)
This should be your first try—it's clean and requires no extra code.
Method 2: Custom JavaScript with onRender
If the legend parameter doesn't work (e.g., older package version), use htmlwidgets::onRender() to target and hide the DOM elements directly:
library(sunburstR) library(htmlwidgets) sunburst(your_data) %>% onRender(" function(el) { // Hide the legend container const legendDiv = el.querySelector('.sunburst-legend'); if (legendDiv) legendDiv.style.display = 'none'; // Hide the 'Legend' checkbox and its label const legendCheckbox = el.querySelector('input[type=\"checkbox\"][id*=\"legend\"]'); if (legendCheckbox) { legendCheckbox.parentElement.style.display = 'none'; } } ")
Method 3: Global CSS in Shiny UI
For a quick, brute-force fix, add CSS to your Shiny UI to hide all legend-related elements:
library(shiny) library(sunburstR) ui <- fluidPage( tags$head( tags$style(HTML(" /* Hide the legend itself */ .sunburst-legend { display: none !important; } /* Hide the Legend checkbox and its label */ input[id*=\"legend\"], label[for*=\"legend\"] { display: none !important; } ")) ), sunburstOutput("sunburst_plot") ) server <- function(input, output) { output$sunburst_plot <- renderSunburst({ sunburst(your_data) }) } shinyApp(ui, server)
2. Fixing Lingering "Legend" Label/Checkbox
If you've already hidden the legend but still see the label and checkbox, it means you only targeted the legend graphic—not the control container. The methods above (especially Method 2 or 3) will fix this by targeting the checkbox and label directly.
Avoid Modifying sunburstR Source Code
While you could dig into the sunburstR package's source files (like inst/htmlwidgets/sunburst.js or the R functions that generate the UI), this is not recommended—any changes will be overwritten when you update the package. The CSS/JS methods above are far more maintainable.
内容的提问来源于stack exchange,提问作者Geoff

