Stylometry with R & RStudio

INSTRUCTIONS FOR USING R FOR STYLOMETRIC ANALYSIS

IMPORTANT CAVEAT: If you’re on a mac, you might also need to download a separate application entirely: XQuartz.  You’ll find out when you try to run stylo.

PREP WORK—R
GET R AND RSTUDIO

1) Install R from this site: https://cran.cnr.berkeley.edu/  (pick the one that is correct for your operating system).
2) Install RStudio, which will be your development environment for R: https://rstudio.com/products/rstudio/download/#download (again, pick the one the will work for your operating system).
PREP WORK—CORPUS
GATHER AND STRUCTURE YOUR CORPUS (THIS SOUNDS FANCY BUT IT IS NOT)
1) gather your corpus. 
Your corpus can be any collection of txt files you are interested in running this package on. They must be txt files. In this example we’ll say I’m interested in finding out which of the GoT novels, according to the stylo() package, are closest in style to each other.
2) Structure your corpus.
This is a simple but important step: On your desktop, make a NEW folder and name it something meaningful. I’ve called mine my_awesome_corpus.
Inside this folder make a new subfolder called corpusPut all of your txt files in that corpus subfolder.  Notice in my example all of the novels in Game of Thrones are in a folder called “corpus,” which is located within a parent folder called “my_awesome_corpus”
3) Confirm that the  my_awesome_corpus folder is on your desktop.
LAUNCH RSTUDIO
SET YOUR WORKING DIRECTORY (TELL RSTUDIO WHERE YOUR FILES ARE)
1) In the upper bar select SESSION
2) From here select SET WORKING DIRECTORY
3) From here select CHOOSE DIRECTORY, navigate to your desktop and select “my_awesome_corpus”
4) Important: DON’T SELECT THE CORPUS SUBFOLDER.
5) Instead, look at the lower right section of RStudio. If the “files” tab is selected you’ll see how it’s pointing to your “corpus” folder.
IMPORT YOUR NECESSARY PACKAGES
(ape, class, el071, pamr, stylo, tsne, tcltk2)
1)Take a look at the lower right section of RStudio.  At the top of this section you’ll see some tabs: Files, Plots, Packages, Help, Viewer
2) From here select PACKAGES
3) Note you have a whole bunch of pre-packaged packages.  But you probably don’t have the ones you need.
4) Select “import,” which is directly under the PACKAGES tab, and enter the following package names, separated by commas:  ape, class, el071, pamr, stylo, tsne, tcltk2
5) Select install
6) NOTE: If you’re on a mac, you might also need to download a separate application entirely: XQuartz.  I’m hoping they’ve fixed this issue, because it’s a pain in the rumpus, but in case not, you can get it here: https://www.xquartz.org/  If you end up needing it, quit RStudio and download XQuartz the way you downloaded R and Rstudio (from your browser: https://www.xquartz.org/).  Once it’s installed, re-open RStudio and try loading all these packages again.  If you get another error, cut your losses, and I’ll take a look at it.  You might need to authorize it from Terminal, which means you will need admin access on your machine.  We’ll try to figure it out together. If you’re on a pc, you should be good to go.  (This is, I think, the only time I’ve ever written that.)
MAKE YOUR PLOT!
Assuming you’ve set your working directory correctly and imported all these nifty packages, making a plot is super easy.  Here is the only code you need: stylo()
That’s it!
1) In the lower left pane of RStudio type this magic code and press enter: stylo()
2) a pop-up box will come up (see below).  Just press “ok'”
3) You’ll see some work in the lower left pane as R processes the request:
4) In just a few seconds, your plot will appear.
Share on FacebookTweet about this on TwitterPin on PinterestShare on Google+

Leave a Reply