Comma-separated value (.csv
) files are a very common format of data that can be easily transferred between many different applications, including R.
You may have a .csv
file on your computer that you would like to work with on the server. To do this:
.csv
file on your computer.csv
, then select “Upload” and “OK”.Congratulations! Your .csv
file is now located on the server. However, it’s not in your environment yet, meaning it is not ready to be used.
.csv
into the R environmentWhether you are using desktop R or the RStudio server, the steps below will walk you through importing your .csv
into your R environment.
Before you begin, open RStudio. If you are using the server, log in and start a new session if you are not already in one.
.csv
However, reproducibility is important!
.csv
. Running this code will import your .csv into your environment.Instead of copying this code from the “Import files” window, you can also write it yourself. Instructions for doing so are in the section below.
readr
To load a .csv
file into your R environment, you can use the readr
package.
First, if you haven’t already, install the readr
package:
install.packages("readr")
Now that you have the package installed, load the package into your environment with library()
.
library(readr)
Now that you have loaded the package, you can load your data.
First, you will need to know where the data is on your computer, or on the RStudio server. You will need the filepath to the data to show readr
where to find the file. Since this is a .csv
file, you will use the read_csv()
function to read in the file.
Here is an example:
cat_data <- read_csv("Desktop/Reed/Bio123/cats.csv")
The above code reads in the file cats.csv
, located in a folder Bio123
within a Reed
folder on the Desktop
. The filepath Desktop/Reed/Bio123
tells read_csv()
where to find cats.csv
.
To find a filepath on computer running MacOSX you can follow these steps:
To find a filepath on a computer running Windows follow these steps:
The copied value can now be used as the argument in the read_csv()
function.
read_csv()
not only finds this data, but also makes it into a dataset, which is then assigned to the name (cat_data
) using the <-
operator. This data is now available for use in R (you can confirm this using View()
, which opens the dataset in a new window).
View(cat_data)
If you are using the Reed RStudio Server, you can upload your data file to the server (“Files” pane, lower right) and read it in from the server (“Environment” pane, upper left). This process uses the same read_csv()
function within the server.
If you want to learn more about the readr
package or the read_csv()
function, the Tidyverse documentation for readr
provides a more extensive overview of the package and its functions.