R/createNewPackage.R

Defines functions createPackage

Documented in createPackage

# @file WebApi
#
# Copyright 2020 Observational Health Data Sciences and Informatics
#
# This file is part of StudyManagement
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#' creates a new study package
#'
#' @details
#' creates a new study package
#'
#' @param path          path where to create the package
#' @param packageName   name of package
#'
#' @return
#' creates a package skeleton
#'
#'
#' @export
createPackage <- function(path, packageName) {

  temp <- getwd()
  currentPackagePath <- system.file(package = 'StudyManagement')

  targetPackagePath <- paste0(path, '/', packageName)
  if (dir.exists(targetPackagePath)) {unlink(targetPackagePath, recursive = TRUE, force = TRUE)}
  dir.create(path = targetPackagePath, showWarnings = FALSE)
  setwd(targetPackagePath)


  # authors <- paste0('person(given = "', Sys.getenv("USERNAME"),
  #                   '", family = "', Sys.getenv("USERNAME"),
  #                   '", email = "', Sys.getenv("USERNAME"), '@its.jnj.com"',
  #                   "role = c(\"aut\", \"cre\")"
  #                   )
  usethis::create_package(
    path = targetPackagePath,
    fields =  list(Language = "en",
                   License = "JnJ"
                   #, `Authors@R` = authors
                   ),
    rstudio = rstudioapi::isAvailable(),
    open = FALSE
  )



  # package to consider as required: 'spelling'
  requiredPackages <-
    c(
      'ggplot2',
      'magrittr',
      'scales',
      'SqlRender',
      'CohortDiagnostics',
      'DatabaseConnector',
      'DBI',
      'dplyr',
      'jsonlite',
      'officer',
      'openxlsx',
      'purrr',
      'readr',
      'renv',
      'RSQLite',
      'rstudioapi',
      'stringr',
      'StudyManagement',
      'tibble'
    )

  .makePackagesRequired <- function(package) {
    usethis::use_package(package = package, type = "Imports")
  }

  for (i in (1:length(requiredPackages))) {
    package <- requiredPackages[i]
    .makePackagesRequired(package = package)
  }
  # usethis::use_spell_check(vignettes = TRUE, lang = "en-US", error = FALSE)
  # usethis::use_citation()
  # usethis::use_tibble()
  # usethis::create_tidy_package()
  # usethis::use_tidy_description()
  usethis::use_blank_slate()

  foldersToCreate <- c('extras', 'utils', 'data', 'data-raw', 'man',
                       'inst', 'inst/AUTHOR', 'inst/COPYRIGHT', 'inst/CITATION',
                       'inst/docs', 'inst/extdata',
                       'material', 'specifications',
                       'specifications/json', 'specifications/json/conceptSets',
                       'specifications/json/cohortDefinitions',
                       'specifications/json/characterization',
                       'specifications/json/estimation',
                       'specifications/json/incidenceRates',
                       'specifications/json/cohortPathways',
                       'specifications/json/prediction',
                       'specifications/json/configuration',
                       'specifications/json/dataSources',
                       'specifications/executables',
                       'specifications/executables/estimation',
                       'specifications/executables/prediction',
                       'results',
                       'results/cohortDefinition',
                       'results/cohortDefinition/json',
                        'results/characterization',
                        'results/characterization/json',
                        'results/cohortPathways',
                        'results/cohortPathways/json',
                        'results/incidenceRates',
                        'results/incidenceRates/json',
                        'results/estimation',
                        'results/estimation/json',
                        'results/prediction',
                        'results/prediction/json')

  .createFolders <- function(folder) {
    dir.create(path = paste0(targetPackagePath, '/', folder))
    file.create(path = paste0(targetPackagePath, '/', folder, '/empty.gitkeep'), showWarnings = FALSE)
  }

  for (i in (1:length(foldersToCreate))) {
    folder <- foldersToCreate[i]
    .createFolders(folder)
  }

  # SQLItePath <- paste0(targetPackagePath, '/data-raw/ohdsi_results.sqlite')
  # ohdsi_results <- RSQLite::dbConnect(RSQLite::SQLite(), SQLItePath)
  # RSQLite::dbDisconnect(ohdsi_results)
  #
  # SQLItePath <- paste0(targetPackagePath, '/data-raw/comparativeEffectivnessDataModel.sqlite')
  # comparativeEffectivnessDataModel <- RSQLite::dbConnect(RSQLite::SQLite(), SQLItePath)
  # RSQLite::dbDisconnect(comparativeEffectivnessDataModel)


  .sinkToFile <- function(currentPackagePath, localPath, file) {
    string <- readr::read_file(paste0(currentPackagePath, "/template/Package/", localPath, "/", file))
    file.create(path = paste0(targetPackagePath, "/" , localPath, "/", file), showWarnings = FALSE)
    sink(paste0(targetPackagePath, "/" , localPath, "/", file))
    cat(string)
    sink()
  }

  .sinkToFile(currentPackagePath = currentPackagePath,
             localPath = "specifications",
             file = "atlasIds.R")

  .sinkToFile(currentPackagePath = currentPackagePath,
              localPath = "specifications",
              file = "connections.R")

  .sinkToFile(currentPackagePath = currentPackagePath,
             localPath = "specifications",
             file = "dataSources.R")

  .sinkToFile(currentPackagePath = currentPackagePath,
             localPath = "specifications",
             file = "utils.R")

  .sinkToFile(currentPackagePath = currentPackagePath,
              localPath = "specifications",
              file = "colorPalette.R")

  .sinkToFile(currentPackagePath = currentPackagePath,
             localPath = "extras",
             file = "codeToRun.R")

  .sinkToFile(currentPackagePath = currentPackagePath,
             localPath = "extras",
             file = "copyOhdsiResultsFromRemoteToLocal.R")

  .sinkToFile(currentPackagePath = currentPackagePath,
             localPath = "extras",
             file = "getCdmSources.R")

  .sinkToFile(currentPackagePath = currentPackagePath,
             localPath = "extras",
             file = "getIncidenceRateDataUsingCohortDiagnostics.R")

  .sinkToFile(currentPackagePath = currentPackagePath,
              localPath = "extras",
              file = "getOhdsiResultsTables.R")

  setwd(temp)

}
gowthamrao/StudyManagement documentation built on March 9, 2020, 10:48 p.m.