Create a new RStudio project

Open RStudio and create a new project, for more info see https://support.rstudio.com/hc/en-us/articles/200526207-Using-Projects

Packrat is a dependency management tool that makes R code more isolated, portable and reproducible by giving the project its own privately managed package library. Learn more about packrat, please see https://rstudio.github.io/packrat/

Set some options and make sure the packages ‘knitr’, ‘tidyverse’, ‘reshape2’, and ‘gridExtra’ are installed (if not install it), and then load

In the R console run the following commands

if (!requireNamespace("knitr")){
  install.packages("knitr")
}
library(knitr)

if (!requireNamespace("tidyverse")){
  install.packages("tidyverse")
}
library(tidyverse)

if (!requireNamespace("reshape2")){
  install.packages("reshape2")
}
library(reshape2)

if (!requireNamespace("gridExtra")){
  install.packages("gridExtra")
}
library(gridExtra)

Learn more about the tidyverse see https://www.tidyverse.org.

Open a new R Notebook

An R notebook is an R Markdown document with chunks that can be executed independently and interactively, with output visible immediately beneath the input. More info see https://rmarkdown.rstudio.com/r_notebooks.html

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the preview or Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed R code and plots in chunks like this:

```{r chunk_name}
print('hello world!')
```

Review the R Markdown page and R Markdown cheat sheets.

Try ‘knitting’ to html, pdf, and doc as well as previewing the notebook. Open the resulting documents.

Try executing the code chunks in the R Notebook.

Download the data file for the workshop document and preview/open it

This is the stats file generated after running samtools stats on a bam file generated from running BWA MEM.

In the R console run the following command.

download.file("https://raw.githubusercontent.com/ucdavis-bioinformatics-training/2019-March-Bioinformatics-Prerequisites/master/wednesday/Data_in_R/bwa.samtools.stats", "bwa.samtools.stats")

Download the template Markdown workshop document and open it

In the R console run the following command

download.file("https://raw.githubusercontent.com/ucdavis-bioinformatics-training/2019-March-Bioinformatics-Prerequisites/master/wednesday/Data_in_R/data_in_R.Rmd", "data_in_R.Rmd")

Edit the file YAML portion

The top YAML (YAML ain’t markup language) portion of the doc tells RStudio how to parse the document.

---
title: "Data_in_R"
author: your_name
date: current_date
output:
    html_notebook: default
    html_document: default
---

What are we going to do?

We will recreate some of the plots generated with plot-bamstats on the same file

You can view the output of plot-bamstats -> http://htmlpreview.github.io/?https://github.com/ucdavis-bioinformatics-training/2019-March-Bioinformatics-Prerequisites/master/wednesday/Data_in_R/bwa_mem_Stats/bwa_mem_Stats.html

LS0tCnRpdGxlOiAiUHJlcGFyZSBEYXRhX2luX1IiCmF1dGhvcjogIkJpb2luZm9ybWF0aWNzIENvcmUiCmRhdGU6ICJgciBmb3JtYXQoU3lzLkRhdGUoKSlgIgpvdXRwdXQ6CiAgICBodG1sX25vdGVib29rOiAKICAgIGh0bWxfZG9jdW1lbnQ6CiAgICAgIGtlZXBfbWQ6IFRSVUUKLS0tCgojIyMgQ3JlYXRlIGEgbmV3IFJTdHVkaW8gcHJvamVjdAoKT3BlbiBSU3R1ZGlvIGFuZCBjcmVhdGUgYSBuZXcgcHJvamVjdCwgZm9yIG1vcmUgaW5mbyBzZWUgPGh0dHBzOi8vc3VwcG9ydC5yc3R1ZGlvLmNvbS9oYy9lbi11cy9hcnRpY2xlcy8yMDA1MjYyMDctVXNpbmctUHJvamVjdHM+CgoqIEZpbGUgPiBOZXcgUHJvamVjdCA+IE5ldyBEaXJlY3RvcnkgPiBOZXcgUHJvamVjdCAobmFtZSB0aGUgbmV3IGRpcmVjdG9yeSwgRXguIERhdGFfaW5fUikgYW5kIGNoZWNrICJ1c2UgcGFja3JhdCB3aXRoIHRoaXMgcHJvamVjdCIgaWYgcHJlc2VudC4KClBhY2tyYXQgaXMgYSBkZXBlbmRlbmN5IG1hbmFnZW1lbnQgdG9vbCB0aGF0IG1ha2VzIFIgY29kZSBtb3JlIGlzb2xhdGVkLCBwb3J0YWJsZSBhbmQgcmVwcm9kdWNpYmxlIGJ5IGdpdmluZyB0aGUgcHJvamVjdCBpdHMgb3duIHByaXZhdGVseSBtYW5hZ2VkIHBhY2thZ2UgbGlicmFyeS4gTGVhcm4gbW9yZSBhYm91d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