Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ... to wordcloud2 passing options to wordcloud2.js (also fixed CRAN checks) #72

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]', 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
Expand All @@ -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 <[email protected]>
RoxygenNote: 6.0.1.9000
RoxygenNote: 7.1.1
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
98 changes: 49 additions & 49 deletions R/letterCloud.R
Original file line number Diff line number Diff line change
@@ -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,...)

}
155 changes: 76 additions & 79 deletions R/wordcloud2.R
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -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],
Expand Down
24 changes: 5 additions & 19 deletions inst/htmlwidgets/lib/wordcloud2-0.0.1/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}

x.abortThreshold = 3000;
x.clearCanvas = false;
WordCloud(el.firstChild, x);
}
37 changes: 12 additions & 25 deletions inst/htmlwidgets/wordcloud2.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,25 @@ HTMLWidgets.widget({
},
renderValue: function(el, x, instance) {
// parse gexf data
listData=[];
for(var i=0; i<x.word.length; i++){
listData.push([x.word[i], x.freq[i]]);
}
listData=[];
for(var i=0; i<x.word.length; i++){
listData.push([x.word[i], x.freq[i]]);
}
x.list = listData;
delete x.word;
delete x.freq;
x.hover = x.hover || cv_handleHover;

if(x.figBase64){

maskInit(el,x);
console.log(3)

}else{
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,
drawMask: x.drawMask,
maskColor: x.maskColor,
maskGapWidth: x.maskGapWidth,
hover: x.hover || cv_handleHover
});
WordCloud(el.firstChild, x);
}
},
resize: function(el, width, height) {
}
resize: function(el, width, height) {
}

});
4 changes: 3 additions & 1 deletion man/demoFreq.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading