This should be quick to answer:
I want to use jags packages' jags.model
function. Among many arguments it needs the argument file
:
file
- the name of the file containing a description of the model in the JAGS dialect of the BUGS language.Alternatively, file can be a readable text-mode connection, or a complete URL.
I want to pass string to this argument. One way to walk-around this problem is to write the string into temporary file and pass the file name. But I hope there is a straightforward way to convert string into connection.
You can use textConnection
like this (here with the read.csv
function, but I guess jags.model
should work the same way) :
mytext <- "x,y\n1,2\n3,4"
read.csv(textConnection(mytext))
# x y
# 1 1 2
# 2 3 4
I would just add a small point about textConnection
. I have occasionally ran into problems when I used jags.model(textConnection(x)...)
inside a large for loop (i.e., more than 125 iterations or so). Essentially, it seems that the textConnections were being opened, but not closed, and then I would receive an error eventually after too may textConnections had been opened stating that all connections are in use
.
I'm not entirely sure of the best solution, but I assume you could do something like:
zz <- textConnection(x)
jags.model(zz, ...)
close(zz)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With