Unable to view source code of a package in R - r

I am trying to view the source code of the function knnreg in caret.
> getAnywhere(knnreg.default)
A single object matching ‘knnreg.default’ was found
It was found in the following places
package:caret
registered S3 method for knnreg from namespace caret
namespace:caret
with value
function (x, ...)
{
if (!any(class(x) %in% "formula"))
stop("knnreg only implemented for formula objects")
}
<environment: namespace:caret>
What's happening? Where is the source code?

I think the error message is pretty obvious:
knnreg only implemented for formula objects
Use getAnywhere(knnreg.formula) to see the source code.

Related

Why does wrapper function result in error

I'm trying to include a function from the Bioconductor package "simpIntLists" in my own package. "import(simpIntLists)" is added to the Namespace file.
However, the function results in an error
do_xy <- function(organism, IDType){
network <- simpIntLists::findInteractionList(organism, IDType)
}
do_xy("human", "EntrezId")
#Error message
data set ‘HumanBioGRIDInteractionEntrezId’ not foundError in get("HumanBioGRIDInteractionEntrezId") :
object 'HumanBioGRIDInteractionEntrezId' not found
# results in the same error (outside of the function)
simpIntLists::findInteractionList(organism, IDType)
It works fine when simpIntLists is attached
# works
library(simpIntLists)
network <- simpIntLists::findInteractionList(organism, IDType)
I saw the code here (https://bioconductor.org/packages/release/data/experiment/html/simpIntLists.html).
This code does not seem to take into account the situation where the package is used without installation.
For your information, the relevant part to this error is around the line 52.
else if (organism == 'human'){
if (idType == 'EntrezId'){
data(HumanBioGRIDInteractionEntrezId);
interactionList <- get("HumanBioGRIDInteractionEntrezId");
}
It tries to fetch the data to the namespace but it fails to do so if the package is not imported via library yet. This only generates a warning. The error then occurs when it tries to use the data because it does not exist in the namespace.
A workaround is that you import the data in your code explicitly. Then you can use the function as below. Note that the warning remains because of the embedded package code. If the warning is annoying, use suppressWarnings.
data(HumanBioGRIDInteractionEntrezId, package="simpIntLists")
simpIntLists::findInteractionList("human", "EntrezId")

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)

R no method for coercing S4 class to a vector BioConductor AnnotationDbi package

I have in code a method call:
> MYOBJECT
PATHID2NAME map for KEGG (object of class "AnnDbBimap")
I am running somebody else's function and getting an error
>as.list.default(MYOBJECT) :
no method for coercing this S4 class to a vector
I can see that in the source code for the function that what is actually being called is
as.list(MYOBJECT)
My hunch is that R is not using the correct as.list function due to possibly some namespace error. How do I manually import the as.list from AnnotationDbi and force R to use it instead of whatever S3 method it's calling?

Looking into the predict function in R

I am trying to understand how the SVM predict function works when using command ksvm from R package kernlab.
I tried the look into the predict function using the following commands:
methods(class="ksvm")
getAnywhere(ksvm:::predict)
However, I get the following output and not the complete predict function:
A single object matching ‘:::’ ‘ksvm’ ‘predict’ was found
It was found in the following places
package:base
namespace:base
with value
function (pkg, name)
{
pkg <- as.character(substitute(pkg))
name <- as.character(substitute(name))
get(name, envir = asNamespace(pkg), inherits = FALSE)
}
<bytecode: 0x00000000088be4f8>
<environment: namespace:base>
Warning message:
In find(x, numeric = TRUE) :
elements of 'what' after the first will be ignored
Can someone help with how to obtain the complete predict function?
Update 1:
Suggestion from misspelled worked fine on predict function for ksvm in kernlab package but doesn't seem to work on svm in e1071 package.
It throws the following error:
> getMethod("predict", "svm")
Error in getMethod("predict", "svm") :
no generic function found for 'predict'
In general, how to know which get method to use?
You were close. I was able to get the function code with getMethod("predict", "ksvm"). This answer describing S4 method dispatch was helpful. View source code for function
Per your updated question, I can get the source code for predict.svm using the ::: function. Specifically with e1071:::predict.svm. The link above also describes this in the section on S3 method dispatch.
There are at least a couple of things going on here. First is that in the former case you are dealing with S4 objects and S3 objects in the latter. The two systems have different method dispatches and different ways to view the source code. Another wrinkle is that the predict.svm function is an invisible function and can only be viewed either with ::: or getAnywhere().

R namespace access and match.fun

I'm working on an R package where one of the functions contains a match.fun call to a function in a package that's imported to the package namespace. But on loading the package, the match.fun call can't find the function name. From Hadley Wickham's description I think I'm doing everything right, but this is clearly not the case.
Example:
# in the package file header, for creation of the NAMESPACE via roxygen2:
##` #import topicmodels
# The function declaration in the package
ModelTopics <- function(doc.term.mat, num.topics, topic.method="LDA"){
topic.fun <- match.fun(topic.method)
output <- topic.fun(doc.term.mat, k=num.topics)
return(output)
}
And then in R:
> library(mypackage)
> sample.output <- ModelTopics(my.dtm, topic.method="LDA", num.topics=5)
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'LDA' of mode 'function' was not found
From my understanding of namespaces, the match.fun call should have access to the package namespace, which should include the topicmodels functions. But that doesn't appear to be the case here. If I import topicmodels directly to the global namespace for the R session, then this works.
Any help is much appreciated. This is R64 2.14.1 running on OSX.
UPDATE:
The package is here
Re the DESCRIPTION file, perhaps that's the problem: roxygen2 doesn't update the DESCRIPTION file with Imports: statements. But none of the other packages are listed there, either; only the match.fun calls appear to be affected.
Re the NAMESPACE extract, here's the import section:
import(catspec)
import(foreach)
import(gdata)
import(Hmisc)
import(igraph)
import(lsa)
import(Matrix)
import(plyr)
import(RecordLinkage)
import(reshape)
import(RWeka)
import(stringr)
import(tm)
import(topicmodels)
I believe this an issue of scope. Although you have imported topicmodels and thus LDA, you haven't exported these functions, so they aren't available in the search path.
From ?match.fun:
match.fun is not intended to be used at the top level since it will
perform matching in the parent of the caller.
Thus, since you are using ModelTopics in the global environment, and LDA isn't available in the global environment, the match.fun call fails.
It seems to me you have two options:
Option 1: use get
An alternative would be to use get where you can specify the environment. Consider this: try to use match.fun to find print.ggplot in the package ggplot2:
match.fun("print.ggplot")
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'print.ggplot' of mode 'function' was not found
Since print.ggplot isn't exported, match.fun can't find it.
However, get does find it:
get("print.ggplot", envir=environment(ggplot))
function (x, newpage = is.null(vp), vp = NULL, ...)
{
set_last_plot(x)
if (newpage)
grid.newpage()
data <- ggplot_build(x)
gtable <- ggplot_gtable(data)
if (is.null(vp)) {
grid.draw(gtable)
}
else {
if (is.character(vp))
seekViewport(vp)
else pushViewport(vp)
grid.draw(gtable)
upViewport()
}
invisible(data)
}
<environment: namespace:ggplot2>
Option 2: Export the functions from topicmodels that you need
If you make the necessary functions from topicmodels available in your NAMESPACE, your code should also work.
Do this by either:
Selective exporting LDA and other functions to the namespace using #export
Declare a dependency, using Depends: topicmodels - this is the same as calling library(topicmodels) in the global environment. This will work, but is possibly a bit of a brute force approach. It also means that any subsequent library call can mask your LDA function, thus leading to unexpected results.
Answering my own question: the DESCRIPTION file wasn't updating the Imports: and Depends: fields after re-roxygenizing the updated code. Hence the match.fun issues. Out of curiousity, why does this affect match.fun but not a range of other function calls made elsewhere to imported package functions?

Resources