Use of xpathSApply function in R - r

I am learning R now and faced these errors while using xpathSApply().
doc=xmlTreeParse("http://www.w3schools.com/xml/simple.xml")
xpathSApply(node,"//name",xmlValue)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘saveXML’ for signature ‘"character"’
and
doc2=htmlTreeParse("http://espn.go.com/nfl/team/_/name/bal/baltimore-ravens")
scores=xpathSApply(doc2,"//li[#class='score]",xmlValue)
Error in UseMethod("xpathApply") :
no applicable method for 'xpathApply' applied to an object of class "XMLDocumentContent"
How to resolve it.

Please refer to the xmlParse and htmlParse functions. Description:
‘xmlParse’ and ‘htmlParse’ are equivalent to the ‘xmlTreeParse’
and ‘htmlTreeParse’ respectively, except they both use a default
value for the ‘useInternalNodes’ parameter of ‘TRUE’, i.e. they
working with and return internal nodes/C-level nodes. These can
then be searched using XPath expressions via ‘xpathApply’ and
‘getNodeSet’.
My solution:
xml.file <- xmlParse( file = "http://www.w3schools.com/xml/simple.xml")
names <- xpathSApply( doc = xml.file
, path = "//name"
, fun = xmlValue
)
[1] "Belgian Waffles" "Strawberry Belgian Waffles" "Berry-Berry Belgian Waffles" "French Toast" "Homestyle Breakfast"
Then, for the ESPN page:
html.file <- htmlParse( file = "http://espn.go.com/nfl/team/_/name/bal/baltimore-ravens")
scores <- xpathSApply( doc = html.file
, path = "//li[#class='score']"
, fun = xmlValue
)
list()
There must be something wrong with your xpath expression since it is not returning anything :).

Below code worked for me:
URL<-"https://www.w3schools.com/xml/simple.xml"
response<-GET(URL)
doc=xmlTreeParse(response,useInternalNodes = TRUE)
root<-xmlRoot(doc)
xml_names<-xpathSApply(root,"//name",xmlValue)
you probably missed to set useInternalNodes option to TRUE, when using xmlTreeParse

Related

CellBlock function is giving an error related to class after minimal specification

