Reading your data into RStudio

Ways to read in data

There are lots of ways that you can read your data into RStudio, depending on the format of your data (.csv, .xlsx, etc.) and where it is (for example, if you have a folder called data in which your data lives, or if your data is in the same folder as your code).

These are a few examples of different ways of reading in your data, but experiment with different set ups to see what works for you!

If your file is a .csv

If your file is in the root directory (i.e. the same folder as the .Rproj file)

test_data <- read_csv("data.csv") # just the file name

Using the here package

here allows you to specify file paths without using relative file paths (like slashes). This makes it easier for people on different operating systems to use your code.

In this example, the data.csv is in a folder called data.

read_csv() wraps around the here("data", "data.csv") function to read in the data.csv file in the data folder.

test_data <- read_csv(here("data", # folder name: data
                           "data.csv")) # file name: data.csv

If your file is a .xlsx

data_from_excel <- read_xlsx("data.xlsx", # file name
                             sheet = "This is the sheet name", # if there are multiple sheets in the .xlsx file
                             na = c("NA")) # explicitly name what the NAs are in the sheet - not necessary, but sometimes nice!

If your file is on Dropbox:

data_from_dropbox <- source_data("share link goes here") # paste the share link, change dl=0 to dl=1 at the end (don't know why but it works!)

Directions

If your file is on Google Sheets:

# get the sheet id from the url
sheet_id <- "insert sheet id here"

data_from_google_sheets <- read_sheet(sheet_id, # the sheet id that you stored above
                                      sheet = "insert sheet name here") # if you have multiple sheets, you can select the sheet you want
Authentication with googlesheets4

Note that when you run this, googlesheets4 will ask for authentication. Critically, this only works if you are there to interact with the package (i.e. actually authenticate yourself); therefore, this is not a good option if you are rendering/knitting your code. One solution is to make the sheet public (which isn’t always ideal). Another solution is to have a separate script in your project that reads stuff in from Google Sheets and creates .csv files on your computer, and use the .csv files downloaded from Google Sheets in your down stream analysis.