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>
Related
Hello
I am practicing ShortRead package for analyzing fastq file format. A text book I am reading, write a function for writing large fastq files as below:
trim.file <- function (f1,
destination=sprintf("%s_filtered.fastq", fq)){
stream <- open(FastqStreamer(f1))
on.exit(close(stream))
repeat {
fq <- yield(stream)
if (length(fq) == 0){break}
fq <- fq[nFilter()(fq)]
fq <- trimTails(fq, 5, "A", successive = T)
fq <- fq[width(fq) > 80]
writeFastq(fq, destination, "a", compress = F)
}
}
trim.file(FastqFiles [1])
The FastqFiles [1] is the path/filename of the original fastq file that is created with this code FastqFiles <- list.files(path=fasrQDir, pattern = "*.fastq", full.names = T) and fasrQDir is the directory of my original fastq file.
When I run this code I encounter this error:
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'file' in selecting a method for function 'writeFastq': no method for coercing this S4 class to a vector
In addition: Warning message:
In open.connection(con$con) :
Please let me know what is the solution.
Best wishes
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.
Can anywone tell me why this code:
filename3 = "restaurants.xml"
if (!file.exists(filename3)) csv <- download.file("https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Frestaurants.xml", filename3, "curl")
if (!exists("data3")) data3 <- xmlRoot(xmlTreeParse(filename3, useInternalNodes = TRUE))
zips <- xpathSApply(data3, "//zipcode", xmlValue)
is giving me the error:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function 'saveXML' for signature '"XMLDocument"'
Thank you.
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
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)