Hi, I recently discovered the R program and I thought it could be useful to me. I have to analyse data saved as .Px file (x between 0 and 8 - .P0 files have 18 lines at the beginning that I have to skip). New files are generated everyday.
This is my strategy : In order to analyse the data, I first want to copy the new data in a database in MySQL (which already contains the previous data). So the first task is to compare the list of the files in the directory (object : rfichiers) to the list of the files already saved (object : tfichiers). The list containing the new files is then given by nfichiers<-setdiff(rfichiers, tfichiers). It sounds easy ... ... but it doesn't work !!! Up to now, I'm am able to connect to MySQL and, if the file "tfichiers.r" doesn't exist, I can copy data files to the MySQL database. But if "tfichiers.r" already exists and there is no new file to save, it ignores the condition if (nfichiers!="0") and save all the files of the directory to the database. Is it a problem with the way I save tfichiers or is it a problem with the condition if (nfichiers!="0") ? Could you please give me some advices to correct my script (written with Tinn-R) ? I thank you in advance for your help. Have a nice week, Ptit Bleu. PS : Ptit Bleu means something like "Full Newbye" in french. So thanks to be patient :-) PPS : I hope you understand my french english ------------------------------------------ # Connexion a la base de donnees database de MySQL library(DBI) library(RMySQL) drv<-dbDriver("MySQL") con<-dbConnect(drv, username="user", password="password", dbname="database", host="localhost") # Creation des objets contenant la liste des fichiers (rel pour chemin relatif) # - dans le repertoire : objet rfichiers # - deja traites : objet tfichiers # - nouveaux depuis la derniere connexion : objet nfichiers # chemin est le repertoire de stockage des donnees # RWork est le repertoire de travail de R # sep='' pour eviter l'ajout d'un espace apres Mydata/ setwd("D:/RWork") chemin<-"d:/Mydata/" relrfichiers<-dir(chemin, pattern=".P") rfichiers<-paste(chemin,relrfichiers, sep='') if (file.exists("tfichiers.r")) { tfichiers<-load("tfichiers.r") nfichiers<-setdiff(rfichiers,tfichiers) } else { nfichiers<-rfichiers } # p0fichiers : fichiers avec l'extension .P0 (fichiers contenant des lignes d'infos à ne pas charger) # pxfichiers : fichiers avec les extensions P1, ..., P8 (sans infos au debut) if (nfichiers!="0") { p0fichiers<-nfichiers[grep(".P0", nfichiers)] pxfichiers<-setdiff(nfichiers, p0fichiers) # Fusion des colonnes jour et heure pour permettre de tracer des variations en fonction du temps # Chaque fichier contenu dans l'objet p0fichiers est chargé, en supprimant les 18 premieres lignes, # et on met dans l'objet jourheure la fusion de la colonne jour (V1) et de la colonne heure (V2) # L'objet jourheure est recopie dans la premiere colonne de donnees # On supprime ensuite la deuxieme colonne (contenant les heures) qui est maintenant superflue # L'objet donnees est copié dans la base de donnees MySQL Mydata # Remarque : R comprend le format jour/mois/annee - MySQL : annee/mois/jour -> stockage en CHAR dans MySQL for (i in 1:length(p0fichiers)) { donnees<-read.table(p0fichiers[i], quote="\"", sep=";", dec=",", skip=18) jourheure<-paste(donnees$V1, donnees$V2, sep=" ") donnees[1]<-jourheure donnees<-donnees[,-2] # assignTable(con, "Datatable", donnees, append=TRUE) - Ne marche pas dbWriteTable(con, "Datatable", donnees, append=TRUE) rm(donnees, jourheure) } # Idem avec les fichiers d'extension .Px en chargant toutes les lignes (skip=0) # Amelioration possible : creer une fonction avec en argument p0fichiers ou pxfichiers for (i in 1:length(pxfichiers)) { donnees<-read.table(pxfichiers[i], quote="\"", sep=";", dec=",", skip=0) jourheure<-paste(donnees$V1, donnees$V2, sep=" ") donnees[1]<-jourheure donnees<-donnees[,-2] # assignTable(con, "Datatable", donnees, append=TRUE) - Ne marche pas dbWriteTable(con, Datatable", donnees, append=TRUE) rm(donnees, jourheure) } } tfichiers<-rfichiers save(rfichiers, file="tfichiers.r", ascii=TRUE) rm(list=ls()) # Deconnexion à MySQL dbDisconnect(con) -- View this message in context: http://www.nabble.com/Problem-with-save-or-and-if-%28I-think-but-maybe-not-...%29-tf4333945.html#a12343236 Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.