R/edit_namespace.R

##---------------------------------------------------------------------------
## 
##---------------------------------------------------------------------------
export = function(name, path, comment = NULL, delete = FALSE)
{
	fd = file.path(path, "NAMESPACE")

	if(file.exists(fd) & delete) file.remove(fd)

	if(!file.exists(fd)) writeLines("# Generated by me", fd)

	tx1 = readLines(fd)


	tx2 = gsub("export\\(|\\)" ,"", tx1)
	w = name[!(name %in% tx2)]

	## insere commentários
	if(!is.null(comment) & length(w))
	{
		n = length(tx1) + 1
		tx1[n] = paste("\n##", comment)
	}#end if

	writeLines(tx1, con = fd)

	## insere nomes das funções
	#ni = unlist(sapply(name, grep, tx1))

	if(length(w))
	{
		n = length(tx1)
		for(i in 1:length(w))
		{
			tx1[n+i] = paste0("export(", w[i], ")")
		}
	}#end if

	writeLines(tx1, con = fd)

	return(tx1)
}#end function export_ns


##-------------------------------------------------------------
## 
##-------------------------------------------------------------
import = function(name, path, comment = NULL, delete = FALSE)
{
	fd = file.path(path, "NAMESPACE")

	if(file.exists(fd) & delete) file.remove(fd)

	if(!file.exists(fd)) writeLines("# Generated by me", fd)

	tx1 = readLines(fd)


	tx2 = gsub("import\\(|\\)" ,"", tx1)
	w = name[!(name %in% tx2)]

	## insere commentários
	if(!is.null(comment) & length(w))
	{
		n = length(tx1) + 1
		tx1[n] = paste("\n##", comment)
	}#end if

	writeLines(tx1, con = fd)

	## insere nomes das funções
	#ni = unlist(sapply(name, grep, tx1))

	if(length(w))
	{
		n = length(tx1)
		for(i in 1:length(w))
		{
			tx1[n+i] = paste0("import(", w[i], ")")
		}
	}#end if

	writeLines(tx1, con = fd)

	return(tx1)
}#end function export_ns

##---------------------------------------------------------------------------
## 
##---------------------------------------------------------------------------
importfrom = function(pkg, fun, path, comment = NULL, delete = FALSE)
{
	fp = paste(pkg, concatena(fun, sep=","), sep=",")

	fd = file.path(path, "NAMESPACE")
	
	if(file.exists(fd) & delete) file.remove(fd)

	if(!file.exists(fd)) writeLines("# Generated by me", fd)

	tx1 = readLines(fd)

	tx2 = gsub("importFrom\\(|\\)" ,"", tx1)
	w = fp[!(fp %in% tx2)]

	if(!is.null(comment) & length(w))
	{
		n = length(tx1) + 1
		tx1[n] = paste("\n##", comment)
	}#end if

	if(length(w))
	{
		n = length(tx1) + 1

		tx1[n] = paste0("importFrom(", fp, ")")
	}#end if

	writeLines(tx1, con = fd)

	return(tx1)
}#end function importFrom
salah31416/toolbox documentation built on June 3, 2019, 6:59 p.m.