[R][TidyR] Use of the "hoist" function to extract data from XML

Viewed 13

I'm running into a seamingly very simple issue, and yet I haven't find the solution so far, so I'm trying here.

I'm trying to extract data from an XML (which is store in "xml_totest" here). To do so, I'm usi

  lecture_xml = as_list(read_xml(xml_totest))
  xml_df = tibble::as_tibble(lecture_xml) %>%
    unnest_longer(extraction)

So far, so good. I'm then filtering on some part of the xml only :

  tempo_demandes = xml_df %>%
    dplyr::filter(extractionMDPH_id == "demande")

So far, so good again. I got at this point a table of list, looking like this :

   extraction    extraction_id
   <named list>      <chr>            
 1 <named list [7]>  demande          
 2 <named list [8]>  demande          
 3 <named list [8]>  demande          

My goal here is to extract the data on each line, knowing two things : (i) the size of the list in each row is not constant ; (ii) my XML is nested, so if I just unnest, it will be too long, and will not work.

Therefore, I'm using hoist, which allows me to select which part of the named_list I want to keep. For example, this works great :

   tempo_demandes %>% hoist(extractionMDPH,"idDossier","typeDossier")

And gives me what i need (here, two columns, one with the idDossier part of the list, one with the TypeDossier).

  idDossier    typeDossier  extraction   extraction_id
   <named list> <named list> <named list>     <chr>            
 1 <list [1]>   <list [1]>   <named list [5]> demande          
 2 <list [1]>   <list [1]>   <named list [6]> demande          
 3 <list [1]>   <list [1]>   <named list [6]> demande  

After that, I can unlist, and it all works fine.

My issue comes when I try to generalize what I do. What I need to do is to replace "idDossier","typeDossier" in my call to hoist with something I would have defined externaly. Yet, this simple call for example :

args <- c("idDossier","typeDossier")
tempo_demandes %>% hoist(extractionMDPH,args)

I can't find the right format for the argument I need to put in hoist ! Seems pretty simple though, so it's quite frustrating. Would someone have any way to help me on that ?

Thanks a lot in advance !

0 Answers
Related