rwinbugsjagsr2winbugs

rJAGS version of write.model()?


The R2WinBUGS package has a function called write.model(). The R package rjags has no such function of which I am aware. write.model creates a temporary text file that can be read as a model by WinBUGS.

I know I can enter write.model into the console to see the function, but this function seems to make calls to function that I've never seen before, and can't search for in help() (e.g., replaceScientificNotationR is apparently a function).

I saw This Post that describes some methods for doing this, but if possible, I'd prefer to not have to use the quotes (just to maintain my syntax highlighting), and a comment suggested that "copying the write.model function" should be doable.

Has anyone done this?


Solution

  • Presumably, you could just load R2WinBUGS to get access to the function.

    However, in general, where there is a function that you can't see the code for, try getAnywhere.

    E.g., getAnywhere(replaceScientificNotationR) produces:

    A single object matching ‘replaceScientificNotationR’ was found
    It was found in the following places
      namespace:R2WinBUGS
    with value
    
    function (bmodel, digits = 5) 
    {
        env <- new.env()
        assign("rSNRidCounter", 0, envir = env)
        replaceID <- function(bmodel, env, digits = 5) {
            for (i in seq_along(bmodel)) {
                if (length(bmodel[[i]]) == 1) {
                    if (as.character(bmodel[[i]]) %in% c(":", "[", 
                      "[[")) 
                      return(bmodel)
                    if ((typeof(bmodel[[i]]) %in% c("double", "integer")) && 
                      ((abs(bmodel[[i]]) < 0.001) || (abs(bmodel[[i]]) > 
                        10000))) {
                      counter <- get("rSNRidCounter", envir = env) + 
                        1
                      assign("rSNRidCounter", counter, envir = env)
                      id <- paste("rSNRid", counter, sep = "")
                      assign(id, formatC(bmodel[[i]], digits = digits, 
                        format = "E"), envir = env)
                      bmodel[[i]] <- id
                    }
                }
                else {
                    bmodel[[i]] <- replaceID(bmodel[[i]], env, digits = digits)
                }
            }
            bmodel
        }
        bmodel <- deparse(replaceID(bmodel, env, digits = digits), 
            control = NULL)
        for (i in ls(env)) {
            bmodel <- gsub(paste("\"", i, "\"", sep = ""), get(i, 
                envir = env), bmodel, fixed = TRUE)
        }
        bmodel
    }
    <environment: namespace:R2WinBUGS>
    

    Thus, it is an internal function in the R2WinBUGS package. Alternatively, you could download the package source from CRAN and explore.