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 parameter seed to the widget. #28

Open
wants to merge 2 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
48 changes: 24 additions & 24 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
Package: wordcloud2
Type: Package
Title: Create Word Cloud by htmlWidget
Version: 0.2.0
Author: Dawei Lang
Description: A fast visualization tool for creating wordcloud
by using wordcloud2.js.
License: GPL-2
Depends:
R (>= 3.1.0)
LazyData: TRUE
VignetteBuilder: knitr
Imports:
htmlwidgets,
base64enc,
grDevices,
graphics
Suggests:
knitr
Enhances: shiny (>= 0.12)
URL: https://github.com/lchiffon/wordcloud2
BugReports: https://github.com/lchiffon/wordcloud2/issues
Maintainer: Dawei Lang <[email protected]>
RoxygenNote: 5.0.1
Package: wordcloud2
Type: Package
Title: Create Word Cloud by htmlWidget
Version: 0.2.0
Author: Dawei Lang
Description: A fast visualization tool for creating wordcloud
by using wordcloud2.js.
License: GPL-2
Depends:
R (>= 3.1.0)
LazyData: TRUE
VignetteBuilder: knitr
Imports:
htmlwidgets,
base64enc,
grDevices,
graphics
Suggests:
knitr
Enhances: shiny (>= 0.12)
URL: https://github.com/lchiffon/wordcloud2
BugReports: https://github.com/lchiffon/wordcloud2/issues
Maintainer: Dawei Lang <[email protected]>
RoxygenNote: 6.0.1
26 changes: 15 additions & 11 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Generated by roxygen2 (4.1.1): do not edit by hand

S3method("+",wordcloud2)
export(WCtheme)
export(letterCloud)
export(renderWordcloud2)
export(wordcloud2)
export(wordcloud2Output)
import(htmlwidgets)
importFrom("grDevices", "dev.off", "png")
importFrom("graphics", "par", "plot.new", "strwidth", "text")
# Generated by roxygen2: do not edit by hand

S3method("+",wordcloud2)
export(WCtheme)
export(letterCloud)
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)
100 changes: 51 additions & 49 deletions R/letterCloud.R
Original file line number Diff line number Diff line change
@@ -1,49 +1,51 @@
##' 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.
##'
##' @examples
##' library(wordcloud2)
##'
##' letterCloud(demoFreq,"R")
#' @importFrom grDevices dev.off png
#' @importFrom graphics par plot.new strwidth text
#' @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,...)

}
14 changes: 9 additions & 5 deletions R/wordcloud2.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
##' 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, seed = 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.
Expand All @@ -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 seed Seed to pass to the JS library. Useful if you want a reproducible figure.
##'
##' @examples
##'library(wordcloud2)
Expand Down Expand Up @@ -92,7 +93,8 @@ wordcloud2 <- function(data,
ellipticity = 0.65,
widgetsize = NULL,
figPath = NULL,
hoverFunction = NULL
hoverFunction = NULL,
seed = NULL
) {
if("table" %in% class(data)){
dataOut = data.frame(name = names(data),
Expand Down Expand Up @@ -145,10 +147,10 @@ wordcloud2 <- function(data,
shape = shape,
ellipticity = ellipticity,
figBase64 = base64,
hover = htmlwidgets::JS(hoverFunction)
hover = htmlwidgets::JS(hoverFunction),
seed = seed
)


chart = htmlwidgets::createWidget("wordcloud2", settings,
width = widgetsize[1],
height = widgetsize[2],
Expand All @@ -161,10 +163,12 @@ wordcloud2 <- function(data,


htmlwidgets::onRender(chart,"function(el,x){
var iii;
iii = true;
console.log(123);
if(!iii){
window.location.reload();
iii = False;
iii = false;

}
}")
Expand Down
27 changes: 21 additions & 6 deletions inst/htmlwidgets/lib/wordcloud2-0.0.1/wordcloud2-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ if (!window.clearImmediate) {
classes: null,

hover: null,
click: null
click: null,

seed: null
};

if (options) {
Expand All @@ -219,6 +221,19 @@ if (!window.clearImmediate) {
}
}

/* Define a random function using a seed */
var random;
if (settings.seed === null) {
random = function random() {
return Math.random();
};
} else {
random = function random() {
var x = Math.sin(settings.seed++) * 10000;
return x - Math.floor(x);
};
}

/* Convert weightFactor into a function */
if (typeof settings.weightFactor !== 'function') {
var factor = settings.weightFactor;
Expand Down Expand Up @@ -333,9 +348,9 @@ if (!window.clearImmediate) {
var getTextColor;
function random_hsl_color(min, max) {
return 'hsl(' +
(Math.random() * 360).toFixed() + ',' +
(Math.random() * 30 + 70).toFixed() + '%,' +
(Math.random() * (max - min) + min).toFixed() + '%)';
(random() * 360).toFixed() + ',' +
(random() * 30 + 70).toFixed() + '%,' +
(random() * (max - min) + min).toFixed() + '%)';
}
switch (settings.color) {
case 'random-dark':
Expand Down Expand Up @@ -468,15 +483,15 @@ if (!window.clearImmediate) {
return 0;
}

if (Math.random() > settings.rotateRatio) {
if (random() > settings.rotateRatio) {
return 0;
}

if (rotationRange === 0) {
return minRotation;
}

return minRotation + Math.random() * rotationRange;
return minRotation + random() * rotationRange;
};

var getTextInfo = function getTextInfo(word, weight, rotateDeg) {
Expand Down
3 changes: 2 additions & 1 deletion inst/htmlwidgets/wordcloud2.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ HTMLWidgets.widget({
drawMask: x.drawMask,
maskColor: x.maskColor,
maskGapWidth: x.maskGapWidth,
hover: x.hover || cv_handleHover
hover: x.hover || cv_handleHover,
seed: x.seed
});
}
},
Expand Down
3 changes: 1 addition & 2 deletions man/WCtheme.Rd

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

29 changes: 14 additions & 15 deletions man/demoFreq.Rd

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

29 changes: 14 additions & 15 deletions man/demoFreqC.Rd

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

Loading