问题描述
亲爱的社区成员,
我使用 R
套餐 sunburstR
为了在 Shiny
中创建一个sunburst图。下面的代码工作完美,我能够创建情节,但是,我想完全删除图例。出于这个原因,我知道使用HTML5我将能够更多地使用绘图的参数。
I am using the R
package sunburstR
in order to create a sunburst plot into Shiny
. The code below works perfect and I am able to create the plot, however, I would like to remove the legend completely. For this reason I know that using HTML5 I will be able to play more with the parameters of the plot.
rm(list = ls())
library(shiny)
library(shinydashboard)
library(sunburstR)
library(data.table)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("Sunburst Plot", tabName = "sunbrstPlot")
)
),
dashboardBody( tabBox(id = "sunbrstPlot", width = "100%", height = "1000px",
sunburstOutput("sunburstPlot", height = "750", width = "100%")
)
)
)
server <- function(input, output) {
# Create Sunburst plot
output$sunburstPlot <- renderSunburst({
tempDat <- data.table(A=sample(rep(c("a","b","c","d","e"), 100)), B = sample(rep(c("a","b","c","d","e"), 100)), C = sample(rep(c("a","b","c","d","e"), 100)))
tempDat[,c("V1","V2"):= list(paste0(A,"-",B, "-", C),1)]
sunburst(tempDat[,.(V1,V2)])
})
}
shinyApp(ui, server)
为此图表编写的HTML5代码为:
The HTML5 code that is written for this chart is:
print(sunburstOutput("sunburstPlot", height = "750", width = "100%"))
<div class="sunburst html-widget html-widget-output" id="sunburstPlot" style="width:100%; height:750px; position:relative;">
<div>
<div class="sunburst-main">
<div class="sunburst-sequence"></div>
<div class="sunburst-chart">
<div class="sunburst-explanation" style="visibility:hidden;"></div>
</div>
</div>
<div class="sunburst-sidebar">
<input type="checkbox" class="sunburst-togglelegend">Legend</input>
<div class="sunburst-legend" style="visibility:hidden;"></div>
</div>
</div>
</div>
我在想如果可以修改HTML代码并将其合并到 dashboardBody
我将能够重现图表并可能在将来摆脱传奇:
I was thinking that if could modify the HTML code and incorporate it into the dashboardBody
I would be able to reproduce the chart and maybe get rid off the legend in the future:
rm(list = ls())
library(shiny)
library(shinydashboard)
library(sunburstR)
library(data.table)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("Sunburst Plot", tabName = "sunbrstPlot")
)
),
dashboardBody( tabBox(id = "sunbrstPlot", width = "100%", height = "1000px",
#sunburstOutput("sunburstPlot", height = "750", width = "100%")
tags$div(class="sunburst html-widget html-widget-output", id="sunburstPlot", style="width:100%; height:750px; position:relative;",
tags$div(
tags$div(class = "sunburst-main",
tags$div(class="sunburst-sequence"),
tags$div(class="sunburst-chart",
tags$div(class="sunburst-explanation", style="visibility:hidden;")
)
), tags$div(class="sunburst-sidebar",
tags$input(type="checkbox", class="sunburst-togglelegend", "Legend"),
tags$div(class="sunburst-legend", style="visibility:hidden;")
)
)
)
)
)
)
server <- function(input, output) {
# Create Sunburst plot
output$sunburstPlot <- renderSunburst({
tempDat <- data.table(A=sample(rep(c("a","b","c","d","e"), 100)), B = sample(rep(c("a","b","c","d","e"), 100)), C = sample(rep(c("a","b","c","d","e"), 100)))
tempDat[,c("V1","V2"):= list(paste0(A,"-",B, "-", C),1)]
sunburst(tempDat[,.(V1,V2)])
})
}
shinyApp(ui, server)
不幸的是,按照这种方法,我无法重现图表。你可以提供这方面的帮助吗?
Unfortunately following this approach I am not able to reproduce the chart. Could you provide assistance with this?
感谢您抽出时间回答我的问题。
Thank you for your time on my question.
干杯,
Kostas
Cheers,Kostas
推荐答案
@warmoverflow答案应该可行,但下面的代码将显示一些可能更强大的方法来实现您的目标。我将在代码中内联注释以尝试描述方法。
@warmoverflow answer should work ok, but the code below will show some possibly more robust methods for achieving your objective. I will comment inline in the code to try to describe the approaches.
library(sunburstR)
sequences <- read.csv(
system.file("examples/visit-sequences.csv",package="sunburstR")
,header = FALSE
,stringsAsFactors = FALSE
)
sunburst(sequences)
选项1 - htmlwidgets :: onRender
我们可以使用 htmlwidgets :: onRender
在绘制旭日形象后删除图例。
option 1 - htmlwidgets::onRender
We can use htmlwidgets::onRender
to remove the legend after the sunburst is drawn.
htmlwidgets::onRender(
sunburst(sequences),
'
function(el,x){
d3.select(el).select(".sunburst-sidebar").remove()
}
'
)
选项2 - 替换sunburst_html函数
htmlwidgets
允许使用自定义html函数来定义 htmlwidget
的容器。我们可以通过 sunburstR ::: sunburst_html
查看sunburstR的功能。在这种方法中,我们将使用没有图例的自定义html函数替换 sunburstR ::: sunburst_html
。
option 2 - replace the sunburst_html function
htmlwidgets
allows the use of a custom html function to define the container for the htmlwidget
. We can see the function for sunburstR with sunburstR:::sunburst_html
. In this approach, we will replace sunburstR:::sunburst_html
with a custom html function without the legend.
library(htmltools)
sunburst_html <- function(id, style, class, ...){
tagList(
tags$div(
id = id, class = class, style = style, style="position:relative;"
,tags$div(
tags$div(class = "sunburst-main"
, tags$div( class = "sunburst-sequence" )
, tags$div( class = "sunburst-chart"
,tags$div( class = "sunburst-explanation", style = "visibility:hidden;")
)
)
# comment this out so no legend
#,tags$div(class = "sunburst-sidebar"
# , tags$input( type = "checkbox", class = "sunburst-togglelegend", "Legend" )
# , tags$div( class = "sunburst-legend", style = "visibility:hidden;" )
)
)
)
}
# replace the package sunburst_html with our custom function
# defined above
assignInNamespace("sunburst_html", sunburst_html, "sunburstR")
sunburst(sequences)
这篇关于使用HTML而不是sunburstOutput在Shiny中创建旭日形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!