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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top