Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

R Packages

Tags: code

R Packages

Introduction to R Packages

The Package is a collection of functions and datasets. Packages help to enhance the functionality in a programming language. If we want to store and process databases (i.e. SQL like functionalities using data frames) then we can use dplyr package in R. Packages also provide documentation on how to use the functions and datasets in a given package.

Where do we Find Packages?

Packages are available on the internet through different sources. However, there are certain trusted repositories from where we can download the packages.

Here are the two important repositories that are available online.

  • CRAN(Comprehensive R Archive Network): This is the official R community with a network of FTP and webservers that contains the latest code and documentation of R. Before, you post your packages online it goes through a series of tests which adheres to CRAN policy.
  • GitHub: GitHub is another famous repository but not specific to R.The online community can share their packages with other people and it is used for version control is well. GitHub is an open-source and doesn’t have any review process.

List of useful R Packages

There are several packages in R and can be downloaded from CRAN or GitHub. Below are the packages that can be used for specific purposes.

1. Loading the Data from External Sources

  • Haven: R reads and writes data from SAS.
  • DBI: To establish communication between the relational database and R.
  • RSQlite: It is used to read data from relational databases.

2. Data Manipulation

  • Dplyr: It is used for data manipulation like subsetting, provides shortcuts to access data and generates sql queries.
  • Tidyr – It is used to convert data into tiny formats.
  • stringr– manipulate string expressions and character strings.
  • lubridate- To work with data and time.

3. Data Visualization

  • Rgl: To work on 3D visualizations.
  • ggvis: To create and build grammar of graphics.
  • googlevis: To use google visualization tools in R.

4. Web-Based Packages

  1. XML: To read and write XML documents in R.
  2. Httpr: Work with http connections.
  3. Jsonlite: To read json data tables.

Obtaining R Packages

We can check the available packages that are present in R by using the below Code.

  • available.packages(): There are approximately 5200 packages available in the CRAN network.

CRAN has task views which groups packages under a particular topic.

Installing R Packages

We can install packages directly through IDE or through commands. To install packages we use the below function and specify the package name.

Syntax:

install.packages() 

Code:

install.packages(“ggplot2”)

The above code installs the ggplot2 package and its dependent packages if any.

We can install several packages at a time by specifying the package’s names under a character vector.

Syntax:

install.packages(c(“package 1”,”package 2”,”package 3”))

Code:

install.packages(c(“ggplot2”,”slidify”,”deplyr”))

Installing using R Studio

The advantage of using an R studio is it is GUI (Graphical User interface). We can choose the packages to install and the source of it.

We can go to tools -> Install packages.

Loading R Packages

After installing the R package we need to load them into R, to start making use of the installed packages.

We use the below function to load the packages.

Syntax:

library(package name)

Note: The package name need not be given in quotes.

Code:

library(ggplot2)

There are certain packages that display messages when loaded. Some of them, don’t. We can see the details of the library installed with the help of the below code.

Code:

library(ggplot2)
search()

Output:

“package:lattice”    “package:ggplot2”    “package:makeslides”

“package:knitr”      “package:slidify”    “tools:rstudio”

Creating Your own Package

Before we create our own package. We should keep the below checklist in our mind before we proceed to create a package.

  • Organizing the code is one of the most important things while writing code in the package. We lose half the time searching for the code location instead of improving the code. Put all the files in a folder that is easily accessible.
  • Documenting the code helps you understand the purpose of code. When we don’t revisit the code often, we forget why we have written the code in a certain way. It can also help people to understand your code better when shared with them.
  • Sharing the scripts through email has become archaic. The easy way is to upload your code and distribute it on GitHub. It is possible you get feedback that can help you enhance the code.

To create your own package, we have to install the devtools package.

Code:

install.packages("devtools")

To help with the documentation we can use the below package.

Code:

install.packages("roxygen2")

After installing the package devtools. You can create your own package.

Code:

devtools::create ("packagename")

In the place of “packagename”, you can give the name you wish. You can now add your functions under this package.

You can create the same filename as your function name.

Syntax:

Devtools:create(“firstpackage”)

Distributing Package

You can distribute your package on github by using the devtools package.

We use the below code to distribute our package on github.

Code:

devtools::install_github("yourusername/firstpackage")

You can give your github username and package name you have created above.

Here are the Required Files for a Package

  • Functions
  • Documentation
  • Data

Once we have all the above files we are good to post it in the repository.

Recommended Articles

This is a guide to R Packages. Here we discuss the list of useful R packages, installing packages using R studio and creating your own package, etc. You may also look at the following articles to learn more –

  1. What is R Programming Language?
  2. Careers in R Programming
  3. R Programming vs Python
  4. MySQL vs SQLite

The post R Packages appeared first on EDUCBA.



This post first appeared on Best Online Training & Video Courses | EduCBA, please read the originial post: here

Subscribe to Best Online Training & Video Courses | Educba

Get updates delivered right to your inbox!

Thank you for your subscription

×