extras/PackageMaintenance.R

# Copyright 2022 Observational Health Data Sciences and Informatics
#
# This file is part of ROhdsiWebApi
#
# 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.

# git actions
usethis::git_vaccinate()


# Format and check code
OhdsiRTools::formatRFolder("R")
OhdsiRTools::checkUsagePackage("ROhdsiWebApi")
OhdsiRTools::updateCopyrightYearFolder()
devtools::spell_check()

# Create manual and vignette
unlink("extras/ROhdsiWebApi.pdf")
shell("R CMD Rd2pdf ./ --output=extras/ROhdsiWebApi.pdf")

dir.create("inst/doc", recursive = TRUE, showWarnings = FALSE)
rmarkdown::render("vignettes/UsingROhdsiWebApi.Rmd",
                  output_file = "../inst/doc/UsingROhdsiWebApi.pdf",
                  rmarkdown::pdf_document(latex_engine = "pdflatex",
                                          toc = TRUE,
                                          number_sections = TRUE))



dir.create("inst/doc", recursive = TRUE, showWarnings = FALSE)
rmarkdown::render("vignettes/authenticationSecurity.Rmd",
                  output_file = "../inst/doc/authenticationSecurity.pdf",
                  rmarkdown::pdf_document(latex_engine = "pdflatex",
                                          toc = TRUE,
                                          number_sections = TRUE))



dir.create("inst/doc", recursive = TRUE, showWarnings = FALSE)
rmarkdown::render("vignettes/InsertCohortDefinitionsIntoPackage.Rmd",
                  output_file = "../inst/doc/InsertCohortDefinitionsIntoPackage.pdf",
                  rmarkdown::pdf_document(latex_engine = "pdflatex",
                                          toc = TRUE,
                                          number_sections = TRUE))

# unlink("inst/doc/UsingROhdsiWebApi.tex")
# rmarkdown::render("extras/README.Rmd",
#                   output_file = "../README.md",
#                   clean = TRUE)
# unlink("README.tex")
# rmarkdown::render("extras/NEWS.Rmd",
#                   output_file = "../NEWS.md",
#                   clean = TRUE)
# unlink("NEWS.tex")

pkgdown::build_site()
OhdsiRTools::fixHadesLogo()

# # Generate functions from template ----------------------
# library(dplyr)
# arguments <- ROhdsiWebApi:::.getStandardCategories()
#
# writeLines(text = readLines("extras/templateAutogeneratedFunctionScripts.R"),
#            con = "R/AutoGeneratedDefinitions.R")
#
# for (i in (1:nrow(arguments))) { #i = 1
#   argument <- arguments %>% dplyr::slice(i)
#
#   code <- readLines("extras/templateWebApiFunctions.R")
#   if (argument$categoryStandard %in% c('cohort', 'incidenceRate', 'pathway', 'characterization')) {
#     code <- c(code, readLines("extras/templateWebApiFunctionsGeneratableCategories.R"))
#   }
#   code <- stringr::str_replace_all(string = code, pattern = "%category%", replacement = argument$categoryStandard)
#   code <- stringr::str_replace_all(string = code, pattern = "%categoryFirstUpper%", replacement = argument$categoryFirstUpper)
#
#   sink(file = "R/AutoGeneratedDefinitions.R",
#        append = TRUE,
#        type = "output")
#   writeLines(code)
#   sink()
# }
#
# OhdsiRTools::formatRFile("R/AutoGeneratedDefinitions.R")


### test that code
arguments <- ROhdsiWebApi:::.getStandardCategories()
writeLines(text = readLines("extras/templateAutogeneratedTestFunctionScripts.R"),
           con = "tests/testthat/test-webapi.R")

