1

I have a string that is built programmatically

 tot=~item1+item2+item3+item4+item5+item6+item7+item8+item9+item10

that I need to wrap in two single quotes (without the double quotes on the string) and assign to an object mod:

# this is what I want
mod <- '
    tot=~item1+item2+item3+item4+item5+item6+item7+item8+item9+item10
'

I've tried:

string <- "tot=~item1+item2+item3+item4+item5+item6+item7+item8+item9+item10"
mod <- as.symbol(paste("'", string, "'", sep=" ")) # to try and remove the double quotes

but mod gets wrapped in backtics

mod
`' tot=~item1+item2+item3+item4+item5+item6+item7+item8+item9+item10 '`

If more background would be helpful, please see a more detailed question I posted to the help group of the package I am using. I am posting a more specific question on SO because I've narrowed the problem to how the mod object is defined.

It does work if I use cat() and readLines():

string <- paste("'", "tot=~item1+item2+item3+item4+item5+item6+item7+item8+item9+item10", "'", sep=" ")
cat(string, file=paste0(string, ".lav"))
mod <- readLines(paste0(string, ".lav"))

but I don't think this should be necessary.

Eric Green
  • 7,385
  • 11
  • 56
  • 102
  • I’m sorry – it’s unclear what you want to accomplish. Your last code is the same as saying `mod <- string`, just very convoluted and error-prone. – Konrad Rudolph Dec 24 '13 at 16:13
  • I want what is shown in the code block with the comment "# this is what I want". The string between the two single quotes is built in a few steps not shown. I want to take that string, wrap it in single quotes, and assign it to an object called `mod`. the code block with the "this is what I want" comment is showing lavaan syntax: http://lavaan.ugent.be/tutorial/syntax1.html – Eric Green Dec 24 '13 at 16:16
  • 2
    Like I said – that’s unclear. What you’ve shown is – once again, what you get when you say `mod <- string` (except that it has additional line breaks inside the string). There’s no difference between a single- and double-quoted string: `"foo" == 'foo'`. I don’t know why the lavaan documentation keeps insisting on single quotes. They’re wrong, it doesn’t matter. – Konrad Rudolph Dec 24 '13 at 16:18
  • 2
    `lavaan` does in fact want strings. I think @EricGreen may be confused by this line in the `lavaan` documentation: `A complete lavaan model syntax is simply a combination of these formula types, enclosed between single quotes.` Eric, I think Konrad is right and that you shouldn't actually need single quotes (it would only matter if you were trying to create a string with embedded double quotes). – Ben Bolker Dec 24 '13 at 16:21
  • @BenBolker, you're right. i was working hard to get single quotes as instructed, but it seems double quotes are ok. – Eric Green Dec 24 '13 at 16:26
  • It might be worth sending the `lavaan` maintainers an e-mail letting them know that this issue can be confusing ... – Ben Bolker Dec 24 '13 at 17:44
  • In R paired single quotes and paired double quote produce the same internal values. Furthermore there are neither double, not single quotes in the resulting value, so the idea of replacing double quotes with single quotes at the ends of character values is really a fantastical notion (fostered by the screen representations of the values but having no "reality"). – IRTFM Dec 24 '13 at 18:00

2 Answers2

2

Generate data:

set.seed(101)
dat.test <- do.call(data.frame,replicate(6,sample(1:10, 50, replace=TRUE),
                                    simplify=FALSE))
names(dat.test) <- paste0("s1.item",1:6)

myList1 <- list(scale1.tot=paste0("s1.item",1:6),
                scale1.sub1=paste0("s1.item",1:3),
                scale1.sub2=paste0("s1.item",4:6))

I only made one item here, but you could apply across a set of these too, with another *apply call or a for loop ...

This works (although it gives warnings because the data are trivial)

fitList <- mapply(function(lhs,rhs) {
           mod <- paste(lhs,"=~",paste(rhs,collapse="+"))
           cfa(mod,data=dat.test)
       },
       names(myList),myList)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
1

I think you are misunderstanding R string syntax. The string you described is:

# this is what I want
mod <- '
    tot=~item1+item2+item3+item4+item5+item6+item7+item8+item9+item10
'

That is exactly the same as writing:

mod <- '\ntot=~item1+item2+item3+item4+item5+item6+item7+item8+item9+item10\n'

or

mod <- "\ntot=~item1+item2+item3+item4+item5+item6+item7+item8+item9+item10\n"

Therefore, for your problem I think it would be sufficient to run:

string <- "tot=~item1+item2+item3+item4+item5+item6+item7+item8+item9+item10"
mod <- paste0("\n", string, "\n")

If you want to output your final model without the enclosing quotes, you can do:

cat(mod)
# 
# tot=~item1+item2+item3+item4+item5+item6+item7+item8+item9+item10
josliber
  • 43,891
  • 12
  • 98
  • 133
  • 1
    Actually, according to [a discussion in their mailing list](https://groups.google.com/forum/#!msg/lavaan/33ivgpMFkBs/SaJOj00ydCQJ), “the '\n' characters are not needed in the model syntax” – Konrad Rudolph Dec 24 '13 at 16:25
  • 2
    @IShouldBuyABoat No, he’s not. – Konrad Rudolph Dec 24 '13 at 16:26
  • But `string` (with backticks) is not the same as 'string'. – IRTFM Dec 24 '13 at 16:32
  • thanks, @josilber. i will accept this answer because it addresses my original question and has some nice tips. but just in case folks get to this page trying to search for a lavaan solution, know that @BenBolker's comment is right. you do not need single quotes. you can assign `string` to `mod` without any modification. – Eric Green Dec 24 '13 at 16:44