Dimitris Karditsas
Dimitris Karditsas

Reputation: 51

reading and parsing xml files with a loop

i have the following xml files in a folder. Manually if i want to read them and parse them i need to do the following two chunks:

if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install(version = "3.15")
BiocManager::install("biocLite")
if (!require("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install(version = "3.15")
if (!require("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("KEGGgraph")
if(!require(xml2)){
    install.packages("xml2")
    library(xml2)
}
if(!require(XML)){
    install.packages("XML")
    library(XML)
}
library(KEGGgraph)
library(xml2)
library(Rgraphviz)
library(methods)
Aml<- read_xml("Acute myeloid leukemia.xml")
Nsclc<- read_xml("Non-small cell lung cancer.xml")
Sclc <-read_xml("Small cell lung cancer.xml")
Brcan <-read_xml("Breast cancer.xml")
Aml_parsed <- parseKGML2Graph(Aml, genesOnly=FALSE)
Nsclc_parsed<- parseKGML2Graph(Nsclc, genesOnly=FALSE)
Sclc_parsed <-parseKGML2Graph(Sclc, genesOnly=FALSE)
Brcan_parsed <-parseKGML2Graph(Brcan, genesOnly=FALSE)

What i want to ask is how can i build a functionn that will do the previous automatically? I tried the following to read them but it doesnt work as i want

cancer_data_xml<- list.files(pattern = "*.xml")
list_of_cancer_data <- as.list(cancer_data_xml)

Can you please suggest me something in order not to have to do it all this manually one by one?

Upvotes: 0

Views: 52

Answers (1)

dcsuka
dcsuka

Reputation: 2997

Instead of reading the xml and making the graph for each file individually, you can use iteration to perform all of the tasks simultaneously. The R code would look something like this:

xml_files <- lapply(list.files(pattern = "*.xml"),
  function(x) {parseKGML2Graph(read_xml(x))})

Upvotes: 1

Related Questions