I get a strange error when knitting
this R Markdown into an HTML file. I think it has to do with some sort of incompatibility in the dplyr
package with knitr
.
UPDATE: I replaced the cbind
chunk with the dplyr::bind_cols
command, as someone suggested below not to use cbind
with dplyr
. However, I now get a different, equally incomprehensible error:
library(dplyr)
counts.all <- bind_cols(count.tables[["SF10281"]], count.tables[["SF10282"]])
The error I get with this change (again, only when knitting):
Error in eval(expr, envir, enclos) : not compatible with STRSXP
Calls: <Anonymous> ... withVisible -> eval -> eval -> bind_cols -> cbind_all -> .Call
Previous error with cbind
instead of dplyr::bind_cols
:
Running the chunks separately works fine, and I was able to knit
fine until I added the last chunk (using select
from dplyr
).
This is the error I get:
Quitting from lines 75-77 (Analysis_SF10281_SF10282_Sep29_2015.Rmd)
Error in UseMethod("select_") :
no applicable method for 'select_' applied to an object of class "NULL"
Calls: <Anonymous> ... withVisible -> eval -> eval -> <Anonymous> -> select_
This is the entire Rmd file:
Read-in gene count tables into a single list of data frames (one data frame per sample):
```{r}
count.files <- list.files(pattern = "^SF[0-9]+_counts.txt$")
count.tables <- lapply(count.files, read.table, header=T, row.names=1)
names(count.tables) <- gsub("\\_counts.txt", "", count.files)
```
Remove gene metadata columns:
```{r}
count.tables <- lapply(count.tables, `[`, -(1:5))
```
Rename cells (columns) to short version:
```{r}
count.tables <- lapply(count.tables, function(x) {names(x) <- gsub("X.diazlab.aaron.tophat_out.SF[0-9]+.Sample_(SF[0-9]+).[0-9]+.([A-Z][0-9]+).accepted_hits.bam", "\\1-\\2", names(x)); x})
```
Save object to file for later:
{r}
saveRDS(count.tables, file="gliomaRawCounts_10281_10282_10345_10360.rds")
Make a single data frame with all 4 samples (384 cells), and write to text file:
```{r}
counts.all <- cbind(count.tables[["SF10281"]], count.tables[["SF10282"]], count.tables[["SF10345"]], count.tables[["SF10360"]])
write.table(counts.all, file="gliomaRawCounts_10281_10282_10345_10360.txt", sep="\t", quote=F, col.names=NA)
```
Read metadata. Do not assign cell ID column as row.names, for compatibility with dplyr.
```{r}
meta <- read.delim("QC_metrics_SCell_SF10281_SF10282_SF10345_SF10360.txt", check.names = F, stringsAsFactors = F)
```
Filter cells based on live/dead/multi calls. Exclude empty, red-only, and multi-cell wells:
```{r, results='hide', message=FALSE, warning=FALSE}
library(dplyr)
meta.select <- filter(meta, grepl("^1g", `Live-dead_call`))
```
Filter cells based on 1,000 gene threshold:
(Includes 12 'FAIL' cells)
```{r}
meta.select <- filter(meta.select, Genes_tagged > 1000)
```
Subset counts table to include only cells that passed QC.
```{r}
counts.select <- dplyr::select(counts.all, one_of(meta.select$ID))
head(counts.select[,1:10])
```