
#' @title Run the app
#' @description Run the app
#' @param app a character string with the name of the app. Currently only 'citizenair.Rmd' is allowed.
#' @return nothing to return
#' @export
#' @examples
#' \dontrun{
#' run_app()
#' run_app('citizenair.Rmd')
#' }
run_app <- function(app = c("citizenair.Rmd")){
  appfolder = tools::file_path_sans_ext(app)
  app <- match.arg(app)
  olddir <- getwd()
  on.exit({
    setwd(olddir)
  })

  # copy app to tempdir
  appdir <- tempdir()
  setwd(appdir)
  dirs <- setdiff(list.dirs(path = system.file("apps", package = "citizenair"), recursive = TRUE, full.names = FALSE), "")
  sapply(dirs, FUN=function(x) dir.create(x, recursive = TRUE, showWarnings = FALSE))
  copyme <- data.frame(from = list.files(path = system.file("apps", package = "citizenair"), full.names = TRUE, recursive = TRUE),
                       to = file.path(appdir, list.files(path = system.file("apps", package = "citizenair"), full.names = FALSE, recursive = TRUE)),
                       stringsAsFactors = FALSE)
  mapply(from = copyme$from, to = copyme$to, function(from, to){
    file.copy(from = from, to = to, overwrite = TRUE)
  })
  # run the app
  rmarkdown::run(sprintf("%s/%s", appfolder, app))
}
