R summary

  • install RStudio (convenient R programming surface)
    • first you have to install R (the programming language) on your system. (follow the instructions on the download page), then you can install RStudio.
  • you need to install additional R-packages (libraries) to excercise the tasks in class, e.g. the package "stylo".
  • if you open RStudio, you have left down you console window, where you can input commands directly. i would recommend open a new R-script datei > neu > R-script to be able to save you commands and automatise your workflow. to execute a command in the script, place the cursor at the line including your command and press CMD+return (mac) or CTRL+return (windows). to execute a command in the terminal window, just type it in there and press return.
  • first command e.g.
install.packages("stylo")
  • then:
library(stylo)
  • mac users at this point may see a message saying you have to install XQuartz. do so, open the link provided and install XQuartz for your system. (it is a small window server which is needed to display the GUI stylo is using.
    • if you in the course of installing R, RStudio or XQuartz are asked if you want to install the XCode developer tools, you can deny that since it takes a while and is about 12GB diskspace and you probably wont need this.
  • to see where you're at, type
getwd()
#this will show you your current working directory.
#any saving or opening files without an absolute
#path will access this directory.
  • you can change your working directory with
setwd("/path/to/your/preferred/dir")
  • or by navigating to that directory in the right bottom window, clicking the zahnrad and choose: set as working directory

try the following snippet to find plays containing a certain name/character in question:

#13266.dracor simple request
library(jsonlite)
einakter <- fromJSON("https://einakter.dracor.org/data.json")
#fetch dataset from dracor server

m<-einakter$printed=="NULL"
sum(m)
einakter$printed[m]<-NA

#build dataframe of name in question
spitcast<-function(set,cast){
  ndf<-data.frame()
  m<-grepl(cast,set$cast)
  print(sum(m))
  m<-grep(cast,set$cast)
  s<-data.frame(author=set$author$name[m],year=unlist(set$printed[m]),title=set$title[m])
  print(s)
  return(s)
}

name_to_analyse<-"Lisette"
ndf<-spitcast(einakter,name_to_analyse)

#print out first 5 elements of dataframe
head(ndf)

you can export the dataframe created above (ndf) with the following line, either to .csv or excel:


#either:
library(writexl)
write_xlsx(ndf,"dracor_names-analysed_dataframe.xlsx")

#or:
write.csv(ndf,"dracor_names-analysed_dataframe.csv")

this will save the dataframe into you working directory.