【问题标题】:wordcloud2 shiny output creates extra widgetwordcloud2 闪亮的输出创建额外的小部件
【发布时间】:2020-07-08 16:57:09
【问题描述】:

使用 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)

【问题讨论】:

  • 我不确定你是否注意到了。有一个鼠标悬停功能,可让您查看每个单词存在多少观察。将鼠标悬停在一个单词上,您会看到一个带有单词和出现次数的小框。此外,您会注意到您在左下角谈论的小框将消失。因此,在您将鼠标悬停在某个单词上之前(并创建一个“新框”),该框一直位于左下角。我同意,这似乎是该框的一个奇怪的默认选择。

标签: r shiny word-cloud


【解决方案1】:

我通过使用这个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
}

更多解释/讨论请见here

【讨论】:

    【解决方案2】:

    移除框的一种方法是使用 CSS 样式将元素设置为不显示。只需将此代码添加到 UI 主体即可完成:

    tags$head(
          tags$style(HTML('div#wcLabel {display: none;}'))
        )
    

    请注意,当您将鼠标悬停在某个单词上时,这也会取消显示术语频率的滚动功能。就我而言,这是可取的。

    【讨论】:

    • 对我有用的是tags$style(type='text/css', '.wcLabel {display:none;}'),
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 2021-10-04
    相关资源
    最近更新 更多