for (i in (1:nrow(arguments))) { #i = 1
  argument <- arguments %>% dplyr::slice(i)

  code1 <- '
  ### autogenerated code - do not edit by hand - negative test
  testthat::test_that(desc = "Test get%categoryFirstUpper%DefinitionsMetaData (negative test)", code = {
    testthat::skip_if(baseUrl == "")
    value <- ROhdsiWebApi::get%categoryFirstUpper%DefinitionsMetaData(baseUrl = baseUrl)
    testthat::expect_s3_class(object = value, class = "tbl")
    testthat::expect_s3_class(object = value$createdDate, class = "POSIXct")
  })

  ### autogenerated code - do not edit by hand - negative test
  testthat::test_that(desc = "Test isValid%categoryFirstUpper%Id (negative test)", code = {
    testthat::skip_if(baseUrl == "")
    value <- ROhdsiWebApi::isValid%categoryFirstUpper%Id(baseUrl = baseUrl, %category%Ids = c(0,-1))
    testthat::expect_type(object = value, type = "logical")
    testthat::expect_setequal(object = value, expected = c(FALSE, FALSE))
  })

  ### autogenerated code - do not edit by hand - negative test
  testthat::test_that(desc = "Test delete%categoryFirstUpper%Definition (negative test)", code = {
    testthat::skip_if(baseUrl == "")
    if ("cohort" %in% "%category%") {
      testthat::expect_null(object = ROhdsiWebApi::delete%categoryFirstUpper%Definition(baseUrl = baseUrl, %category%Id = -1))
    } else {
      testthat::expect_error(object = ROhdsiWebApi::delete%categoryFirstUpper%Definition(baseUrl = baseUrl, %category%Id = -1))
    }
  })

  ### autogenerated code - do not edit by hand - negative test
  testthat::test_that(desc = "Test exists%categoryFirstUpper%Name (negative test)", code = {
    testthat::skip_if(baseUrl == "")
    testthat::expect_false(object = ROhdsiWebApi::exists%categoryFirstUpper%Name(baseUrl = baseUrl,
          %category%Name = paste0("test--this name should not exist-",
                                      paste0(sample(letters, 14, replace = TRUE), collapse = ""))))

  })

  ### autogenerated code - do not edit by hand - negative test
  testthat::test_that(desc = "Test detect%categoryFirstUpper%sByName (negative test)", code = {
    testthat::skip_if(baseUrl == "")
    response <- ROhdsiWebApi::detect%categoryFirstUpper%sByName(baseUrl = baseUrl, pattern = "", negate = TRUE)
    testthat::expect_false(object = response)
  })

  '



  code1 <- stringr::str_replace_all(string = code1, pattern = "%category%", replacement = argument$categoryStandard)
  code1 <- stringr::str_replace_all(string = code1, pattern = "%categoryFirstUpper%", replacement = argument$categoryFirstUpper)

  sink(file = "tests/testthat/test-webapi.R",
       append = TRUE,
       type = "output")
  writeLines(code1)
  sink()
}

## Create ROhdsiWebApi's internal package data ---------------------
library(dplyr)

StandardCategories <- tibble(categoryStandard = c("conceptSet", "cohort", "incidenceRate", "estimation", "prediction", "characterization", "pathway")) %>% 
  mutate(categoryFirstUpper = paste0(toupper(substr(.data$categoryStandard, 1, 1)), 
                                     substr(.data$categoryStandard, 2, nchar(.data$categoryStandard)))) %>% 
  mutate(categoryUrl = case_when(categoryStandard == "conceptSet" ~ "conceptset", 
                                               categoryStandard == "cohort" ~ "cohortdefinition", 
                                               categoryStandard == "characterization" ~ "cohort-characterization", 
                                               categoryStandard == "pathway" ~ "pathway-analysis", 
                                               categoryStandard == "incidenceRate" ~ "ir", 
                                               categoryStandard == "estimation" ~ "estimation", 
                                 categoryStandard == "prediction" ~ "prediction", TRUE ~ "")) %>% 
  mutate(categoryUrlGetExpression = case_when(categoryStandard == "conceptSet" ~ "expression", 
                                              categoryStandard == "characterization" ~ "design", TRUE ~ "")) %>%
  mutate(categoryUrlGenerationInformation = case_when(categoryStandard == "cohort" ~ "info", 
                                                      categoryStandard == "characterization" ~ "generation", 
                                                      categoryStandard == "pathway" ~ "generation", 
                                                      categoryStandard == "incidenceRate" ~ "info", TRUE ~ "")) %>% 
  mutate(categoryUrlGeneration = case_when(categoryStandard == "cohort" ~ "generate", 
                                           categoryStandard == "characterization" ~ "generation", 
                                           categoryStandard == "pathway" ~ "generation", 
                                           categoryStandard == "incidenceRate" ~ "execute", 
                                           TRUE ~ "")) %>% 
  mutate(categoryUrlCancel = dplyr::case_when(categoryStandard == "cohort" ~ "cancel", 
                                              categoryStandard == "characterization" ~ "generation", 
                                              categoryStandard == "pathway" ~ "generation", 
                                              categoryStandard == "incidenceRate" ~ "execute", 
                                              TRUE ~ "")) %>% 
  mutate(categoryUrlPut = dplyr::case_when(categoryStandard == "conceptSet" ~ "items", TRUE ~ "")) %>% 
  mutate(categoryUrlPostExpression = dplyr::case_when(categoryStandard == "conceptSet" ~ "items", 
                                                      categoryStandard == "cohort" ~ "", 
                                                      categoryStandard == "characterization" ~ "import", 
                                                      categoryStandard == "pathway" ~ "", 
                                                      categoryStandard == "incidenceRate" ~ "", 
                                                      TRUE ~"")) 

usethis::use_data(StandardCategories, internal = TRUE, overwrite = TRUE)
OHDSI/ROhdsiWebApi documentation built on March 15, 2023, 1:23 p.m.