Here is my code that is causing the error. Note that this code works perfectly in a normal R document and the chunk runs in R markdown, but for some reason when I try to knit I get an error saying that the object of closure is not subsettable. Specifically the error says:
Error in t[[i]] <- cancer %>% tabyl(i) %>% select("percent") : object of type 'closure' is not subsettable etc..
I have all the packages loaded. Any help would be really helpful!
Code
library(tidyverse)
library(janitor)
id <- c("001", "002", "003")
Gender <- c(0,1,1)
Insurance <- c(1,0,0)
Employment <- c(0,1,0)
Stroke <- c(1,1,0)
sample <- tibble(id, Gender, Insurance, Employment, Stroke)
sample
vbles <- c("Gender", "Insurance", "Employment", "Stroke")
for (i in vbles){
t[[i]] <- sample %>% tabyl(i) %>% select("percent")}
test <- matrix(c(t$Gender[1,], t$Insurance[1,], t$Employment[1,], t$Stroke[1,],t$Gender[2,], t$Insurance[2,], t$Employment[2,], t$Stroke[2,]), ncol=2, byrow=FALSE)
rownames(test) <- c("Gender", "Insurance", "Employment", "Stroke")
colnames(test) <- c(0,1)
test <- as.table(test)
test