0

I am trying to call C function provided by R (default installation) for HoltWinters in my own R script, but getting message:

Error in hw(alpha, beta, gamma) : object 'C_HoltWinters' not found

And part of the code in my R script is:

 hw <- function(alpha, beta, gamma)
        .C(C_HoltWinters,
           as.double(x),
           lenx,
           as.double(max(min(alpha, 1), 0)),
           as.double(max(min(beta, 1), 0)),
           as.double(max(min(gamma, 1), 0)),
           as.integer(start.time),
           ## no idea why this is so: same as seasonal != "multiplicative"
           as.integer(! + (seasonal == "multiplicative")),
           as.integer(f),
           as.integer(!is.logical(beta) || beta),
           as.integer(!is.logical(gamma) || gamma),

           a = as.double(l.start),
           b = as.double(b.start),
           s = as.double(s.start),

       ## return values
           SSE = as.double(0),
           level = double(len + 1L),
           trend = double(len + 1L),
           seasonal = double(len + f)
           )

Based on my preliminary research, it seems we need to use dyn.load(dllName);

I have 2 questions now:

1) How can I find out `dll` name for this function? 
2) If I run default `HoltWinters.R` we don't need to load `dll`, then why it is required load `dll` in my custom script case? 
kosa
  • 65,990
  • 13
  • 130
  • 167

1 Answers1

1

The C_HoltWinters object is not exported from the stats namespace. You can reference it using stats:::C_HoltWinters.

Do note that this is against CRAN policy, so it won't be allowed if you put this into a package you plan to submit to CRAN.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • is there a rule for when `:::` is preferred to `::` – rawr Mar 18 '14 at 19:19
  • It worked. Thanks! I am trying to understand R implementation of HoltWinters by going over each line and compare it with hand calculated exercise. I am no way positioned to submit any thing. This is for learning purpose. – kosa Mar 18 '14 at 19:22
  • @rawr: `:::` accesses unexported objects in a namespace. `::` will only access exported objects. – Joshua Ulrich Mar 18 '14 at 19:24
  • Can you help with one more thing? C function is just named as "HoltWinters.C", but in R call we are passing parameter as "C_HoltWinters", how does this work? Thanks! – kosa Mar 18 '14 at 19:24
  • @Nambari: See the "Functions that call compiled code" section of the answer to [How can I view the source code for a function?](http://stackoverflow.com/q/19226816/271616). – Joshua Ulrich Mar 18 '14 at 19:41
  • I can access holt winters with `stats::` and `stats:::` – rawr Mar 18 '14 at 19:43
  • @JoshuaUlrich: Thanks! I didn't fully understand, but as I progress this reference will be useful to get clarity. – kosa Mar 18 '14 at 19:47
  • @rawr: `HoltWinters` is exported. `C_HoltWinters` the (`"NativeSymbolInfo"`) object is not. – Joshua Ulrich Mar 18 '14 at 19:54