October 19, 2021

RMarkdown: new file template

This is the template I use to make my RMarkdown files. This adds the date run, a table of contents, and the session information (R packages loaded and their versions, operating system, etc). The indentation in the title section must be actual tabs, not individual spaces, or you'll get an error. The back quote character ` is from the [~] key, not the ["] key. My filenames always include the date also! See this tutorial for other ways to edit formatting.


---
title: "My title"
author: "Tania L Gonzalez, PhD"
date: "`r Sys.Date()`"
output: 
  html_document:
    toc: true
    toc_float: true
    toc_depth: 3
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## About
Some information about the project, including what data you are using and what you are doing. Use two spaces after lines where you want a linebreak.  

Keep chronological notes about the project below.  

**Initials and Date**: update #1  

**Initials and Date**: update #2  

**bold** 

*italics*


## Packages
```{r packages, eval=TRUE, results='hold'}
#install.packages(c("tidyr","ggplot2"))
library(tidyr)
library(ggplot2)

```


## Output directory
```{r where-output, eval=TRUE}
outDir = "fig_2021-10/"  ## output directory
if (!dir.exists(outDir)) {dir.create(outDir)}  ## creates directory if it doesn't exist
```


## Import data
```{r eval=TRUE}
df = read.csv(file="", header=TRUE, stringsAsFactors=FALSE)

df[1:10,1:5] ## look at data

colnames(df)  ## column names
rownames(df)[1:10]  ## check first 10 rows

dim(df) ## number rows columns
```


## Adjust data
```{r eval=TRUE}
   #rename columns or whatever

```


## Subset data
Data filtering notes mm/dd/yyyy:  
```{r eval=TRUE}
## example subset requires condition1 AND EITHER condition2 or condition3:
proteins = subset(df, biotype=="protein_coding" & 
     (meanTPM.First>0.66 | meanTPM.Third>0.66))

```



## Check data
```{r eval=TRUE}


```



## Count for manuscript
```{r eval=TRUE, results="hold"}

cat("Table of biotypes:\n\n")
table(df$biotype, useNA="always")

```



## Figure

### Quick plot
Just make a quick plot to see what the data looks like, if I need to log-transform, etc.
```{r eval=TRUE}
plot(df$columnA, df$columnB)  
```


### Plot function
```{r eval=TRUE, showLineNumbers}
scatterplot = function(data) {
   plot = ggplot(data, aes(x=columnA, y=columnB, fill=columnC)) +
             geom_point(shape=21, alpha=0.5)

   return(plot)         
}
```



### Call function and save figure
```{r eval=TRUE, dpi=72}
fn = paste0(outDir, "Plot_",Sys.Date(),".png"); fn
scatterplot(data=proteins)
ggsave(filename = fn, width=3, height=3, dpi=300, scale=1)
file.show(fn)  ## open a window with your image file
rm(fn)  ## delete the filename so you can recycle the variable 
```


## Session info  
Print out your operating system, the date, the R version, and all loaded packages and their version information. These logs are important for reproducibility and for writing methods.     
```{r session, eval=TRUE}
sessionInfo()
system('cat /etc/os-release')  ## additional operating system info
capabilities() ## graphics information
Sys.Date()
```

No comments:

Post a Comment

Bookmarks: single cell RNA-seq tutorials and tools

These are my bookmarks for single cell transcriptomics resources and tutorials. scRNA-seq introductions How to make R obj...