wordcloud2 闪亮输出创建额外的小部件
wordcloud2 shiny output creates extra widget
使用 wordcloud2 cran 页面 (https://cran.r-project.org/web/packages/wordcloud2/vignettes/wordcloud.html) 上的 rshiny 示例,我在 wordcloud 下方得到了一个额外的小框。每当我使用 wordcloud2 包的 rshiny 功能时,就会发生这种情况:
生成这个的代码只是:
library(wordcloud2)
# Global variables can go here
n <- 1
# Define the UI
ui <- bootstrapPage(
numericInput('size', 'Size of wordcloud', n),
wordcloud2Output('wordcloud2')
)
# Define the server code
server <- function(input, output) {
output$wordcloud2 <- renderWordcloud2({
# wordcloud2(demoFreqC, size=input$size)
wordcloud2(demoFreq, size=input$size)
})
}
shinyApp(ui = ui, server = server)
移除框的一种方法是使用 CSS 样式将元素设置为不显示。这可以通过将此代码添加到 UI:
的主体来完成
tags$head(
tags$style(HTML('div#wcLabel {display: none;}'))
)
请注意,当您将鼠标悬停在某个词上时,这也会终止显示术语频率的滚动功能。就我而言,这是可取的。
我使用这个 wordcloud2a()
函数而不是常规的 wordcloud2()
解决了问题
wordcloud2a <- function (data, size = 1, minSize = 0, gridSize = 0, fontFamily = "Segoe UI",
fontWeight = "bold", color = "random-dark", backgroundColor = "white",
minRotation = -pi/4, maxRotation = pi/4, shuffle = TRUE,
rotateRatio = 0.4, shape = "circle", ellipticity = 0.65,
widgetsize = NULL, figPath = NULL, hoverFunction = NULL)
{
if ("table" %in% class(data)) {
dataOut = data.frame(name = names(data), freq = as.vector(data))
}
else {
data = as.data.frame(data)
dataOut = data[, 1:2]
names(dataOut) = c("name", "freq")
}
if (!is.null(figPath)) {
if (!file.exists(figPath)) {
stop("cannot find fig in the figPath")
}
spPath = strsplit(figPath, "\.")[[1]]
len = length(spPath)
figClass = spPath[len]
if (!figClass %in% c("jpeg", "jpg", "png", "bmp", "gif")) {
stop("file should be a jpeg, jpg, png, bmp or gif file!")
}
base64 = base64enc::base64encode(figPath)
base64 = paste0("data:image/", figClass, ";base64,",
base64)
}
else {
base64 = NULL
}
weightFactor = size * 180/max(dataOut$freq)
settings <- list(word = dataOut$name, freq = dataOut$freq,
fontFamily = fontFamily, fontWeight = fontWeight, color = color,
minSize = minSize, weightFactor = weightFactor, backgroundColor = backgroundColor,
gridSize = gridSize, minRotation = minRotation, maxRotation = maxRotation,
shuffle = shuffle, rotateRatio = rotateRatio, shape = shape,
ellipticity = ellipticity, figBase64 = base64, hover = htmlwidgets::JS(hoverFunction))
chart = htmlwidgets::createWidget("wordcloud2", settings,
width = widgetsize[1], height = widgetsize[2], sizingPolicy = htmlwidgets::sizingPolicy(viewer.padding = 0,
browser.padding = 0, browser.fill = TRUE))
chart
}
进一步参见hereexplanation/discussion
使用 wordcloud2 cran 页面 (https://cran.r-project.org/web/packages/wordcloud2/vignettes/wordcloud.html) 上的 rshiny 示例,我在 wordcloud 下方得到了一个额外的小框。每当我使用 wordcloud2 包的 rshiny 功能时,就会发生这种情况:
生成这个的代码只是:
library(wordcloud2)
# Global variables can go here
n <- 1
# Define the UI
ui <- bootstrapPage(
numericInput('size', 'Size of wordcloud', n),
wordcloud2Output('wordcloud2')
)
# Define the server code
server <- function(input, output) {
output$wordcloud2 <- renderWordcloud2({
# wordcloud2(demoFreqC, size=input$size)
wordcloud2(demoFreq, size=input$size)
})
}
shinyApp(ui = ui, server = server)
移除框的一种方法是使用 CSS 样式将元素设置为不显示。这可以通过将此代码添加到 UI:
的主体来完成tags$head(
tags$style(HTML('div#wcLabel {display: none;}'))
)
请注意,当您将鼠标悬停在某个词上时,这也会终止显示术语频率的滚动功能。就我而言,这是可取的。
我使用这个 wordcloud2a()
函数而不是常规的 wordcloud2()
wordcloud2a <- function (data, size = 1, minSize = 0, gridSize = 0, fontFamily = "Segoe UI",
fontWeight = "bold", color = "random-dark", backgroundColor = "white",
minRotation = -pi/4, maxRotation = pi/4, shuffle = TRUE,
rotateRatio = 0.4, shape = "circle", ellipticity = 0.65,
widgetsize = NULL, figPath = NULL, hoverFunction = NULL)
{
if ("table" %in% class(data)) {
dataOut = data.frame(name = names(data), freq = as.vector(data))
}
else {
data = as.data.frame(data)
dataOut = data[, 1:2]
names(dataOut) = c("name", "freq")
}
if (!is.null(figPath)) {
if (!file.exists(figPath)) {
stop("cannot find fig in the figPath")
}
spPath = strsplit(figPath, "\.")[[1]]
len = length(spPath)
figClass = spPath[len]
if (!figClass %in% c("jpeg", "jpg", "png", "bmp", "gif")) {
stop("file should be a jpeg, jpg, png, bmp or gif file!")
}
base64 = base64enc::base64encode(figPath)
base64 = paste0("data:image/", figClass, ";base64,",
base64)
}
else {
base64 = NULL
}
weightFactor = size * 180/max(dataOut$freq)
settings <- list(word = dataOut$name, freq = dataOut$freq,
fontFamily = fontFamily, fontWeight = fontWeight, color = color,
minSize = minSize, weightFactor = weightFactor, backgroundColor = backgroundColor,
gridSize = gridSize, minRotation = minRotation, maxRotation = maxRotation,
shuffle = shuffle, rotateRatio = rotateRatio, shape = shape,
ellipticity = ellipticity, figBase64 = base64, hover = htmlwidgets::JS(hoverFunction))
chart = htmlwidgets::createWidget("wordcloud2", settings,
width = widgetsize[1], height = widgetsize[2], sizingPolicy = htmlwidgets::sizingPolicy(viewer.padding = 0,
browser.padding = 0, browser.fill = TRUE))
chart
}
进一步参见hereexplanation/discussion