File: registry.R

package info (click to toggle)
r-cran-nmf 0.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,344 kB
  • sloc: cpp: 680; ansic: 7; makefile: 2
file content (96 lines) | stat: -rw-r--r-- 3,974 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
###% Define access/setup methods for NMF package registry.
###% 
###% The registry is used to provide a common interface to NMF methods (algorithms, seeding methods, distance, ...).
###% It enables the user to add custom methods which will be accessible in the same way as the built-in ones.
###% 
###% @author Renaud Gaujoux
###% @created 22 Jul 2009 


###########################################################################
# COMMON REGISTRY
###########################################################################

#' @import pkgmaker
#' @import registry
nmfRegistry <- function(...) pkgmaker::packageRegistry(...)

# Returns the names of all the packages that contibute to all or a given
# package's primary registry  
registryContributors <- function(package, regname = NULL){
    regs <- packageRegistries(regname = regname, package = package, primary = TRUE)
    if( length(regs) ) unique(names(unlist(lapply(paste0(package, '::', regs), packageRegistries))))
}

###% Return a method stored in the NMF registry.
###% 
###% @param name the key (a character string) of the method to be retrieved
###% @param regname the name of the sub-registry where to look for the \code{key}
###% @param exact a boolean. When set to \code{TRUE} the key is searched exactly, otherwise (default) the key
###% is matched partially with the keys registered in the registry.
###% @param error a boolean. When set to \code{TRUE} (default) the function will raise an error if the key is not found.
###% Otherwise it will not raise any error and return \code{NULL}.
###%
nmfGet <- function(regname, name=NULL, ...){
	
	# retrieve from the given package's sub-registry
	pkgmaker::pkgreg_fetch(regname, key=name, ...)
	
}

###% Register a NMF method so that it is accessible via the common interface defined by the \code{nmf} function.
###% @param method an NMFStrategy object or a function that defines the method
###% @param key a non-empty character string that will be used as an identifier to access the method
###% @param overwrite a boolean that specify if an existing method (i.e. with exactly the same \code{key}) should be overwritten or not.
###% If \code{FALSE} and a method with the same key exists, an error will be thrown.
###% @param save [Not used] a boolean that if set to \code{TRUE} will save in database so that it is available in other R sessions.
###% @param ... [Not used]
###%
###% @return \code{TRUE} invisibly in case of success.
###%
###% @seealso nmf
###%
setGeneric('nmfRegister', function(key, method, ...) standardGeneric('nmfRegister') )
setMethod('nmfRegister', signature(key='character'), 
	function(key, method, regname, ...){		
		#TODO: add functionality to save the registered strategy into a file for use is other R sessions
		
		parent.method <- attr(method, 'parent')
		tmpl <- if( !is.null(parent.method) && parent.method != key ){
			str_c(" based on template '", parent.method, "'")
		}
		setPackageRegistryEntry(regname, key, method, ..., where='NMF', msg=tmpl)
	}
)

####% Unregister a NMF method.
####%
####% @param name the key of the method to unregister [character string]
####%
#nmfUnregister <- function(name, regname, quiet=FALSE){				
#	
#	return( pkgreg_remove(regname, key=name, quiet=quiet) )
#	# add the strategy to the registry
#	obj <- nmfGet(name, exact=TRUE, error=FALSE, regname=regname)
#	regentry <- nmfRegistry(regname, entry=TRUE)
#	registry <- regentry$regobj
#	objtype <- regentry$entrydesc
#	
#	if( !is.null(obj) ){
#		# get the method registry and the method's fullname
#		name <- attr(strategy, 'name')
#		
#		if( !quiet ){
#			msg <- paste0("Removing ", objtype," '", name, "' from registry '", regname, "' [", class(obj), ']')
#			message(msg, ' ... ', appendLF=FALSE)
#		}
#		# delete from registry
#		registry$delete_entry(name)
#		if( !quiet ) message('OK')
#		TRUE
#	}else{
#		if( !quiet )
#			warning("Could not remove ", objtype, " '", name, "': no matching registry entry.", call.=FALSE)
#		FALSE
#	}
#}