2010-09-09 4 views
2

je le fichier XML suivant:Autoriser l'utilisateur à sélectionner la plage de fichier de données à analyser?

<Company > 
    <shareprice> 
     <timeStamp> 12:00:00.01</timeStamp> 
     <Price> 25.02</Price> 
    </shareprice> 

    <shareprice> 
     <timeStamp> 12:00:00.02</timeStamp> 
     <Price> 15</Price> 
    </shareprice> 

    <shareprice> 
     <timeStamp> 12:00:01.025</timeStamp> 
     <Price> 15.02</Price> 
    </shareprice> 

    <shareprice> 
     <timeStamp> 12:00:01.031</timeStamp> 
     <Price> 18.25</Price> 
    </shareprice> 

    <shareprice> 
     <timeStamp> 12:00:01.039</timeStamp> 
     <Price> 18.54</Price> 
    </shareprice> 

    <shareprice> 
     <timeStamp> 12:00:01.050</timeStamp> 
     <Price> 16.52</Price> 
    </shareprice> 

    <shareprice> 
     <timeStamp> 12:00:02.01</timeStamp> 
     <Price> 17.50</Price> 
    </shareprice> 

    <shareprice> 
     <timeStamp> 12:00:03.01</timeStamp> 
     <Price> 25.02</Price> 
    </shareprice> 

    <shareprice> 
     <timeStamp> 12:00:05.02</timeStamp> 
     <Price> 30</Price> 
    </shareprice> 

    <shareprice> 
     <timeStamp> 12:00:11.025</timeStamp> 
     <Price> 32.25</Price> 
    </shareprice> 

    <shareprice> 
     <timeStamp> 12:00:12.031</timeStamp> 
     <Price> 26.05</Price> 
    </shareprice> 

    <shareprice> 
     <timeStamp> 12:00:15.039</timeStamp> 
     <Price> 18.54</Price> 
    </shareprice> 

    <shareprice> 
     <timeStamp> 12:00:19.050</timeStamp> 
     <Price> 16.52</Price> 
    </shareprice> 

    <shareprice> 
     <timeStamp> 12:01:02.01</timeStamp> 
     <Price> 17.50</Price> 
    </shareprice> 
</Company> 

Et j'ai aussi le Code R suivant:

library (ggplot2) 
library (XML) 
df <- xmlToDataFrame(file.choose()) 
df$timeStamp <- strptime(as.character(df$timeStamp), "%H:%M:%OS") 
df$Price <- as.numeric(as.character(df$Price)) 
sapply(df, class)   
options("digits.secs"=3) 
summary (df)    
df$timeStamp <- df[1,"timeStamp"] + cumsum(runif(1:length(df$timeStamp))*60) 
summary(df) 
diff1 = 0 
diff <- append(diff1,diff(df$Price)) 
summary (df$Price) 
Ymin <- min(df$Price) 
Ymax <- max(df$Price) 
Ymedian <- median (df$Price) 
Ymean <- mean(df$Price) 
Ysd <- sd (df$Price) 
sink (file="c:/xampp/htdocs/Sharedata.xml", type="output",split=FALSE) 
cat("<graph caption=\"Share 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 = \"The difference from last value: %s\" ></set>\n", df$timeStamp, df$Price, diff)) 
cat ("</graph>\n") 
unlink("data.xml") 
sink (file="c:/xampp/htdocs/Sharesstatistics.xml", type="output",split=FALSE) 
cat (" <statistics>\n") 
cat (sprintf(" <mean>%s</mean>\n", Ymean)) 
cat (sprintf(" <sd>%s</sd>\n",Ysd)) 
cat (sprintf(" <min>%s</min>\n", Ymin)) 
cat (sprintf(" <median>%s</median>\n",Ymedian)) 
cat (sprintf(" <max>%s</max>\n", Ymax)) 
cat (" </statistics>\n") 
unlink("statistics.xml") 
quit() 

Le code R fait tout ce que je veux et dois le faire sur le dossier complet. Ma question se rapporte à la façon de laisser l'utilisateur sélectionner une plage du fichier d'entrée à analyser au lieu du fichier complet, comment cela serait-il fait? Par exemple, si l'utilisateur veut juste les entrées 2 à 5 du fichier XML d'entrée et conserver la même sortie que celle définie par les instructions cat.

<shareprice> 
     <timeStamp> 12:00:00.02</timeStamp> 
     <Price> 15</Price> 
    </shareprice> 

    <shareprice> 
     <timeStamp> 12:00:01.025</timeStamp> 
     <Price> 15.02</Price> 
    </shareprice> 

    <shareprice> 
     <timeStamp> 12:00:01.031</timeStamp> 
     <Price> 18.25</Price> 
    </shareprice> 

Toute l'aide grandement appréciée.

Cordialement,

Anthony.

+0

J'ai ajouté le tag xml. –

Répondre

1

Cette question peut être facilement résolue simplement en lisant la trame de données, puis en demandant à l'utilisateur de donner une limite inférieure d'enregistrement en utilisant, par exemple, un balayage (n = 2). Voir aussi? Scan. Il vous permet de donner des informations de manière interactive, afin que l'utilisateur puisse choisir quoi faire. C'est un cas pour entrer une plage de données à utiliser.

x <- scan(n=2) 
id <- min(x):max(x) 

df2 <- df[id,] 

Si vous voulez lire uniquement les champs obligatoires d'une très grande table XML, c'est une autre histoire. Je ne pouvais pas penser à une fonction intégrée pour faire cela, donc vous auriez à faire quelque chose comme suit:

# function reads a subset of an xml file, 
# assuming a white line is dividing the individual records. 
# n is a vector containing the record numbers wanted 

subset.xml <- function(x,n,...){ 
    # set a range if n is just a number 
    if (length(n)==1) n <- 1:n 

    #initiate vars 
    skp <- 0 # the number of lines to skip by scan 
    count <- 1 
    out <- character(1) 

    repeat{ 
     tmp <- scan(x,what=character(0),n=1,skip=skp,blank.lines.skip=F,sep="\n") 
     skp <- skp+1 
     if(length(tmp)==0) {break} # no more input 

     if((count %in% n) & (tmp !="")) out <- paste(out,tmp,sep="\n") 
     if(tmp=="") count <- count+1 # white line seperates records 
    } 
    out <- substring(out,3) 
    out <- paste("<Data>",out,"</Data>",sep="\n") 
    return(xmlToDataFrame(xmlParse(out))) 
} 

df <- subset.xml("test.xml",2:4) 
> df 
     timeStamp Price 
1 12:00:00.02  15 
2 12:00:01.025 15.02 
3 12:00:01.031 18.25 
Questions connexes