Reputation: 9
I'm currently trying to generate a R-script so I can mass produce xml files. The files I am trying to make only vary in certain areas like SEED# where I want the SEED# to change in value each time.
I also don't know ho to write the xml file in R. The XML package didn't have a way to write an XML file from what I saw. I tried to write it as a txt file but I got errors with the <> symbols and no output was produced, how can I write it so no errors come up?
The file looks like this, the ALLCAPS are the parts I want to change.
<modelThree>
<name>willamette</name>
<network load="true">willamette2</network>
<ocean>9313</ocean>
<firstNonOcean>2</firstNonOcean>
<species>SPECIESVECTOR</species>
<seed>SEED</seed>
<nyears>30</nyears>
<output>c:\users\rbrown09\SMURFrun\output\OUTPUTLOCATION</output>
<ProportionMove>0.25</ProportionMove>
<HSIperception>HSIPER</HSIperception>
<HSIimportance>1.0</HSIimportance>
<Beta>BETAVALUE</Beta>
<KMultiplier>0.9</KMultiplier>
<PdoMultipliers>1 1.054752174 1.109504348 1.164256522 1.219008696 1.27376087 1.328513043 1.383265217 1.438017391 1.492769565 1.547521739 1.602273913 1.657026087 1.711778261 1.766530435 1.821282609 1.876034783 1.930786957 1.98553913 2.040291304 2.095043478 2.149795652 2.204547826 2.2593 2.204547826 2.149795652 2.095043478 2.040291304 1.98553913 1.930786957 1.876034783 1.821282609 1.766530435 1.711778261 1.657026087 1.602273913 1.547521739 1.492769565 1.438017391 1.383265217 1.328513043 1.27376087 1.219008696 1.164256522 1.109504348 1.054752174</PdoMultipliers>
<PdoStartIndex>0</PdoStartIndex>
</modelThree>
Upvotes: 0
Views: 429
Reputation: 30425
You could use the XML package. In practice I find it very slow for creating XML nodes especially when the number of nodes increases. One option I prefer using is whisker
'<modelThree>
<name>willamette</name>
<network load="true">willamette2</network>
<ocean>9313</ocean>
<firstNonOcean>2</firstNonOcean>
<species>{{SPECIESVECTOR}}</species>
<seed>{{SEED}}</seed>
<nyears>30</nyears>
<output>c:\users\rbrown09\{{SMURF}}run\output\{{OUTPUTLOCATION}}</output>
<ProportionMove>0.25</ProportionMove>
<HSIperception>{{HSIPER}}</HSIperception>
<HSIimportance>1.0</HSIimportance>
<Beta>{{BETAVALUE}}</Beta>
<KMultiplier>0.9</KMultiplier>
<PdoMultipliers>1 1.054752174 1.109504348 1.164256522 1.219008696 1.27376087 1.328513043 1.383265217 1.438017391 1.492769565 1.547521739 1.602273913 1.657026087 1.711778261 1.766530435 1.821282609 1.876034783 1.930786957 1.98553913 2.040291304 2.095043478 2.149795652 2.204547826 2.2593 2.204547826 2.149795652 2.095043478 2.040291304 1.98553913 1.930786957 1.876034783 1.821282609 1.766530435 1.711778261 1.657026087 1.602273913 1.547521739 1.492769565 1.438017391 1.383265217 1.328513043 1.27376087 1.219008696 1.164256522 1.109504348 1.054752174</PdoMultipliers>
<PdoStartIndex>0</PdoStartIndex>
</modelThree>' -> myTemplate
library(whisker)
myDF <- data.frame(
SPECIESVECTOR = paste("A", "B")
, SEED = "ANOTHER"
, SMURF = "PAPA"
, OUTPUTLOCATION = "VILLAGE"
, HSIPER = 78
, BETAVALUE = 2)
myXML <- whisker.render(myTemplate, myDF)
You can check the output:
library(XML)
> xmlParse(myXML)
<?xml version="1.0"?>
<modelThree>
<name>willamette</name>
<network load="true">willamette2</network>
<ocean>9313</ocean>
<firstNonOcean>2</firstNonOcean>
<species>A B</species>
<seed>ANOTHER</seed>
<nyears>30</nyears>
<output>c:\users\rbrown09\PAPArun\output\VILLAGE</output>
<ProportionMove>0.25</ProportionMove>
<HSIperception>78</HSIperception>
<HSIimportance>1.0</HSIimportance>
<Beta>2</Beta>
<KMultiplier>0.9</KMultiplier>
<PdoMultipliers>1 1.054752174 1.109504348 1.164256522 1.219008696 1.27376087 1.328513043 1.383265217 1.438017391 1.492769565 1.547521739 1.602273913 1.657026087 1.711778261 1.766530435 1.821282609 1.876034783 1.930786957 1.98553913 2.040291304 2.095043478 2.149795652 2.204547826 2.2593 2.204547826 2.149795652 2.095043478 2.040291304 1.98553913 1.930786957 1.876034783 1.821282609 1.766530435 1.711778261 1.657026087 1.602273913 1.547521739 1.492769565 1.438017391 1.383265217 1.328513043 1.27376087 1.219008696 1.164256522 1.109504348 1.054752174</PdoMultipliers>
<PdoStartIndex>0</PdoStartIndex>
</modelThree>
Upvotes: 3