TL;DR

Our previous tutorial have shown you how to process your data or create plots from your data. This post will cover how to export data frames and plots from the RStudio environment.

Introduction

Once you have got your head around assigning objects to the RStudio environment, you might want to export the file as a post processed data frame or as a record of the results of your processing. Additionally, you may want to export any generated plots to be used outside of RStudio for reports or publications. Luckily, this is quite easy to do with a few simple lines of code.

Exporting data frames

To demonstrate how to export a data frame from RStudio we will first enter some data that we will assign to an object. We have decided to enter the data via the console rather than a file on this occasion but the result will be the same.

data <- data.frame(id = c(1,2,3,4),
                   age = c(23, 24, 25, 26),
                   height = c(1.69, 1.58, 1.53, 1.66),
                   weight = c(87.3, 68.4, 57.7, 74.8))
#this code enters the data into RStudio via the console

The data has been assigned to the object called data and so lets replicate some data analyses by calculating the bmi of each of the individuals.

library(dplyr)
data <- data %>%
  mutate(bmi = weight / (height^2))
#from the data we have calculated a bmi value from the weight and height variables
#this requires the dplyr package for the pipes

The data frame data now contains processed information that you might want to export to use later on. To do this you use the write.csv command.

write.csv(data, "data.csv", row.names = FALSE)
#write.csv exports a csv file
#you can set the export directory by including it within the file name
#you first define the name of the object you want to save then the file name you would like to save to
#if you don't include row.names = false then your file will include a variable with a unique row indicator

The above can export the file to a .csv and if you wanted to export to a .xlsx file you can use the xlsx package, but exporting data as a .csv is probably the best way to go. What we haven’t done in the above code it define the output directory which you could do by putting the file path before the “data.csv” portion. If you don’t define the path then it saves it to the working directory you have defined.

Saving plots

Saving data frames is one way of exporting data from RStudio, but if you have created a plot you would like to reuse, you could use the export function within the RStudio graphical user interface, but the code version is just as easy.

So, lets create a plot that we can export using the data frame we created earlier.

library(ggplot2)
#this code creates a simple plot in ggplot2 with a regression line
#we haven't styled this but revisit the visualise section for more help
ggplot(data, aes(x = height, y = weight)) +
  geom_point() +
  geom_smooth(method = "lm")

The above plot is simple but the code for exporting is just the same for more complex plots.

ggsave("plot.jpeg", plot = last_plot(), width = 40, height = 40, units = "cm", dpi = 300)
#you file will save to wherever your working directory is and this should have been set by starting a new project
#the file name needs to be stated after the opening bracket and you can choose to save as a eps, pdf, jpeg, png etc by changing the suffix
#this command saves the last plot by default but if you have assigned the plot to an object, you can specify this here
#width, height and units specifies the size of the plot
#dpi sets the plot resolution 

There are other options such as setting the path to save to, scaling the plot and background colour, but you can find these out using the ggplot2 manual.

Conclusion

This post has quickly run through how to export data frames and plots from RStudio. This is particularly useful if you have processed your data using a particular package and you would like to export for back up purposes or even if you want to export plots for publication. Like everything in R, there are extensions to what we have just described such as developing panel plots, and we aim to cover in later tutorials. But if there is there is something you would like us to cover then please let us know via the contact page.

Complete code

data <- data.frame(id = c(1,2,3,4),
                   age = c(23, 24, 25, 26),
                   height = c(1.69, 1.58, 1.53, 1.66),
                   weight = c(87.3, 68.4, 57.7, 74.8))
#this code enters the data into RStudio via the console

library(dplyr)
data <- data %>%
  mutate(bmi = weight / (height^2))
#from the data we have calculated a bmi value from the weight and height variables
#this requires the dplyr package for the pipes

write.csv(data, "data.csv", row.names = FALSE)
#write.csv exports a csv file
#you can set the export directory by including it within the file name
#you first define the name of the object you want to save then the file name you would like to save to
#if you don't include row.names = false then your file will include a variable with a unique row indicator

library(ggplot2)
ggplot(data, aes(x = height, y = weight)) +
  geom_point() +
  geom_smooth(method = "lm")
#the above code creates a simple plot in ggplot2 with a regression line
#we haven't styled this but revisit the visualise section for more help

ggsave("plot.jpeg", plot = last_plot(), width = 40, height = 40, units = "cm", dpi = 300)
#you file will save to wherever your working directory is and this should have been set by starting a new project
#the file name needs to be stated after the opening bracket and you can choose to save as a eps, pdf, jpeg, png etc by changing the suffix
#this command saves the last plot by default but if you have assigned the plot to an object, you can specify this here
#width, height and units specifies the size of the plot
#dpi sets the plot resolution