How to create tibble from xml_attr and xml_text in tidyverse?

I am trying to create dataframe / tibble from xml_attr and xml_text in tidyverse pipeline ...

x <- read_html("https://stats.oecd.org/DownloadFiles.aspx?DatasetCode=CRS1")
x %>% 
  xml_find_all("//*/tr/td/a") %>% 
  tibble(a = map_chr(., xml_text),
         b = map_chr(., xml_attr, "onclick"))

but i get an error
Error:
! All columns in a tibble must be vectors.
x Column . is a xml_nodeset object.

I don't know why it does not work (as I would expect) while the following gives a result.
(To be honest I would not know what to do with it, if you gave it to me as a present :grinning:)

suppressPackageStartupMessages(
  {
library(xml2)
library(tibble)
library(purrr)
  }
)

x <- read_html("https://stats.oecd.org/DownloadFiles.aspx?DatasetCode=CRS1")
x1 <- xml_find_all(x,"//*/tr/td/a") 
a <- map_chr(x1, xml_text)
b <- map_chr(x1, xml_attr, "onclick")
tibble(a = a, b = b)
#> # A tibble: 20 x 2
#>    a                          b                                                 
#>    <chr>                      <chr>                                             
#>  1 CRS 2020 / SNPC 2020       return OpenFileac3a59f6_5acb_4747_9ef3_3c00e2ab3e~
#>  2 CRS 2019 / SNPC 2019       return OpenFile348f223c_7d11_43ad_ab95_b7c0ab537f~
#>  3 CRS 2018 / SNPC 2018       return OpenFileed94e89a_e81e_4718_ac69_2a51f54296~
#>  4 CRS 2017 / SNPC 2017       return OpenFilebf6e1a95_a4ac_4545_9e91_1a38c9b738~
#>  5 CRS 2016 / SNPC 2016       return OpenFileb64f050c_4e2f_44ed_a8e6_db32ce00fd~
#>  6 CRS 2015 / SNPC 2015       return OpenFile4440e8de_f471_479a_9692_ddd7bf06d7~
#>  7 CRS 2014 / SNPC 2014       return OpenFilea990e786_6647_4e5a_8795_bbec445294~
#>  8 CRS 2013 / SNPC 2013       return OpenFilefa4ae8e5_a954_4cd5_ae72_9053094bf9~
#>  9 CRS 2012 / SNPC 2012       return OpenFileed338f01_47b7_421c_92be_dee8141202~
#> 10 CRS 2011 / SNPC 2011       return OpenFile05dd3f52_a5fd_4d20_8ded_2b4159a567~
#> 11 CRS 2010 / SNPC 2010       return OpenFilecb44825a_d8e8_41b0_80f8_f42e67f961~
#> 12 CRS 2009 / SNPC 2009       return OpenFilea530b713_0f45_48d7_b921_e3a70871ba~
#> 13 CRS 2008 / SNPC 2008       return OpenFile251471b4_5117_49cf_8d73_7e9b4eb186~
#> 14 CRS 2007 / SNPC 2007       return OpenFilef49c3166_e1fd_48f1_831a_c1fe38b96c~
#> 15 CRS 2006 / SNPC 2006       return OpenFile8d7734f9_4fc6_4c3a_994f_8bfd271317~
#> 16 CRS 2004-05 / SNPC 2004-05 return OpenFile26a883a7_afb4_4d98_95e8_29cc63638c~
#> 17 CRS 2002-03 / SNPC 2002-03 return OpenFile28e76303_4d91_4a36_aabb_6cce13c084~
#> 18 CRS 2000-01 / SNPC 2000-01 return OpenFile02d8f462_08eb_4b99_b12d_bd6a0edf96~
#> 19 CRS 1995-99 / SNPC 1995-99 return OpenFilec0c345b3_74e8_499d_a280_e8af7e3f47~
#> 20 CRS 1973-94 / SNPC 1973-94 return OpenFile82c8801d_641f_49ec_8ace_9e003224c3~
Created on 2022-02-22 by the reprex package (v2.0.1)

And this also works (????)

suppressPackageStartupMessages(
  {
library(xml2)
library(tibble)
library(purrr)
  }
)
read_html("https://stats.oecd.org/DownloadFiles.aspx?DatasetCode=CRS1") %>%
  xml_find_all(.,"//*/tr/td/a") %>% 
  map_dfr(., function(x) {
    tibble(a=xml_text(x),
           b=xml_attr(x, "onclick"))
  }) %>% 
  head()
#> # A tibble: 6 x 2
#>   a                    b                                                     
#>   <chr>                <chr>                                                 
#> 1 CRS 2020 / SNPC 2020 return OpenFileac3a59f6_5acb_4747_9ef3_3c00e2ab3eb5();
#> 2 CRS 2019 / SNPC 2019 return OpenFile348f223c_7d11_43ad_ab95_b7c0ab537fdd();
#> 3 CRS 2018 / SNPC 2018 return OpenFileed94e89a_e81e_4718_ac69_2a51f5429657();
#> 4 CRS 2017 / SNPC 2017 return OpenFilebf6e1a95_a4ac_4545_9e91_1a38c9b738de();
#> 5 CRS 2016 / SNPC 2016 return OpenFileb64f050c_4e2f_44ed_a8e6_db32ce00fd4f();
#> 6 CRS 2015 / SNPC 2015 return OpenFile4440e8de_f471_479a_9692_ddd7bf06d7d2();
Created on 2022-02-22 by the reprex package (v2.0.1)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.