0

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
jay.sf
  • 60,139
  • 8
  • 53
  • 110
sam
  • 31
  • 1
  • 4
  • 2
    I think `select(“percent”)` should be `select(percent)`. – Limey Sep 04 '21 at 04:43
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Were does the `tabyl` function come from? It doesn't look right to see `t[[i]]` and then just to see `t$Gender`. Do you have two different `t` variables? I'm surprised this works at all. – MrFlick Sep 04 '21 at 05:15
  • Ok thanks for the comments. I've made a simplified example that again, works in normal script but not in markdown. – sam Sep 04 '21 at 05:56
  • 2
    You are assigning to `t[[i]]` before you've defined `t`. And by default `t()` is the built in function for matrix transposition. You should initialize your list before the loop: `t <- list()`. That will shadow the `t` function., – MrFlick Sep 04 '21 at 07:58

0 Answers0