-2

The variables from inside the function are not getting stored in the global variables in R programming. Look at the code snippet:

install.packages('HMM')
library('HMM')

hmm_source <- function(){

  lamba_1 <- initHMM(c("s1","s2"), c("a","b","c"), c(1,0), matrix(c(.1,.6,.9,.4),nrow = 2,ncol = 2),matrix(c(.1,.4,.3,.2,.6,.4),nrow = 2,ncol = 3))

  lamba_2 <- initHMM(c("s1","s2"), c("a","b","c"), c(1,0), matrix(c(.4,.8,.6,.2),nrow = 2,ncol = 2),matrix(c(.5,.2,.4,.1,.1,.7),nrow = 2,ncol = 3))

  return(list(m1=lamba_1,m2=lamba_2))
}
source1_2 <- hmm_source()install.packages('HMM')
library('HMM')

hmm_source <- function(){

  lamba_1 <- initHMM(c("s1","s2"), c("a","b","c"), c(1,0), matrix(c(.1,.6,.9,.4),nrow = 2,ncol = 2),matrix(c(.1,.4,.3,.2,.6,.4),nrow = 2,ncol = 3))

  lamba_2 <- initHMM(c("s1","s2"), c("a","b","c"), c(1,0), matrix(c(.4,.8,.6,.2),nrow = 2,ncol = 2),matrix(c(.5,.2,.4,.1,.1,.7),nrow = 2,ncol = 3))

  return(list(m1=lamba_1,m2=lamba_2))
}
source1_2 <- hmm_source()

here my function returns the two hmm models but the variables lamba_1 and lamba_2 are not getting saved in global environment in R. I am working in RStudio, though i tried running the code in R shell too.. But it always give me the error : object lamba_1 not found. Any help ?

nrussell
  • 18,382
  • 4
  • 47
  • 60
  • 1
    Objects are stored as you assign them. So when you do `source1_2 <- hmm_source()`, your result is a `list` named with the name you gave it: `source1_2` – Gregor Thomas Feb 23 '17 at 20:53
  • 1
    Use `<<-` to store objects inside functions in the global enviroment. – count Feb 23 '17 at 20:53
  • 4
    they are getting saved as `source1_2$m1` and `source1_2$m2`. I would not advise using the `<<-` way of assigning them - otherwise you will overwrite variables in your GlobalEnv. I don't think this is a good use case for side effects in your function – Chris Feb 23 '17 at 20:57
  • 6
    @count That’s moderately terrible advice … – Konrad Rudolph Feb 23 '17 at 20:57
  • @ Konrad Rudolph true, using `assign` might be better. – count Feb 23 '17 at 20:58
  • 4
    @count that's not what they meant - don't do this in general – Chris Feb 23 '17 at 20:59
  • 1
    @Chris I also wouldn`t recommend doing that but isn´t that what op was asking for? – count Feb 23 '17 at 21:00
  • It is generally frowned upon to write to a different environment from a function. Just output the data you want (in a list or some other handy structure - R's got plenty of them!) and process them once outside the function. This way you can make your function(s) more predictable and thus safe. – Roman Luštrik Feb 23 '17 at 23:26

1 Answers1

0

Objects created within functions are not stored to the global environment (by default, anyway). If you're returning lambda_1 and lambda_2, they will be elements of the returned object. They won't exist separately, but from the way you call the function, they should exist as source1_2$m1 and source1_2$m2.

Matt Tyers
  • 2,125
  • 1
  • 14
  • 23