xml2 icon indicating copy to clipboard operation
xml2 copied to clipboard

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

Open MichaelChirico opened this issue 3 years ago • 2 comments

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 avatar Oct 12 '22 06:10 MichaelChirico

Well, the issue is like so:

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

https://github.com/r-lib/xml2/blob/ab2078976c9ab57116c7c9175632e7d19d45273f/R/xml_find.R#L87-L90

xml_nodeset() attempts the node_duplicated routine:

https://github.com/r-lib/xml2/blob/2f3da489f58c707a283a6bb816d12a3df62996b2/R/classes.R#L76

Which attempts the VECTOR_ELT() call:

https://github.com/r-lib/xml2/blob/2f3da489f58c707a283a6bb816d12a3df62996b2/src/xml2_node.cpp#L560

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.

MichaelChirico avatar Oct 12 '22 06:10 MichaelChirico

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 avatar Oct 30 '23 18:10 hadley