I am having some specific problems with xlsx package after having moved to a newer version of R (4.0.5).
You will see that we have an error here for CellBlock function and the only object of note is the sheet itself as the rest are very stock standard things.
Browse[4]> xlsx::CellBlock(sheet, 1, 1, 100, 100, create = TRUE)
Error in .jcall("java/lang/Class", "Ljava/lang/Class;", "forName", cl, :
RcallMethod: cannot determine object class
Error in (function (cond) :
error in evaluating the argument 'Class' in selecting a method for function 'new': java.lang.StackOverflowError
Browse[4]> sheet
[1] "Java-Object{Name: /xl/worksheets/sheet1.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml}"
For full background you will see sheet above but I will include the lines that produced sheet as well:
wb <- createWorkbook(type = "xlsx")
sheet <- createSheet(wb, sheetName = "New loadings")
Some of the packages relevant to the problem above:
rJava_1.0-6
xlsxjars_0.6.1
xlsx_0.6.5
Let me know if you require more info.

unable to find an inherited method for function 'distance' for signature '"matrix", "character"', but it's a df?

Here is all my code leading up to the function as per #Till request!
library("phyloseq")
library("qiime2R")
library("vegan")
# read in phyloseq objects from qiime
physeq<-qza_to_phyloseq(
features="~/Documents/qiime2-analyses/CRD/fresh_run/table.qza",
tree="~/Documents/qiime2-analyses/CRD/fresh_run/rooted-tree.qza",
"~/Documents/qiime2-analyses/CRD/fresh_run/taxonomy.qza",
metadata = "crd-metadata.txt")
# Clean out unwanted taxa annotations. Base script removes endozoicimonaceae, escherischia,
# and shigella contaminates
physeq <- subset_taxa(physeq, Family!="f__Endozoicimonaceae")
physeq <- subset_taxa(physeq, Family!="f__Enterobacteriaceae")
physeq <- subset_taxa(physeq, Family!="f__mitochondria")
physeq <- subset_taxa(physeq, Class!="c__Chloroplast")
#pull out otu table
otu_table <- (as.data.frame(otu_table(physeq)))
# rotate otu matrix layout
rownames(otu_table) <- factor(rownames(otu_table), levels = rownames(otu_table))
otu_ord <- as.data.frame(t(otu_table[rowSums(otu_table)!=0, ]))
# remove any rows or columns with only 0s
otu_ord <- otu_ord[, colSums(otu_ord !=0)>0]
otu_ord <- otu_ord[rowSums(otu_ord[])>0,]
#edits from observations of this StO chat
rownames(otu_ord) <- gsub("sample-", "", rownames(otu_ord))
rownames(otu_ord) <- as.numeric(rownames(otu_ord))
otu_ord <- as.matrix(otu_ord)
#the args of the function
raup_crick_abundance = function(spXsite=otu_ord, plot_names_in_col1=TRUE,
classic_metric=FALSE, split_ties=TRUE,
reps=9999, set_all_species_equal=FALSE,
as.distance.matrix=TRUE, report_similarity=FALSE){
Please note the whole function is verbatim from Stegen_etal_ISME_2013 github linked below and here.
I am receiving the error
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘distance’ for signature ‘"matrix", "character"’
Called from: (function (classes, fdef, mtable)
{
methods <- .findInheritedMethods(classes, fdef, mtable)
if (length(methods) == 1L)
return(methods[[1L]])
else if (length(methods) == 0L) {
cnames <- paste0("\"", vapply(classes, as.character,
""), "\"", collapse = ", ")
stop(gettextf("unable to find an inherited method for function %s for signature %s",
sQuote(fdef#generic), sQuote(cnames)), domain = NA)
}
else stop("Internal error in finding inherited methods; didn't return a unique method",
domain = NA)
})(list("matrix", "character"), new("nonstandardGenericFunction",
.Data = function (physeq, method, type = "samples", ...)
{
standardGeneric("distance")
}, generic = "distance", package = "phyloseq", group = list(),
valueClass = character(0), signature = c("physeq", "method",
"type"), default = NULL, skeleton = (function (physeq, method,
type = "samples", ...)
stop("invalid call in method dispatch to 'distance' (no default method)",
domain = NA))(physeq, method, type, ...)), <environment>)
Browse[1]> traceback()
No traceback available
within this function linked here.
My argument is a data.frame (dput() below) with no character strings? As I understand it, the error is saying the function distance () can't be calculated with matrix or character strings, which I agree with...
Therefore, I am unsure why distance () cannot operate with my arg, if I am interpreting the error correctly.
Thank you.
Here is my Qiime2 OTU table.qza linked to this Dropbox file, my rooted tree linked here, and my taxonomy linked here to recreate my phyloseq object.
What the error message is actually trying to get across is that your matrix has character values and the function you called can not handle it. The first column in data.frame is a character column.
If your data frame is called ex_otu_ord, try this:
ex_otu_ord <- ex_otu_ord[-1]
ex_otu_ord <- as.matrix(ex_otu_ord)
Then try again to call the function on ex_otu_ord.

R: overwrite rules that apriori produced

I want to overwrite confidence values of an apriori output, then put the output into is.redundant. I got an error at the last line. How do you do it?
library(arules)
data(Groceries) # read sample data
# find apriori rules
outApriori = apriori(Groceries,
parameter = list(support=0.001, confidence=0.70, minlen=1, maxlen=4)
,appearance = list(rhs = "whole milk" ) )
dfApriori = as.data.frame(inspect(outApriori[1:5])) # convert into data.frame
# modify the confidence value conservatively by adding one error sample
(estimateConfidence= dfApriori$count / (1 + round( dfApriori$count / dfApriori$confidence ) ))
dfApriori$confidence = estimateConfidence
outRmRedundant <- dfApriori[!is.redundant(dfApriori)] # Error in (function (classes, fdef, mtable) :
# Error in (function (classes, fdef, mtable) :
# unable to find an inherited method for function ‘is.redundant’ for signature ‘"data.frame"’
The function is.redundant() expects a rules object not a data.frame. Here is how you change the quality slot of the rules object:
library(arules)
data(Groceries)
# find apriori rules
rules <- apriori(Groceries,
parameter = list(support=0.001, confidence=0.70, minlen=1, maxlen=4),
appearance = list(rhs = "whole milk"))
estimatedConfidence <- quality(rules)$count / (1 + round(quality(rules)$count / quality(rules)$confidence))
quality(rules)$confidence <- estimatedConfidence
rules.nonredundant <- rules[!is.redundant(rules)]
inspect(head(rules.nonredundant))
BTW: You might want to look at Laplace Corrected Confidence (http://michael.hahsler.net/research/association_rules/measures.html#laplace) which can be calculated using the function interestMeasure().

Using XLConnect to read in xls

I'm using the following code to try and read in an old xls file
library("XLConnect")
path <- "C:/Users/foo/Desktop/WEEK 17.xls"
df <- readWorksheet(path, sheet = 1)
i get the following error
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘readWorksheet’ for signature ‘"character", "numeric"’
Does anyone know why?
I can open the file in excel
It s because readWorksheet use workbook as object.
You have to
# Load workbook
wb <- loadWorkbook("C:/Users/foo/Desktop/WEEK 17.xls")
and then
df <- readWorksheet(wb, sheet = 1)
or use
readWorksheetFromFile("C:/Users/foo/Desktop/WEEK 17.xls", sheet = 1)
Which realy do the same
> XLConnect::readWorksheetFromFile
function (file, ...)
{
args <- list(...)
args$object <- loadWorkbook(file, create = FALSE)
do.call("readWorksheet", args)
}
<environment: namespace:XLConnect>

error with RBGL package in R

I want to install the RGBL package in bioconductor to perform some graph algorithms.
I updated R to the latest version 3.2.0, and installed the package as instructed on http://www.bioconductor.org/
source("http://bioconductor.org/biocLite.R")
biocLite("RBGL")
It was installed successfully, then I tried to run
library(graph)
library(RBGL)
x<- strongComp(graph)
and returns this error
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘isDirected’ for signature ‘"igraph"’
Here's the traceback
> traceback()
4: stop(gettextf("unable to find an inherited method for function %s for signature %s",
sQuote(fdef#generic), sQuote(cnames)), domain = NA)
3: (function (classes, fdef, mtable)
{
methods <- .findInheritedMethods(classes, fdef, mtable)
if (length(methods) == 1L)
return(methods[[1L]])
else if (length(methods) == 0L) {
cnames <- paste0("\"", vapply(classes, as.character,
""), "\"", collapse = ", ")
stop(gettextf("unable to find an inherited method for function %s for signature %s",
sQuote(fdef#generic), sQuote(cnames)), domain = NA)
}
else stop("Internal error in finding inherited methods; didn't return a unique method",
domain = NA)
})(list("igraph"), function (object)
standardGeneric("isDirected"), <environment>)
2: isDirected(g)
1: strongComp(graph)
My system is Windows 32-bit.
I'm not sure if this is enough information. Please let me know if any other information needed.
Any ideas are appreciated, thanks!
EDIT:
I used the igraph packaged to create the graph object from an edge list with weight
library(igraph)
graph<- graph.data.frame(edge.list[,c(2:4)],directed=TRUE)
I'm not very good with generating random number, here's a reproducible example for my graph
set.seed(123)
edge.list<-cbind(seq(10),c(1,1,2,3,3,4,5,5,5,5),c(2,2,3,5,4,3,4,4,4,2),
runif(10, 1, 30))
colnames(edge.list) <-c("ID","V1","V2","weight")
As pointed out in the comments, you need to make a graph object, not an igraph object.
Here's how I might transform your edge.list into the form that graph expects.
rawEL <- data.frame(source = as.character(edge.list[,1]),
edges = as.character(edge.list[,2]),
weights = edge.list[,3], stringsAsFactors=F
)
V <- unique(c(rawEL$source, rawEL$edges))
edL <- lapply(
split(
rawEL[,-1],
factor(edge.list[,1], levels=V)
), as.list
)
gr <- graphNEL(V, edL, "directed")
plot(gr)

Resources