Quick guide to opening files from within R

R has loads of useful, but little known functions that can make life a little bit easier. As part of the "quick guide" series, this quick guide takes a look at opening files from within R - no more opening up file explorer and hunting around for that file!


© 2018-2019 Benjamin Bell. All Rights Reserved. https://www.benjaminbell.co.uk

Opening files from within R

! This guide was written using R version 3.5.1 on Windows 10.

R has built in functions which allow you to interact with the host operating system, extending the functionality of R, by allowing you to execute any system command from the R console. One application of this, is opening files from the R console.

Why might this be useful you may be wondering? Well, lets say you have just created a file in R (such as a saving plot as a pdf, or a writing a csv file of your data frame) and you want to check that the file was created properly, or you want to see how your plot turned out.

After writing the file, you might typically fire up explorer (or finder, or files) to hunt down your creation to open it up. But this involves several mouse clicks and takes time! Instead, you could just write a simple line of code.

The way you do this depends on your operating system, and for these examples, we'll create a plot, save it as a pdf, then open it directly from the R console.

# Create random data plot
x <- rnorm(30) 
y <- rnorm(30)
# Save plot as pdf
pdf("plot1.pdf")
plot(x, y)
dev.off()

This code will create a pdf containing the plot called "plot1.pdf", which will be saved the current R working directory.

To open the file from R, check out the relevant section for your operating system.

Windows

To open the plot file on Windows systems, you need to use the shell function, and specify the file name. For example:

shell("plot1.pdf", wait=FALSE)

The additional argument wait=FALSE tells R not to wait for you to interact with the R console - i.e. it will carry on with whatever it is doing. If you omit this argument (the default value is TRUE), R will stop executing commands until you interact with the console.

The code above assumes the file you want to open is located within the current R working directory. If the file is located elsewhere, you need to tell R where to find it.

For example, if you wanted to open a csv file named "data.csv" which was in your documents folder, you might use the following code (change the file location for your system):

shell("C:/Users/ben/Documents/data.csv", wait=FALSE)

Remember that you need to use forward slashes instead of back slashes for Windows file locations in R.

MacOS

To open the plot file on MacOS systems, you need to use the system function, and specify the command and the file name. For example:

system("open plot1.pdf", wait=FALSE)

On MacOS, the command to open a file is "open", which will open the specified file using the default program for that file type.

If you wanted to open the file in a different program, you can specify this in the command. For example, to open the pdf in Safari, use the following code:

system("open -a Safari plot1.pdf", wait=FALSE)

The code above assumes the file you want to open is located within the current R working directory. If the file is located elsewhere, you need to tell R where to find it.

For example, if you wanted to open a csv file named "data.csv" which was on your Desktop, you might use the following code:

system("open ~/Desktop/data.csv", wait=FALSE)

Linux

To open the plot file on Linux systems, you need to use the system function, and specify the command and the file name. For example:

system("xdg-open plot1.pdf", wait=FALSE)

On most Linux distributions the universal command for opening a file is "xdg-open", which will open the specified file using the default program for that file type. Check your system documentation if this command does not work.

If you wanted to open the file in a different program, you can specify this in the command. For example, to open the pdf in LibreOffice, use the following code:

system("libreoffice plot1.pdf", wait=FALSE)

The code above assumes the file you want to open is located within the current R working directory. If the file is located elsewhere, you need to tell R where to find it.

For example, if you wanted to open a csv file named "data.csv" which was in your Documents directory, you might use the following code (change the file location for your system):

system("xdg-open /home/ben/Documents/data.csv", wait=FALSE)


Simple eh? Opening files in R just became much easier!

Of course the shell and system functions can be used for other system commands - and you should check the help files for these functions if you want to learn more.

Thanks for reading, please leave any commends or feedback below.


Ad

© 2018-2019 Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

Further reading

A quick guide to line types (lty) - A quick guide to the different line types available in R, and how to use them. [R Graphics]

A quick guide to layout() in R - How to create multi-panel plots and figures using the layout() function. [R Graphics]


No comments:

Post a Comment