How to Download and Run R Scripts from this Site

October 23, 2014
By

(This article was first published on Fantasy Football Analytics » R | Fantasy Football Analytics, and kindly contributed to R-bloggers)

This post outlines how to download and run R scripts from this website.  We have many Fantasy Football scripts that show how to download and calculate fantasy projectionsdetermine the riskiness of a playeridentify sleepers, and much more!

What Software You Need

  • Git and GitHub (for downloading the scripts)
  • R (for running the scripts)
  • RStudio (best text editor for viewing, editing, and running R scripts)

Downloading the R Scripts

First, install and setup Git and GitHub. Git is version control software, and GitHub is a service for storing Git projects called repositories.  We store data and R scripts on our GitHub repository (repo) located here:

https://github.com/dadrivr/FantasyFootballAnalyticsR

After setting up Git and GitHub, you can download our data and scripts by forking our GitHub repo.  Go to our repo (above) and, in the top-right corner of the page, click “Fork”.  Once you have forked the repo, you can download the repo files to your local computer by running the following line of code in the command line (Windows) or Terminal (Mac):

git clone [email protected]:YOUR-USERNAME/FantasyFootballAnalyticsR.git

For more info on how to fork a repository (so you can download our data and scripts), including how to keep your local copy in sync with changes to the repo, see here.

Running the R Scripts

First, install R and RStudio.  After installing R and RStudio, open RStudio.  In RStudio, open an R script (.R extension).  You can run individual lines of code by highlighting the lines you want to run and clicking “Run” in the top-right corner.  You can run the whole R script by clicking “Source” in the top-right corner.  Many of our R scripts depend on first running two scripts called Functions.R and League Settings.R.  You can run them using the source() function:

source(paste(getwd(),"/R Scripts/Functions/Functions.R", sep=""))
source(paste(getwd(),"/R Scripts/Functions/League Settings.R", sep=""))

Before running the scripts, you will have to download any packages used by the scripts you are trying to run.  We load the packages needed at the beginning of each script with the library() function.  For instance, in the example below, we need the reshape, MASS, and psych packages:

#Library
library("reshape")
library("MASS")
library("psych")

Before you can load these packages the first time, however, you will have to install the packages.  Here’s how to install a package:

install.packages("reshape")

For more info on how to install R packages, see here.  You may also have to change the working directory to be the root directory of the repository so the script knows where to look for files.  Here’s how to set the working directory:

setwd("C:/ENTER/YOUR/DIRECTORY/HERE")

For more info on how to set the working directory, see here.

Sharing Your R Scripts with the Community

If you create new scripts or make changes to the R scripts, you are encouraged to share them with the Fantasy Football Analytics community.  This is why we make the scripts available to you for free—so the community can collectively use and improve them.  This benefits everyone.  Please consider sharing your new or improved files by creating a pull request back to our repo.  For info on how to create a pull request, see here.

Troubleshooting

You may run into errors when running the R scripts.  Here are some common issues and how to troubleshoot.

Error in library(“XML”) : there is no package called ‘XML’

  • This suggests that you are trying to load the XML package without first installing it.  Here’s how to install it:
  • install.packages(“XML”)
  • For more info on how to install packages, see here.

Error: object ‘projections’ not found

  • This suggests that you did not load the data file at the top of the script—the line of code should look something like: load(filename.RData)
  • Make sure you downloaded the data file to the correct location, you set your working directory, and run this line of code to the load the data.

Learning R and GitHub

For info on how to learn R, see here.  For a beginner’s guide to GitHub, see here.

The post How to Download and Run R Scripts from this Site appeared first on Fantasy Football Analytics.

To leave a comment for the author, please follow the link and comment on their blog: Fantasy Football Analytics » R | Fantasy Football Analytics.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)