knitr2wordpress and gradient_cloud Revisited

This post serves three function:

  1. It allows me to revisit an old blogpost
  2. It let's me test out the new-ish knitr function knti2wp and RWordPress
  3. 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)

plot of chunk grad_cloud

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 = "http://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.url and
base.dir went. For more on this problem see this thread.

About these ads

About tylerrinker

I am Literacy PhD student with a bent for the quantitative and a passion for R.
This entry was posted in knitr, qdap, text, Uncategorized, visualization, word cloud and tagged , , , , , , , , , , , , , , , , . Bookmark the permalink.

4 Responses to knitr2wordpress and gradient_cloud Revisited

  1. I enjoy your posts Tyler :)

  2. Manuel Fdez-Moya says:

    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.

  3. norcalbiostat says:

    Thanks Tyler, I think this is exactly the link between WP and knitR i was looking for.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s