This post serves three function:
- It allows me to revisit an old blogpost
- It let's me test out the new-ish knitr function knti2wp and RWordPress
- It enables me to avoid the massive ammount of reading I need to do and still feel like I'm doing “work”
The follwoing packages are needed to run the code:
install.packages(c("knitr", "qdap")) install.packages("RWordPress", repos = "http://www.omegahat.org/R", type = "source") library(qdap) library(knitr) library(RWordPress)
*Mac users see this link and this link
In this blogpost I explored the use of gradient word clouds. It took 31 lines of code to plot the figure. I'm lazy (though I tell other's efficient) and 31 lines is enough to keep me from exploring with the gradient word cloud. In a recent update to qdap I included a function to greatly reduce the lines of code in that post to 6, making gadient clouds more accessible.
Grab the Presidential Debate Transcript
# download transcript of the debate to working directory url_dl(pres.deb1.docx)
Read in the Data
# load multiple files with read transcript and assign to working directory dat1 <- read.transcript("pres.deb1.docx", c("person", "dialogue")) # qprep for quick cleaning dat1$dialogue <- qprep(dat1$dialogue) # view a truncated version of the data (see also htruncdf) left.just(htruncdf(dat1, 10, 45))
## person dialogue ## 1 LEHRER We'll talk about specifically about health ca ## 2 ROMNEY What I support is no change for current retir ## 3 LEHRER And what about the vouchers? ## 4 ROMNEY So that's that's number one. Number two is fo ## 5 OBAMA Jim, if I if I can just respond very quickly, ## 6 LEHRER Talk about that in a minute. ## 7 OBAMA but but but overall. ## 8 LEHRER OK. ## 9 OBAMA And so... ## 10 ROMNEY That's that's a big topic. Can we can we stay
Remove Lehrer (need bivariate variable) and plot
dat2 <- rm_row(dat1, 1, "LEHRER") #make a bivariate column (remove LEHRER) gradient_cloud(dat2$dialogue, dat2$person, title = "Debate", X = "blue", Y = "red", stopwords = BuckleySaltonSWL, max.word.size = 2.2, min.word.size = 0.55)
Notice we have control over min/max word size, the two colors and stopwords? Easy huh?
Try a Few more with Different Parameters
gradient_cloud(dat2$dialogue, dat2$person, title = "fun", X = "green", Y = "orange") gradient_cloud(dat2$dialogue, dat2$person, title = "fun", rev.binary = TRUE) gradient_cloud(dat2$dialogue, dat2$person, title = "fun", max.word.size = 5, min.word.size = 0.025)
Now Discussion on knitr to WordPress
Here is the Rmd (text) of the file used to make this post.
Here's the format I used to send the file to WordPress.com
options(WordPressLogin = c(USERNAME = "PASSWORD"), WordPressURL = "https://trinkerrstuff.wordpress.com/xmlrpc.php") library(knitr) knit2wp(file.path("C:/Users/trinker/Desktop/gradient_clouds_revisited/PRESENTATION", "gradient_clouds_revisited.Rmd"), title = "knitr2wordpress and gradient_cloud Revisited", shortcode = TRUE) knit2wp("yourfile.Rmd", title = "knitr2wordpress and gradient_cloud Revisited")
Where USERNAME and PASSWORD are your WordPress username and password.
Please note that there was some confusion I had about where the
base.dir went. For more on this problem see this thread.
I enjoy your posts Tyler 🙂
Very interesting… thank you!
I’ve found something that didn’t work, a small “bug” (i’m not a developer, so take it easy), in the gradient_wordcloud the parameter “min.freq” it’s fixed to 1 inside the funtion. I’ve changed it and now works for me. I don’t know how to use “github” so I can’t change it o inform in that place, sorry.
@Manuel Fdez-Moya good catch. I have fixed this in the devl. version @GitHub. By the way github is pretty easy to rasie an issue. (1) Go to the issues page of a repo: https://github.com/trinker/qdap/issues?state=open (2) click “new issue and type in you comment just like you did here. Plus you can use mark down to make stuff look nice without a lot of effort. if you’d like. I appreciate the feedback. I opened an issue here: https://github.com/trinker/qdap/issues/119 so you can see what it looks like. I put you name on the bug fix in the NEWS file: https://github.com/trinker/qdap/blob/master/NEWS
Thanks Tyler, I think this is exactly the link between WP and knitR i was looking for.