r

FUN.VALUE missing when using vapply


I'm trying to apply a function over a list multiple times to extract the html from those urls, but when I tried to do that using vapply I got an error associated with the FUN,VALUE argument. I've the urls, and I want to parse them through the rvest::html_nodes, but this latter function doesn't accept lists, so lapply doesn't work.

library(xml2)

mat <- c("204554", "204521")
x <- vapply(paste0("https://www.camara.leg.br/deputados/", mat), read_html)

gives me an error

Error in vapply(paste0("https://www.camara.leg.br/deputados/", mat), read_html): 
  argument "FUN.VALUE" is missing, with no default

I tried some answers that I found in other topics but I couldn't resolve the problem.


Solution

  • vapply tries to coerce the results of your function (read_html) to the type that you specify in FUN.VALUE. for example, if you want all the results to be given in a character vector, you can use

    FUN.VALUE = character ()

    If you would rather have the results separated, try lapply instead of vapply, which will return a list with the elements separated.