RSS

Category Archives: XML and File Handling

XML, Folder and File handling


R provide us the facility to read and write XML and can do basic file and folder handling like check the existence of folder, delete directory, create directory, create file path and all basic file handling. These basic handling helps us to remove hard coding and deploy R solutions to any machine after changing required parameters in the XML file. For this, we need to install XML library.

Reading XML file

Suppose we have XML file, name “nodes.xml”

<Node1>
<N1>
<N1.1>N1.1 Value</N1.1>
<N1.2>N1.2 Value</N1.2>
</N1>
</Node1>

> library(“XML”)
> xmlnodes = xmlTreeParse(“nodes.xml”, getDTD = F) – It parse the XML and create the tree that contain different element of XML.
> rootnode = xmlRoot(xmlnodes) – To get the top node after creating its tree structure.

To get the value of node N1.1, we have xmlChildren function

> Node1=xmlChildren(rootnode)[c(“N1”)][[1]]
> xmlChildren(Node1)[c(“N1.1”)][[1]][[1]]
N1.1 Value

To get the value of node N1.2

> xmlChildren(Node1)[c(“N1.2”)][[1]][[1]]
N1.2 Value

 

File Handling

R provides below file function

file.create(…, showWarnings = TRUE)
file.exists(…)
file.remove(…)
file.rename(from, to)
file.append(file1, file2)
file.copy(from, to, overwrite = TRUE, recursive = FALSE, copy.mode = TRUE)

file.create – creates files with the given names in the working directory or in the path specified. if they do not already exist and truncates them if they do.  They are created with the maximal read/write permissions allowed by the ‘umask’ setting (where relevant). By default a warning is given (with the reason) if the operation fails.

file.exists – It returns a logical vector indicating whether the files named by its argument exist. eg. file.exists(“C:/test.R”)

file.remove – It will remove file from the path specified.  eg. file.remove(“C:/test.R”)

file.rename – file.rename attempts to rename files (and from and to must be of the same length).  It can move files but not directories between volumes. It will remove file specified in the “from”

file.append – It attempts to append the files named by its second argument to those named by its first. For example – file.append(“file1.R”, “File2.R”) – File2 content will be appended in the file1.

file.copy – It works in a similar way to file.append but with the arguments in the natural order for copying. Copying to existing destination files is skipped unless overwrite = TRUE. The to argument can specify a single existing directory. If copy.mode = TRUE (added in R 2.13.0) file read/write/execute permissions are copied where possible. recursive = TRUE copies all files and folder to the target directory. For example – file.copy(“MyR/.”, “MyNewRFolder/.”, recursive = T) – This will copy all the file and folder of MyR directory to MyNewRFolder directory.

By combining both the features, we can make move all the configurable things within XML and bundle them as a package which we can deploy in any machine.

 
Leave a comment

Posted by on September 3, 2013 in XML and File Handling

 

Tags: , , , , , , , , , , , ,