Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VECTOR_ELT() can only be applied to a 'list', not a 'logical' #375

Open
MichaelChirico opened this issue Oct 12, 2022 · 2 comments
Open
Labels
feature a feature request or enhancement

Comments

@MichaelChirico
Copy link
Contributor

Found this by mistake, probably a friendlier error is warranted:

xml_find_all(read_xml("<a line1='1'></a>"), "@line1 = 1")
# Error in xml_nodeset(nodes) :
# VECTOR_ELT() can only be applied to a 'list', not a 'logical'
@MichaelChirico
Copy link
Contributor Author

Well, the issue is like so:

xml_find_all() evaluates the XPath search to a logical vector on which xml_nodeset() is attempted:

xml2/R/xml_find.R

Lines 87 to 90 in ab20789

xml_find_all.xml_node <- function(x, xpath, ns = xml_ns(x), ...) {
nodes <- .Call(xpath_search, x$node, x$doc, xpath, ns, Inf)
xml_nodeset(nodes)
}

xml_nodeset() attempts the node_duplicated routine:

nodes <- nodes[!.Call(nodes_duplicated, nodes)]

Which attempts the VECTOR_ELT() call:

SEXP cur = VECTOR_ELT(nodes, i);

It's easy enough to throw a stopifnot(is.list(.)) somewhere, but I do pause to wonder the expected behavior in this case. What should xml_find_all() do when the search result is a logical vector?

On the one hand, the XPath search executed successfully, so it seems like a waste to trigger an error. OTOH, it may be peculiar to allow xml_find_all() to return a variety of types.

@hadley
Copy link
Member

hadley commented Oct 30, 2023

You probably wanted to use xml_find_lgl() here, but in general I'm not sure how xpath types should interact with these functions. I think the ideal would probably be to throw an informative error saying that you should use xml_find_lgl() etc instead.

@hadley hadley added the feature a feature request or enhancement label Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants