Question

I am getting an error, Error in lines[[i]] : subscript out of bounds when trying to knit a chunk that reads in a model from an external file and then fits the model in lavaan.

I create the model in a .R file:

model <- readLines(con = textConnection('
  depression =~ thoughts + pain + brain + use + suic + talk + sitalone +         
                headaches + app + heart + cheek + cry + sleep + disob + cold +
                liedown + worries + alone + annoyed + holdhead + drinkal +
                insult + greet + think + mutter + trust + donoth + sad + bad +
                weak + notalk + forget + crycont + livedie
  '))

cat(model, file = 'scripts/mod.lav.f1.0', sep = '\n')

When running the .R file, it puts the following in the scripts/mod.lav.f1.0 file:

depression =~ thoughts + pain + brain + use + suic + talk + sitalone +         
              headaches + app + heart + cheek + cry + sleep + disob + cold +
              liedown + worries + alone + annoyed + holdhead + drinkal +
              insult + greet + think + mutter + trust + donoth + sad + bad +
              weak + notalk + forget + crycont + livedie

My .rnw file then reads in the scripts/mod.lav.f1.0 that specifies my model.

\documentclass{article}

\begin{document}

<<cfa, include=FALSE, tidy=FALSE>>=
# read in model from file
 mod.1f.0 <- readLines("scripts/mod.lav.f1.0")
# fit the model
 fit.1f.0 <- cfa(mod.1f.0, data = mydata, ordered=items)
@

\end{document}

The problematic statement in the chunk seems to be fit.1f.0 <- cfa(mod.1f.0, data = mydata, ordered=items). When knitting the document, I get an error: Error in lines[[i]] : subscript out of bounds.

I am able to run the chunk in R without any problems. The model is stored in mod.1f.0 and the fit is stored in fit.1f.0.

Any thoughts about what is causing this error?

Was it helpful?

Solution

Someone sent me another example that I was able to run, so I decided to delete my cache and all of the hidden project files that RStudio creates. It worked. Prior to doing this I had only tried restarting RStudio.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top