From d1608e2aa724891809b9fd6b6a9a06e20113f0d2 Mon Sep 17 00:00:00 2001 From: Edwin de Jonge Date: Thu, 17 Dec 2020 15:29:14 +0100 Subject: [PATCH 1/3] added ... to wordcloud2 so extra options can be passed on to wordcloud2.js --- DESCRIPTION | 1 - R/wordcloud2.R | 8 ++-- .../htmlwidgets/lib/wordcloud2-0.0.1/hover.js | 24 +++--------- inst/htmlwidgets/wordcloud2.js | 37 ++++++------------- 4 files changed, 22 insertions(+), 48 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b18e6ac..6dd8a84 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,6 @@ Type: Package Title: Create Word Cloud by htmlWidget Version: 0.2.2 Author: Dawei Lang -Contributer of this version: Puxin(Jacob) Xu Description: A fast visualization tool for creating wordcloud by using wordcloud2.js. License: GPL-2 diff --git a/R/wordcloud2.R b/R/wordcloud2.R index 9ec8bb2..5d516c4 100644 --- a/R/wordcloud2.R +++ b/R/wordcloud2.R @@ -35,6 +35,7 @@ ##' @param widgetsize size of the widgets ##' @param hoverFunction Callback to call when the cursor enters or leaves a region occupied ##' by a word. A string of java script function. +##' @param ... extra options, will be passed onto wordcloud2.js ##' ##' @examples ##'library(wordcloud2) @@ -92,7 +93,8 @@ wordcloud2 <- function(data, ellipticity = 0.65, widgetsize = NULL, figPath = NULL, - hoverFunction = NULL + hoverFunction = NULL, + ... ) { if("table" %in% class(data)){ dataOut = data.frame(name = names(data), @@ -145,10 +147,10 @@ wordcloud2 <- function(data, shape = shape, ellipticity = ellipticity, figBase64 = base64, - hover = htmlwidgets::JS(hoverFunction) + hover = htmlwidgets::JS(hoverFunction), + ... ) - htmlwidgets::createWidget("wordcloud2", settings, width = widgetsize[1], height = widgetsize[2], diff --git a/inst/htmlwidgets/lib/wordcloud2-0.0.1/hover.js b/inst/htmlwidgets/lib/wordcloud2-0.0.1/hover.js index e0140f5..9aebd0e 100644 --- a/inst/htmlwidgets/lib/wordcloud2-0.0.1/hover.js +++ b/inst/htmlwidgets/lib/wordcloud2-0.0.1/hover.js @@ -116,22 +116,8 @@ function maskInit(el,x){ ctx.drawImage(maskCanvasScaled, 0, 0); maskCanvasScaled = ctx = imageData = newImageData = bctx = bgPixel = undefined; - WordCloud(el.firstChild, { list: listData, - fontFamily: x.fontFamily, - fontWeight: x.fontWeight, - color: x.color, - minSize: x.minSize, - weightFactor: x.weightFactor, - backgroundColor: x.backgroundColor, - gridSize: x.gridSize, - minRotation: x.minRotation, - maxRotation: x.maxRotation, - shuffle: x.shuffle, - shape: x.shape, - rotateRatio: x.rotateRatio, - ellipticity: x.ellipticity, - clearCanvas: false, - hover: x.hover || cv_handleHover, - abortThreshold: 3000 - }); -} \ No newline at end of file + + x.abortThreshold = 3000; + x.clearCanvas = false; + WordCloud(el.firstChild, x); +} diff --git a/inst/htmlwidgets/wordcloud2.js b/inst/htmlwidgets/wordcloud2.js index b6e0067..942e1cb 100644 --- a/inst/htmlwidgets/wordcloud2.js +++ b/inst/htmlwidgets/wordcloud2.js @@ -16,38 +16,25 @@ HTMLWidgets.widget({ }, renderValue: function(el, x, instance) { // parse gexf data - listData=[]; - for(var i=0; i Date: Thu, 17 Dec 2020 15:41:26 +0100 Subject: [PATCH 2/3] fixing CRAN checks --- DESCRIPTION | 7 +- NAMESPACE | 6 ++ R/letterCloud.R | 98 +++++++++++++------------- R/wordcloud2.R | 149 +++++++++++++++++++--------------------- man/demoFreq.Rd | 4 +- man/demoFreqC.Rd | 4 +- man/wordcloud2-shiny.Rd | 3 +- man/wordcloud2.Rd | 4 +- wordcloud2.Rproj | 1 + 9 files changed, 142 insertions(+), 134 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6dd8a84..0605d66 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,8 @@ Package: wordcloud2 Type: Package Title: Create Word Cloud by htmlWidget Version: 0.2.2 -Author: Dawei Lang +Author: c(person("Dawei", "Lang", email = 'chiffonlang@icloud.com', role=c("aut", "cre")), + person(person("Puxin", "Xu", "(Jacob)")) Description: A fast visualization tool for creating wordcloud by using wordcloud2.js. License: GPL-2 @@ -17,8 +18,8 @@ Imports: graphics, shiny (>= 0.12) Suggests: - knitr + knitr, rmarkdown URL: https://github.com/lchiffon/wordcloud2 BugReports: https://github.com/lchiffon/wordcloud2/issues Maintainer: Dawei Lang -RoxygenNote: 6.0.1.9000 +RoxygenNote: 7.1.1 diff --git a/NAMESPACE b/NAMESPACE index 5ffb566..8cf7354 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,3 +7,9 @@ export(renderWordcloud2) export(wordcloud2) export(wordcloud2Output) import(htmlwidgets) +importFrom(grDevices,dev.off) +importFrom(grDevices,png) +importFrom(graphics,par) +importFrom(graphics,plot.new) +importFrom(graphics,strwidth) +importFrom(graphics,text) diff --git a/R/letterCloud.R b/R/letterCloud.R index 7d5a139..e960995 100644 --- a/R/letterCloud.R +++ b/R/letterCloud.R @@ -1,49 +1,49 @@ -##' Create wordcloud with the shape of a word -##' -##' @description -##' Function for Creating wordcloud with the shape of a word -##' -##' @usage -##' letterCloud(data, word, wordSize = 0, letterFont = NULL, ...) -##' -##' @param data A data frame including word and freq in each column -##' @param word A word to create shape for wordcloud. -##' @param wordSize Parameter of the size of the word. -##' @param letterFont Letter font -##' @param ... Other parameters for wordcloud. -##' -##' @examples -##' library(wordcloud2) -##' -##' letterCloud(demoFreq,"R") -#' @export - - -letterCloud = function(data, word, wordSize = 0, - letterFont = NULL,...){ - fileid = paste('ID', format(Sys.time(), "%Y%m%d%H%M%S"), - round(proc.time()[3]*100), sep="_") - figDir = paste0(tempdir(),"/",fileid,".png") - - # word = "COS" - if(nchar(word)==1){ - ofCex = -25 - }else if(nchar(word)==2){ - ofCex = -10 - }else{ - ofCex = -1 - } - png(filename = figDir,width = 800,height = 600) - offset = par(mar = par()$mar) - op = par(mar = c(0,0,0,0)) - plot.new() - - text(0.5, 0.5, word, font = 2, family = letterFont, - cex = 1/strwidth(word) + ofCex + wordSize) - - dev.off() - par(offset) - - wordcloud2(data,figPath = figDir,...) - -} +#' Create wordcloud with the shape of a word +#' +#' @description +#' Function for Creating wordcloud with the shape of a word +#' +#' @usage +#' letterCloud(data, word, wordSize = 0, letterFont = NULL, ...) +#' +#' @param data A data frame including word and freq in each column +#' @param word A word to create shape for wordcloud. +#' @param wordSize Parameter of the size of the word. +#' @param letterFont Letter font +#' @param ... Other parameters for wordcloud. +#' @importFrom grDevices dev.off png +#' @importFrom graphics par plot.new strwidth text +#' @importFrom graphics par plot.new strwidth text +#' @examples +#' library(wordcloud2) +#' +#' letterCloud(demoFreq,"R") +#' @export +letterCloud = function(data, word, wordSize = 0, + letterFont = NULL,...){ + fileid = paste('ID', format(Sys.time(), "%Y%m%d%H%M%S"), + round(proc.time()[3]*100), sep="_") + figDir = paste0(tempdir(),"/",fileid,".png") + + # word = "COS" + if(nchar(word)==1){ + ofCex = -25 + }else if(nchar(word)==2){ + ofCex = -10 + }else{ + ofCex = -1 + } + png(filename = figDir,width = 800,height = 600) + offset = par(mar = par()$mar) + op = par(mar = c(0,0,0,0)) + plot.new() + + text(0.5, 0.5, word, font = 2, family = letterFont, + cex = 1/strwidth(word) + ofCex + wordSize) + + dev.off() + par(offset) + + wordcloud2(data,figPath = figDir,...) + +} diff --git a/R/wordcloud2.R b/R/wordcloud2.R index 5d516c4..11fa167 100644 --- a/R/wordcloud2.R +++ b/R/wordcloud2.R @@ -1,82 +1,77 @@ -##' Create wordcloud by wordcloud2.js -##' -##' @description -##' Function for Creating wordcloud by wordcloud2.js -##' -##' @usage -##' wordcloud2(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) -##' -##' @param data A data frame including word and freq in each column -##' @param size Font size, default is 1. The larger size means the bigger word. -##' @param minSize minimum font size to draw on the canvas. -##' @param gridSize Size of the grid in pixels for marking the availability of the canvas -##' the larger the grid size, the bigger the gap between words. -##' @param fontFamily Font to use. -##' @param fontWeight Font weight to use, e.g. normal, bold or 600 -##' @param color color of the text, keyword 'random-dark' and 'random-light' can be used. -##' color vector is also supported in this param -##' @param backgroundColor Color of the background. -##' @param minRotation If the word should rotate, the minimum rotation -##' (in rad) the text should rotate. -##' @param maxRotation If the word should rotate, the maximum rotation (in rad) the text should rotate. -##' Set the two value equal to keep all text in one angle. -##' @param shuffle Shuffle the points to draw so the result will be different each time for the same list and settings. -##' @param rotateRatio Probability for the word to rotate. Set the number to 1 to always rotate. -##' @param shape The shape of the "cloud" to draw. Can be a keyword present. Available presents are 'circle' -##' (default), 'cardioid' (apple or heart shape curve, the most known polar equation), -##' 'diamond' (alias of square), 'triangle-forward', 'triangle', 'pentagon', and 'star'. -##' @param ellipticity degree of "flatness" of the shape wordcloud2.js should draw. -##' @param figPath The path to a figure used as a mask. -##' @param widgetsize size of the widgets -##' @param hoverFunction Callback to call when the cursor enters or leaves a region occupied -##' by a word. A string of java script function. -##' @param ... extra options, will be passed onto wordcloud2.js -##' -##' @examples -##'library(wordcloud2) -##'# Global variables can go here -##' -##' -##' -##' wordcloud2(demoFreq) -##' wordcloud2(demoFreq, size = 2) -##' -##' wordcloud2(demoFreq, size = 1,shape = 'pentagon') -##' wordcloud2(demoFreq, size = 1,shape = 'star') -##' -##' wordcloud2(demoFreq, size = 2, -##' color = "random-light", backgroundColor = "grey") -##' -##' wordcloud2(demoFreq, size = 2, minRotation = -pi/2, maxRotation = -pi/2) -##' wordcloud2(demoFreq, size = 2, minRotation = -pi/6, maxRotation = -pi/6, -##' rotateRatio = 1) -##' wordcloud2(demoFreq, size = 2, minRotation = -pi/6, maxRotation = pi/6, -##' rotateRatio = 0.9) -##' -##' wordcloud2(demoFreqC, size = 2, -##' color = "random-light", backgroundColor = "grey") -##' wordcloud2(demoFreqC, size = 2, minRotation = -pi/6, maxRotation = -pi/6, -##' rotateRatio = 1) -##' -##' # Color Vector -##' -##' colorVec = rep(c('red', 'skyblue'), length.out=nrow(demoFreq)) -##' wordcloud2(demoFreq, color = colorVec, fontWeight = "bold") -##' -##' wordcloud2(demoFreq, -##' color = ifelse(demoFreq[, 2] > 20, 'red', 'skyblue')) - - - +#' Create wordcloud by wordcloud2.js +#' +#' @description +#' Function for Creating wordcloud by wordcloud2.js +#' +#' @usage +#' wordcloud2(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, ...) +#' +#' @param data A data frame including word and freq in each column +#' @param size Font size, default is 1. The larger size means the bigger word. +#' @param minSize minimum font size to draw on the canvas. +#' @param gridSize Size of the grid in pixels for marking the availability of the canvas +#' the larger the grid size, the bigger the gap between words. +#' @param fontFamily Font to use. +#' @param fontWeight Font weight to use, e.g. normal, bold or 600 +#' @param color color of the text, keyword 'random-dark' and 'random-light' can be used. +#' color vector is also supported in this param +#' @param backgroundColor Color of the background. +#' @param minRotation If the word should rotate, the minimum rotation +#' (in rad) the text should rotate. +#' @param maxRotation If the word should rotate, the maximum rotation (in rad) the text should rotate. +#' Set the two value equal to keep all text in one angle. +#' @param shuffle Shuffle the points to draw so the result will be different each time for the same list and settings. +#' @param rotateRatio Probability for the word to rotate. Set the number to 1 to always rotate. +#' @param shape The shape of the "cloud" to draw. Can be a keyword present. Available presents are 'circle' +#' (default), 'cardioid' (apple or heart shape curve, the most known polar equation), +#' 'diamond' (alias of square), 'triangle-forward', 'triangle', 'pentagon', and 'star'. +#' @param ellipticity degree of "flatness" of the shape wordcloud2.js should draw. +#' @param figPath The path to a figure used as a mask. +#' @param widgetsize size of the widgets +#' @param hoverFunction Callback to call when the cursor enters or leaves a region occupied +#' by a word. A string of java script function. +#' @param ... extra options, will be passed onto wordcloud2.js +#' +#' @examples +#'library(wordcloud2) +#'# Global variables can go here +#' +#' +#' +#' wordcloud2(demoFreq) +#' wordcloud2(demoFreq, size = 2) +#' +#' wordcloud2(demoFreq, size = 1,shape = 'pentagon') +#' wordcloud2(demoFreq, size = 1,shape = 'star') +#' +#' wordcloud2(demoFreq, size = 2, +#' color = "random-light", backgroundColor = "grey") +#' +#' wordcloud2(demoFreq, size = 2, minRotation = -pi/2, maxRotation = -pi/2) +#' wordcloud2(demoFreq, size = 2, minRotation = -pi/6, maxRotation = -pi/6, +#' rotateRatio = 1) +#' wordcloud2(demoFreq, size = 2, minRotation = -pi/6, maxRotation = pi/6, +#' rotateRatio = 0.9) +#' +#' wordcloud2(demoFreqC, size = 2, +#' color = "random-light", backgroundColor = "grey") +#' wordcloud2(demoFreqC, size = 2, minRotation = -pi/6, maxRotation = -pi/6, +#' rotateRatio = 1) +#' +#' # Color Vector +#' +#' colorVec = rep(c('red', 'skyblue'), length.out=nrow(demoFreq)) +#' wordcloud2(demoFreq, color = colorVec, fontWeight = "bold") +#' +#' wordcloud2(demoFreq, +#' color = ifelse(demoFreq[, 2] > 20, 'red', 'skyblue')) #' @import htmlwidgets #' @export -# data = data.frame(name=c("New","Old"), -# freq=c(100,30)) wordcloud2 <- function(data, size = 1, minSize = 0, diff --git a/man/demoFreq.Rd b/man/demoFreq.Rd index 00105ca..f50ac31 100644 --- a/man/demoFreq.Rd +++ b/man/demoFreq.Rd @@ -4,7 +4,9 @@ \name{demoFreq} \alias{demoFreq} \title{Demo dataset with Words and Frequency} -\format{A data set with 1011 observations of 2 variables, words and frequancy} +\format{ +A data set with 1011 observations of 2 variables, words and frequancy +} \usage{ demoFreq } diff --git a/man/demoFreqC.Rd b/man/demoFreqC.Rd index 9dc5b80..b1e8c72 100644 --- a/man/demoFreqC.Rd +++ b/man/demoFreqC.Rd @@ -4,7 +4,9 @@ \name{demoFreqC} \alias{demoFreqC} \title{Demo dataset with Chinese character Words and Frequency} -\format{A data set with 885 observations of 2 variables, words and frequancy} +\format{ +A data set with 885 observations of 2 variables, words and frequancy +} \usage{ demoFreqC } diff --git a/man/wordcloud2-shiny.Rd b/man/wordcloud2-shiny.Rd index be3c2fa..696d5b9 100644 --- a/man/wordcloud2-shiny.Rd +++ b/man/wordcloud2-shiny.Rd @@ -6,9 +6,7 @@ \alias{renderWordcloud2} \title{Plot wordcloud2 in shiny} \usage{ - wordcloud2Output(outputId, width = "100\%", height = "400px") -clickedWordInputId is automatically generated by: paste0(outputId, "_clicked_word")) renderWordcloud2(expr, env = parent.frame(), quoted = FALSE) @@ -38,4 +36,5 @@ Use renderWordcloud2 to render an wordcloud2 object and use wordcloud2Output output an wordcloud2 object. See more details in shiny package. This will be the same value as shown in the hover information (example: \code{super:32}). +clickedWordInputId is automatically generated by: paste0(outputId, "_clicked_word")) } diff --git a/man/wordcloud2.Rd b/man/wordcloud2.Rd index 49c2a80..41dbbd6 100644 --- a/man/wordcloud2.Rd +++ b/man/wordcloud2.Rd @@ -9,7 +9,7 @@ wordcloud2(data, size = 1, minSize = 0, gridSize = 0, 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) + widgetsize = NULL, figPath = NULL, hoverFunction = NULL, ...) } \arguments{ \item{data}{A data frame including word and freq in each column} @@ -52,6 +52,8 @@ Set the two value equal to keep all text in one angle.} \item{hoverFunction}{Callback to call when the cursor enters or leaves a region occupied by a word. A string of java script function.} + +\item{...}{extra options, will be passed onto wordcloud2.js} } \description{ Function for Creating wordcloud by wordcloud2.js diff --git a/wordcloud2.Rproj b/wordcloud2.Rproj index 497f8bf..270314b 100644 --- a/wordcloud2.Rproj +++ b/wordcloud2.Rproj @@ -18,3 +18,4 @@ StripTrailingWhitespace: Yes BuildType: Package PackageUseDevtools: Yes PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,collate,namespace From 3d761da0071a10e6b864212e6db099210476e233 Mon Sep 17 00:00:00 2001 From: Edwin de Jonge Date: Thu, 17 Dec 2020 15:44:24 +0100 Subject: [PATCH 3/3] updating authors --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 0605d66..e09b774 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,8 @@ Type: Package Title: Create Word Cloud by htmlWidget Version: 0.2.2 Author: c(person("Dawei", "Lang", email = 'chiffonlang@icloud.com', role=c("aut", "cre")), - person(person("Puxin", "Xu", "(Jacob)")) + person("Puxin", "Xu", "(Jacob)", role="ctb"), + persion("Timothy", "Guan-tin Chien", role="aut", comment="author of wordcloud2.js")) Description: A fast visualization tool for creating wordcloud by using wordcloud2.js. License: GPL-2