Errors generated in Rstudio I'am using Rstudio to create some codes. the cods works pretty well, without any error and gives the output desired. But when I turn on the PC after period and re-execute the same code, it generes errors especially in reading external files like

(Error in names(out) &lt;- named :    'names' attribute [10] must be the
same length as the vector [3]).
Error during wrapup: 'names' attribute [10] must be the same length as
the vector [3]

(these 2 different errors are generated from executing the same part of code 2 times. the code want to tokenize a corpus of 10 files and save it in  tables of 3 columns)
I'am using the same dataset from the same location and I'am sure of calling the correct libraries
library(&quot;tm&quot;)

#seting the corpus
corp &lt;- Corpus(DirSource(path), 
               readerControl=list(reader=readPlain, 
                                  language='en_CA',
                                  load=TRUE));


myTokenizer &lt;- function(x, n, n_min) {
  library(tokenizers)
  tok &lt;- unlist(tokenize_ngrams(x, n = n, n_min = n_min))
  M &lt;- matrix(nrow=length(tok), ncol=3, 
              dimnames=list(NULL, c( &quot;gram&quot; , &quot;num.words&quot;, &quot;words&quot;)))
  M[, 3] &lt;- tok
  M[, 2] &lt;- lengths(strsplit(M[, 3], &quot;\\W+&quot;))  # counts the words
  M[, 1] &lt;- 1:length(tok)
  return(M)
}
  corp&lt;-tm_map( myTokenizer(x=corp, n=6, n_min=1) )
writecorpus(corp)
You need to convert the elements of corpus to character:

myTokenizer &lt;- function(x, n, n_min) {
  tok &lt;- unlist(tokenize_ngrams(as.character(x), n = n, n_min = n_min))
  M &lt;- matrix(nrow=length(tok), ncol=3, 
              dimnames=list(NULL, c( "gram" , "num.words", "words")))
  M[, 3] &lt;- tok
  M[, 2] &lt;- lengths(strsplit(M[, 3], "\\W+"))  # counts the words
  M[, 1] &lt;- 1:length(tok)
  return(M)
}
res &lt;- tm_map(corp, function (x) myTokenizer(x, n=3, n_min=1))


or apply your function directly without using Corpus:

res &lt;- myTokenizer(x = list("A Sample Text ...", "Lorup ipsum x 12"), n=3, n_min=1))