Site icon R-bloggers

How to Download and Run R Scripts from this Site

[This article was first published on Fantasy Football Analytics » R | Fantasy Football Analytics, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

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 git@github.com: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’

Error: object ‘projections’ not found

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.