我目前正在尝试生成一个R脚本,因此我可以批量生成xml文件。我想要制作的文件只在SEED#等特定区域有所不同,我希望每次都能改变SEED#的值。
我也不知道如何在R中编写xml文件.XML包没有办法从我看到的内容中编写XML文件。我试着把它写成一个txt文件,但我在<>时遇到错误产生了符号而没有输出,我怎么写它才能出现错误?
文件看起来像这样,ALLCAPS是我想要更改的部分。
<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>
答案 0 :(得分:3)
您可以使用XML包。在实践中,我发现创建XML节点的速度非常慢,尤其是当节点数量增加时。我更喜欢使用的一个选项是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)
您可以查看输出:
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>