Statistiques
| Branche: | Révision :

root / src / R / cachecache.R @ master

Historique | Voir | Annoter | Télécharger (2,05 ko)

1 c68acc42 Florent Chuffart
get_content = structure( function(## Get content from cached 
2 c68acc42 Florent Chuffart
### Access to the cached content of a file via the global variable CACHE_CACHE. Load content in CACHE_CACHE if needed.
3 5e1de699 Florent Chuffart
filename, ##<< The path to the file that contain dataset
4 5e1de699 Florent Chuffart
class=NULL, ##<< The class of dataset
5 4ef79560 Florent Chuffart
# FORCE_RELOAD=FALSE, ##<< Force cachecache to reload the value
6 c68acc42 Florent Chuffart
... ##<< Parameters that will be passed to my_read
7 c68acc42 Florent Chuffart
) {
8 c68acc42 Florent Chuffart
  if (inherits(try(CACHE_CACHE,TRUE), "try-error") || is.null(CACHE_CACHE)) {
9 c68acc42 Florent Chuffart
    CACHE_CACHE <<- list()
10 c68acc42 Florent Chuffart
  }
11 c68acc42 Florent Chuffart
  obj = list(filename=filename)
12 c68acc42 Florent Chuffart
  class(obj) = class
13 4ef79560 Florent Chuffart
  FORCE_RELOAD = FALSE
14 5e1de699 Florent Chuffart
  if (is.null(CACHE_CACHE[[obj$filename]]) | FORCE_RELOAD) {
15 c68acc42 Florent Chuffart
    print(paste("Loading file ",obj$filename, sep=""))
16 c68acc42 Florent Chuffart
		tmp_content = my_read(obj, ...)
17 c68acc42 Florent Chuffart
    print("affect it...")
18 c68acc42 Florent Chuffart
    CACHE_CACHE[[obj$filename]] <<- tmp_content
19 c68acc42 Florent Chuffart
    print("done.")
20 c68acc42 Florent Chuffart
  }  
21 c68acc42 Florent Chuffart
  return(CACHE_CACHE[[obj$filename]])
22 c68acc42 Florent Chuffart
### Returns the cached content of the current object. 
23 c68acc42 Florent Chuffart
}, ex=function(){
24 c68acc42 Florent Chuffart
	# Create a dataframe
25 c68acc42 Florent Chuffart
	df = data.frame(list(key1 = "value1", key2 = "value2"), stringsAsFactors=FALSE)
26 c68acc42 Florent Chuffart
	df = rbind(df, list(key1 = "value1'", key2 = "value3'"))
27 c68acc42 Florent Chuffart
	# Dump it into tmp file
28 c68acc42 Florent Chuffart
	write.table(df, file="/tmp/tmp.dump.table.tmp")
29 c68acc42 Florent Chuffart
	# Load it into cache using feature.
30 c68acc42 Florent Chuffart
	# First time it will be load into cache
31 c68acc42 Florent Chuffart
	print(get_content("/tmp/tmp.dump.table.tmp", "table", stringsAsFactors=FALSE))
32 c68acc42 Florent Chuffart
	# Second time not
33 c68acc42 Florent Chuffart
	print(get_content("/tmp/tmp.dump.table.tmp", "table", stringsAsFactors=FALSE))
34 c68acc42 Florent Chuffart
	
35 c68acc42 Florent Chuffart
})
36 c68acc42 Florent Chuffart
	
37 c68acc42 Florent Chuffart
my_read = function(
38 c68acc42 Florent Chuffart
### Abstract my_read function.	
39 c68acc42 Florent Chuffart
	obj, ...) {
40 c68acc42 Florent Chuffart
  UseMethod("my_read", obj)
41 c68acc42 Florent Chuffart
}
42 c68acc42 Florent Chuffart
	
43 c68acc42 Florent Chuffart
my_read.default = function(
44 c68acc42 Florent Chuffart
### Default my_read function.	
45 c68acc42 Florent Chuffart
	obj, ...){
46 c68acc42 Florent Chuffart
  stop(paste("ERROR, my_read is not defined for any Objects (file ", obj$filename," )", sep=""))			
47 c68acc42 Florent Chuffart
}
48 c68acc42 Florent Chuffart
49 c68acc42 Florent Chuffart
my_read.table = function(
50 c68acc42 Florent Chuffart
### my_read function for table files.	
51 c68acc42 Florent Chuffart
	obj, ...){
52 c68acc42 Florent Chuffart
	if (rev(unlist(strsplit(obj$filename, ".", fixed=TRUE)))[1] == "gz") {
53 c68acc42 Florent Chuffart
	  return(read.table(file=gzfile(obj$filename), ...))			
54 c68acc42 Florent Chuffart
	} else { 
55 c68acc42 Florent Chuffart
	  return(read.table(file=obj$filename, ...))
56 c68acc42 Florent Chuffart
	}
57 c68acc42 Florent Chuffart
}	
58 c68acc42 Florent Chuffart
59 5f3f6be5 Florent Chuffart
my_read.csv = function(
60 c68acc42 Florent Chuffart
### my_read function for cvs files.	
61 c68acc42 Florent Chuffart
	obj, ...){
62 c68acc42 Florent Chuffart
  return(read.csv(file=obj$filename, ...))
63 c68acc42 Florent Chuffart
}