mirror of
https://github.com/agdamsbo/REDCapCAST.git
synced 2026-06-19 05:07:30 +02:00
first commit to add shiny app for basic upload of dictionary and dataset
This commit is contained in:
parent
796826d7d9
commit
05c0f35016
12 changed files with 333 additions and 5 deletions
50
R/utils.r
50
R/utils.r
|
|
@ -490,3 +490,53 @@ is_repeated_longitudinal <- function(data, generics = c(
|
|||
}
|
||||
any(generics %in% names)
|
||||
}
|
||||
|
||||
|
||||
|
||||
#' Helper to import files correctly
|
||||
#'
|
||||
#' @param filenames file names
|
||||
#'
|
||||
#' @return character vector
|
||||
#' @export
|
||||
#'
|
||||
#' @examples
|
||||
#' file_extension(list.files(here::here(""))[[2]])[[1]]
|
||||
file_extension <- function(filenames) {
|
||||
sub(pattern = "^(.*\\.|[^.]+)(?=[^.]*)", replacement = "", filenames, perl = TRUE)
|
||||
}
|
||||
|
||||
#' Flexible file import based on extension
|
||||
#'
|
||||
#' @param file file name
|
||||
#' @param consider.na character vector of strings to consider as NAs
|
||||
#'
|
||||
#' @return tibble
|
||||
#' @export
|
||||
#'
|
||||
#' @examples
|
||||
#' read_input("https://raw.githubusercontent.com/agdamsbo/cognitive.index.lookup/main/data/sample.csv")
|
||||
read_input <- function(file, consider.na= c("NA", '""',"")){
|
||||
|
||||
ext <- file_extension(file)
|
||||
|
||||
tryCatch(
|
||||
{
|
||||
if (ext == "csv") {
|
||||
df <- readr::read_csv(file = file,na = consider.na)
|
||||
} else if (ext %in% c("xls", "xlsx")) {
|
||||
df <- openxlsx2::read_xlsx(file = file, na.strings = consider.na)
|
||||
} else if (ext == "dta"){
|
||||
df <- haven::read_dta(file = file)
|
||||
} else {
|
||||
stop("Input file format has to be either '.csv', '.xls' or '.xlsx'")
|
||||
}
|
||||
},
|
||||
error = function(e) {
|
||||
# return a safeError if a parsing error occurs
|
||||
stop(shiny::safeError(e))
|
||||
}
|
||||
)
|
||||
|
||||
df
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue