How to debug a "hidden" function in an R package? - r

can someone please help me understand this:
I encountered an error when calling a function from a library, specifically "steinertree" from the "SteinerNet" package. When stepping into the function with debug(steinertree), I see that the error occurs, when the function in turn calls "steinertree3". When I try debug(steinertree3), I get "object 'steinertree3' not found". Similarly, I can get the code for 'steinertree' by typing it in the terminal, but not for 'steinertree3'.
So it seems to me that there are some "higher-level" functions and "hidden" functions in packages. I did eventually find the error by finding a file "steinertree.R" in the package at CRAN, which contains both 'steinertree' and 'steinertree3', but I`m wondering how to properly go about debugging such "hidden" functions.
Here is a simple example:
library(igraph)
library(SteinerNet)
set.seed(1)
g= erdos.renyi.game(n=10,p.or.m=0.2)
plot(g)
steinertree(type= 'KB', terminals= c(1,3), graph= g)
Thank you!

Use triple colon ::: to execute a function that is not exported by the package/namespace:
package:::hidden_function()

Related

Parsing error in MonteCarlo::MonteCarlo function in R

I am trying to run a power analysis using a MonteCarlo approach in R.
I have created a function of two parameters that does output a boolean (tested manually for all relevant values of the parameters). I also have run baby-examples of the MonteCarlo function to make sure that I understand it and that it works well.
Yet when I try to run the real thing, I get the following error message:
Error in parse(text = all_funcs_found[i]) : <text>:1:1: unexpected '::'
1: ::
I read through the source code of the MonteCarlo function (which I found here) and found
#loop through non-primitive functions used in func and check from which package they are
for(i in 1:length(all_funcs_found)){
if(environmentName(environment(eval(parse(text=all_funcs_found[i]))))%in%env_names){
packages<-c(packages,env_names[which(env_names==environmentName(environment(eval(parse(text=all_funcs_found[i])))))])
}
}
which doesn't really make sense to me - why should there be a problem there?
Thank you for any ideas.
I found the answer: the function I wrote was calling a function from a specific library in the form libraryname::functionname.
This works OK if you use the function once manually, but makes MonteCarlo break.
I solved the problem by first loading the relevant library, then removing the 'libraryname::' part from the definition of the main function. MonteCarlo then runs just fine.

Function not working anymore when executed from own package

Hope you can give me a quick help:
I have a function that works perfect as run in the global envir. When I eval the function from my own package it gives me an error. I can't figure out what the problem is. Seem so random to me.
catTableFun(aaa,grouped_by = "varNameAsString",preview=T) #from global envir works as expected
elricoFuns::catTableFun(aaa,grouped_by = "varNameAsString",preview=T) # identical function run from package gives the following error
Error:
#Error in get(grouped_by) : object 'varNameAsString' not found
Its such a construct causing the error. But why?
setDT(x)
gnLevels <- x[,get(grouped_by),drop=F]
I'm sorry I cant give you some rep example and this is a broad question, but I hope someone has encountered a similar phenomenon or has an educated guess about it.

Where did the forecast.Holtwinters go in R 3.4.3?

I'm using R Studio based on R 3.4.3. However, when I tried to call the forecast.HoltWinters function, R told me that "could not find function "forecast.HoltWinters"". Inspect the installed package (v8.2) told me that it's true, there is no forecast.HoltWinters. But the manual in https://cran.r-project.org/web/packages/forecast/ clearly stated that forecast.HoltWinters is still available.
I have also tried stats::HoldWinters, but it's working wrong. The code run fine on another computer, but it couldn't run at all on mine. Is there any solution?
Here is the code. Book2.csv has enough data to last more than 3 periods.
dltt <- read.csv("book2.csv", header = TRUE)
dltt.ts <- ts(dltt$Total, frequency=12, start=c(2014,4))
dltt.ts.hw <- HoltWinters(dltt.ts)
library(forecast)
dltt.ts.hw.fc <- forecast.HoltWinters(dltt.ts.hw) //Error as soon as I run this line
Fit a HoltWinters model using the HoltWinters function and then use forecast. Its all in the help for HoltWinters and forecast, namely "The function invokes particular _methods_ which depend on the class of the first argument". I'll copy the guts of it here:
m <- HoltWinters(co2)
forecast(m)
Note this will call the non-exported forecast.HoltWinters function, which you should never call directly using triple-colon notation as some may suggest.

SSOAP processWSDL SoapType

I'm new to R/web services.
I'm trying to use R's SSOAP package to get/post soap messages.
I want to generate functions definitions using the genSOAPCLientInterface by way of the processWSDL function.
when i run the processWSDL function, i get the following error message, which i dont know how to interpret:
Error in SOAPType(el, namespaceDefs=namespaceDefinitions):could not find function "SOAPType"
any help/pointers would be very helpful.
wsdl_doc<-xmlParse(paste0(wsdl_url, '?wsdl')
definitions<-processWSDL(wsdl_doc, verbose=T, useInternalNodes=T)
Error in SOAPType(el, namespaceDefs = namespaceDefinitions) :
could not find function "SOAPType"
SOAPType is a class used in SSOAP package, but it is declared in package XMLSchema. And for some reason it is not an exported class.
This workaround helped me to make the function processWSDL() working for me:
SOAPType = XMLSchema:::SOAPType
def <- processWSDL(doc)

Error : could not find function "ImportMethodFrom"

I tried to run the code in Chapter 7 Data mining with R learning with case study book but I got an error in following line:
rankWorkflows(svm, maxs = TRUE)
The error was:
Error in as.character.default(X[[i]], ...) : no method for coercing
this S4 class to a vector
Then I searched on the internet and found following solution:
importMethodsFrom(GenomicRanges, as.data.frame)
and again again I got a new error:
Error: could not find function "importMethodFrom"
I searched a lot but I got nothing :(
You can try using library(sos) to find the packages where your function is located.
library(sos)
findFn("replaceherewithyourfunction")
Based on the answer of #Bea, there does not seem to be a importMethodsFrom anywhere in R. My guess is you found the call in a NAMESPACE file. Those files have different syntax than normal R scripts.
If you want to load a specific function from an R package (rather than all functions from a package), you can use libraryname::functionname instad of functionname in your code. In your case, replace as.data.frame with GenomicRanges::as.data.frame
If this does not work (for example because you don't have as.data.frame anywhere in your code), you can also load the whole GenomicRanges library with library(GenomicRanges)

Resources