我有以下R代码:
df <- xmlToDataFrame(/Users/usr/Desktop/shares.xml)
df$timeStamp <- strptime(as.character(df$XTimeStamp), "%H:%M:%OS")
df$SharePrice <- as.numeric(as.character(df$SharePrice))
sapply(df, class)
options("digits.secs"=3)
diff <- diff(df$SharePrice)
diff
sink (file="c:/xampp/htdocs/data.xml", type="output",split=FALSE)
cat("<graph caption=\"Share Price Data Wave\" subcaption=\"For Company's Name\" xAxisName=\"Time\" yAxisMinValue=\"0\" yAxisName=\"Price\" decimalPrecision=\"5\" formatNumberScale=\"0\" numberPrefix=\"\" showNames=\"1\" showValues=\"0\" showAlternateHGridColor=\"1\" AlternateHGridColor=\"ff5904\" divLineColor=\"ff5904\" divLineAlpha=\"20\" alternateHGridAlpha=\"5\">\n")
cat(sprintf(" <set name=\"%s\" value=\"%f\" hoverText = \"The difference from last value = %d\" ></set>\n", df$XTimeStamp, df$SharePrice, diff))
我正在以FusionChart Free可以读取的格式创建一个新的xml文件,我正在尝试将消息放入hoverText区域。但是,当我运行命令时,我收到以下错误:
Error in sprintf(" <set name=\"%s\" value=\"%f\" hoverText = \"The difference from last value = %d\" ></set>\n", :
arguments cannot be recycled to the same length
当我查看差异时,它比SharePrice少一个差异(显然因为从第1点到第1点的差异为零),所以如何在sprint函数中解释这一点(如果我生成正确格式的xml文件省略差异?
答案 0 :(得分:2)
要么摆脱df$XTimeStamp
和df$SharePrice
的第一个元素,要么添加NA
作为diff
的第一个元素。
cat(sprintf(" <set name=\"%s\" value=\"%f\" hoverText = \"The difference from last value = %d\" ></set>\n", df$XTimeStamp[-1], df$SharePrice[-1], diff))
或
cat(sprintf(" <set name=\"%s\" value=\"%f\" hoverText = \"The difference from last value = %d\" ></set>\n", df$XTimeStamp, df$SharePrice, c(NA,diff)))
答案 1 :(得分:1)
另外,如果你正在进行大量的这种模板填充,请查看CRAN上的'brew'包 - 与原始问题无关,但每当我看到一个丑陋的长序列'cat'时函数调用我只需要提一下。