# ---------------------------------------------------------------
#
# Author : Damian Skrzypiec <damian.j.skrzypiec@gmail.com>
#
# Date : 2017-12-29 23:37:22
#
# Description: This script contains functions for
# analysing structure of files and dirs of given dir.
#
# ---------------------------------------------------------------
#' R Files Representation
#'
#' Function \code{getRFilesTree} returns \code{RFilesTree} S3 object
#' which repressents structure of folders and R scripts of given path.
#'
#' @usage getRfilesTree(path)
#'
#' @param path A character of path to directory with R project.
#' Default value is current working directory path \code{getwd()}.
#'
#' @return A S3 object of class \code{RFilesTree}.
#'
#' @examples
#' getRfilesTree(getwd())
#'
#' \dontrun{
#' getRfilesTree("C:/SomeRProjectDir")
#' }
#'
#' @export
getRfilesTree <- function(path = getwd())
{
result <- getRfilesTree_Implement(path)
class(result) <- "RFilesTree"
return(result)
}
#' Overloaded print method for \code{RFilesTree}
#'
#' @usage print(getRfilesTree(path))
#'
#' @examples
#' \dontrun{
#' x <- getRfilesTree(path)
#' x # print.RFilesTree invoked
#'}
#' @export
print.RFilesTree <- function(object, indentWidth = 2)
{
for (rfileName in object$.RFiles)
{
message(addNSpacesBefore(msg = paste0("-", rfileName),
nSpace = indentWidth))
}
special <- c(".RFiles", ".Path")
for (dirName in names(object)[!names(object) %in% special])
{
message(addNSpacesBefore(dirName, indentWidth))
print.RFilesTree(object[[dirName]], indentWidth * 2)
}
}
getRfilesTree_Implement <- function(path = getwd())
{
isValidPath(path)
rFilesTree <- list()
rFilesTree$.RFiles <- getRFiles(path)
rFilesTree$.Path <- path
for (folderName in getSubFolders(path))
{
currFolderName <- getLastEleOfPath(folderName)
nextPath <- appendPath(path, currFolderName)
rFilesTree[[currFolderName]] <- getRfilesTree(nextPath)
}
return(rFilesTree)
}
addNSpacesBefore <- function(msg, nSpace = 2)
{
isCharacter(msg)
isNumeric(nSpace)
nSpace <- as.integer(nSpace)
spaces <- paste0(rep(" ", nSpace), collapse = "")
return(paste0(spaces, msg))
}
is.file <- function(objectName)
{
isCharacter(objectName)
splitByDot <- strsplit(x = objectName, split = ".", fixed = TRUE)
return(length(splitByDot[[1]]) == 2)
}
is.dir <- function(path)
{
isCharacter(path)
return(file.info(path)$isdir)
}
is.Rfile <- function(objectName)
{
isCharacter(objectName)
splitByDot <- strsplit(x = objectName, split = ".", fixed = TRUE)
if (!is.file(objectName)) {
return(FALSE)
}
if (splitByDot[[1]][2] != "R") {
return(FALSE)
}
return(TRUE)
}
getRFiles <- function(path = getwd())
{
isValidPath(path)
files <- dir(path)
rFilesIds <- which(as.logical(sapply(files, is.Rfile)))
return(files[rFilesIds])
}
getSubFolders <- function(path = getwd())
{
isValidPath(path)
if (length(dir(path)) == 0) {
return(character(0))
}
files <- appendPath(path, dir(path))
folders <- which(as.logical(sapply(files, is.dir)))
return(files[folders])
}
appendPath <- function(path, folder)
{
isValidPath(path)
isCharacter(folder)
if (isWindowsOS()) {
return(paste0(path, "/", folder))
}
if (isLinuxOS()) {
return(paste0(path, "/", folder)) # Make sure for path in Linux
}
# TODO: make case for iOS
}
getLastEleOfPath <- function(path)
{
isValidPath(path)
parts <- strsplit(x = path, split = "/", fixed = TRUE)[[1]]
return(parts[length(parts)])
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.