diff --git a/DESCRIPTION b/DESCRIPTION index b18e6ac..e09b774 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,8 +2,9 @@ Package: wordcloud2 Type: Package Title: Create Word Cloud by htmlWidget Version: 0.2.2 -Author: Dawei Lang -Contributer of this version: Puxin(Jacob) Xu +Author: c(person("Dawei", "Lang", email = 'chiffonlang@icloud.com', role=c("aut", "cre")), + 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 @@ -18,8 +19,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 9ec8bb2..11fa167 100644 --- a/R/wordcloud2.R +++ b/R/wordcloud2.R @@ -1,81 +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. -##' -##' @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, @@ -92,7 +88,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 +142,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