<- read_csv("data.csv") # just the file name test_data
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)
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.
<- read_csv(here("data", # folder name: data
test_data "data.csv")) # file name: data.csv
If your file is a .xlsx
<- read_xlsx("data.xlsx", # file name
data_from_excel 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:
<- 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!) data_from_dropbox
If your file is on Google Sheets:
# get the sheet id from the url
<- "insert sheet id here"
sheet_id
<- read_sheet(sheet_id, # the sheet id that you stored above
data_from_google_sheets sheet = "insert sheet name here") # if you have multiple sheets, you can select the sheet you want
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.