tags:

views:

75

answers:

3

Hi All,

I am having trying to cat different strings to a file for different conditions using if - else statements:

cat("<graph caption=\"ECG Data Wave\" subcaption=\"For Person's Name\"  xAxisName=\"Time\" yAxisMinValue=\"-0.025\" yAxisName=\"Voltage\" decimalPrecision=\"5\"  formatNumberScale=\"0\" numberPrefix=\"\" showNames=\"1\" showValues=\"0\"  showAlternateHGridColor=\"1\" AlternateHGridColor=\"ff5904\" divLineColor=\"ff5904\"  divLineAlpha=\"20\" alternateHGridAlpha=\"5\">\n")
    if (df$BP > 140 && df$Yvoltage < -0.05){  
    cat(sprintf("    <set name=\"%s\" value=\"%f\" hoverText = \"Blood Pressure High, Level of Activity Low\" ></set>\n", df$XTimeStamp, df$YVoltage, diff))else
        if (df$BP > 140 && df$Yvoltage > -0.05) 
            cat(sprintf("    <set name=\"%s\" value=\"%f\" hoverText = \"Blood Pressure Normal, Level of Activity Normal\" ></set>\n", df$XTimeStamp, df$YVoltage, diff)) else
                 if (df$BP > 140 && df$Yvoltage > -0.35)
                    cat(sprintf("    <set name=\"%s\" value=\"%f\" hoverText = \"Blood Pressure High, Level of Activity High\" ></set>\n", df$XTimeStamp, df$YVoltage, diff))}
    cat(' <trendlines>
       <line startvalue="0.30" displayValue="High Activity" color="FF0000" thickness="1" isTrendZone="0"></line>
       <line startvalue="-0.05" displayValue="Low Activity" color="009999" thickness="1"   isTrendZone="0"></line>

\n') cat ("\n")

When I try to run the code I keep getting an error as follows:

Error: unexpected 'else' in:
"if (df$BP > 140 && df$Yvoltage < -0.05){  
 cat(sprintf("    <set name=\"%s\" value=\"%f\" hoverText = \"Blood Pressure High, Level of Activity Low\" ></set>\n", df$XTimeStamp, df$YVoltage, diff))else"

Any ideas where I am going wrong? Is this the right way do this are is there a better way to write the file?

I am trying to follow Spacedman's advice and use brew. I have tried different variances of it and I either get the following error message:

Error: unexpected input in "cat (sprintf("    <set name=\"%s\" value=\"%f\" hoverText = "<%= hovertext =>" ></set>\n", df$XTimeStamp, df$YVoltage))"

or if I get it working the hovertext remains the same the whole way down.

Currently the R code is as follows:

if(df$BP > 140 && df$YVoltage < -0.05){
    hovertext ="Blood Pressure High, Level of Activity Low"
}else{
   if(df$BP < 140 && df$BP > 90 && df$YVoltage > -0.05 && df$YVoltage < 0.30 ){
    hovertext ="Blood Pressure normal, Level of Activity normal"
      } else {
    hovertext ="Blood Pressure High, Level of Activity high"
       }

sink (file="c:/Users/usr/Desktop/data.xml", type="output",split=FALSE)
cat("<graph caption=\"ECG Data Wave\" subcaption=\"For Person's Name\" xAxisName=\"Time\" yAxisMinValue=\"-0.025\" yAxisName=\"Voltage\" 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 = "<%= hovertext =>" ></set>\n", df$XTimeStamp, df$YVoltage))
cat(' <trendlines>   
    <line startvalue="0.30" displayValue="High Activity" color="FF0000" thickness="1" isTrendZone="0"></line>
   <line startvalue="-0.05" displayValue="Low Activity" color="009999" thickness="1" isTrendZone="0"></line>
</trendlines>\n')
cat ("</graph>\n")
unlink("data.xml")

Any ideas on where I am still going wrong in the code?

+2  A: 

No closing end bracket (}) before the else.

For nested if, you should use:

if (test) {statement} else {if (test) {statement} else {...}}

Also, you seem to be referencing a data.frame column in your if statement. If this has nrow > 1, only the first element will be used. For a vectorised version, use ifelse, but I'm not sure this would give exactly what you want, you may have write a loop.

James
@James: Changed `length > 1` to `nrow > 1`. For a data frame, length returns the number of columns, but the if statement goes wrong when there are multiple rows.
Richie Cotton
@Richie: Thanks, I was refering the the column of the data frame but can see how this is unclear.
James
+1  A: 

Also, yucky code. Try using the "brew" package (on CRAN) for templating. It keeps things simple, saves you a zillion backslashes for quotes in your template strings, and forces you to take large chunks of logic out of template strings and into your application which is where it belongs.

For example, your first if-else mess just decides what hovertext to use. It would be better as:

if(foo){
  hovertext = "first hover text"
}else{
   if(foobar){
      hovertext = "second hovertext"
   } else {
     hovertext = "third hovertext"
   }
}

Then put hovertext in your brew template file:

...
<set name="<%= df$XTimeStamp %>" value="<%= df$YVoltage=>" hoverText = "<%= hovertext =>" ></set>
...

Also also, your sprintfs have two placeholders (%-markers) but three variables. What's the extra 'diff' on the end for? Another reason to use brew!

Spacedman
Anthony Keane
Sorted the not meaningful for factors error message. Also worked on the following string: cat(sprintf(" <set name=\"%s\" value=\"%f\" hoverText = \" <%= hovertext =>\" ></set>\n", df$XTimeStamp, df$YVoltage)) However when I run it I get the following error message: Error in sprintf(" <set name=\"%s\" value=\"%f\" hoverText = \" <%= hovertext =>\" ></set>\n", : too few arguments
Anthony Keane
A: 

Better use XML package to convert your data to XML. Yet the problem itself has been spotted by James.

mbq