clean up and ods multi sheet merge

This commit is contained in:
Andreas Gammelgaard Damsbo 2025-03-13 12:45:37 +01:00
parent 49016a4aa8
commit e3261ad328
No known key found for this signature in database

View file

@ -1,9 +1,3 @@
# library(htmltools)
# library(shiny)
# library(shinyWidgets)
# library(rlang)
# library(readxl)
#' @title Import data from a file #' @title Import data from a file
#' #'
#' @description Let user upload a file and import data #' @description Let user upload a file and import data
@ -242,46 +236,6 @@ import_file_server <- function(id,
} }
}) })
# output$sheet <- shiny::renderUI({
# if (is_workbook(input$file$datapath)) {
# if (isTRUE(is_excel(input$file$datapath))) {
# choices <- readxl::excel_sheets(input$file$datapath)
# } else if (isTRUE(is_ods(input$file$datapath))) {
# choices <- readODS::ods_sheets(input$file$datapath)
# }
# selected <- choices[1]
#
# shiny::selectInput(
# inputId = ns("sheet"),
# label = datamods:::i18n("Select sheet(s) to import:"),
# choices = choices,
# selected = selected,
# width = "100%",
# multiple = TRUE
# )
# # shinyWidgets::pickerInput(
# # inputId = ns("sheet"),
# # label = datamods:::i18n("Select sheet(s) to import:"),
# # choices = choices,
# # selected = selected,
# # width = "100%",
# # multiple = TRUE
# # )
# }
# })
# observeEvent(
# input$sheet,
# {
# req(input$file)
# if (is_workbook(input$file$datapath) && is.null(shiny::req(input$sheet))) {
# temporary_rv$data <- NULL
# }
# }
# )
observeEvent( observeEvent(
list( list(
input$file, input$file,
@ -456,18 +410,28 @@ import_xls <- function(file, sheet, skip, na.strings) {
} }
import_ods <- function(file, sheet, skip, na.strings) { import_ods <- function(file, sheet, skip, na.strings) {
readODS::read_ods( tryCatch(
path = file, {
sheet = sheet, sheet |>
skip = skip, purrr::map(\(.x){
na = na.strings readODS::read_ods(
path = file,
sheet = .x,
skip = skip,
na = na.strings
)
}) |>
purrr::reduce(dplyr::full_join)
},
warning = function(warn) {
showNotification(paste0(warn), type = "warning")
},
error = function(err) {
showNotification(paste0(err), type = "err")
}
) )
} }
# import_xls(openxlsx2::read_xlsx("~/freesearcheR/dev/Test data/trials_redcap_sheets.xlsx"),)
# list()
#' @title Create a select input control with icon(s) #' @title Create a select input control with icon(s)
#' #'
#' @description Extend form controls by adding text or icons before, #' @description Extend form controls by adding text or icons before,
@ -511,89 +475,89 @@ selectInputIcon <- function(inputId,
} }
#' Test app for the import_file module
#'
#' @rdname import-file_module
# library(shiny) #'
# library(datamods) #' @examples
#' \dontrun{
ui <- shiny::fluidPage( #' import_file_demo_app()
# theme = bslib::bs_theme(version = 5L), #' }
# theme = bslib::bs_theme(version = 5L, preset = "bootstrap"), import_file_demo_app <- function() {
shiny::tags$h3("Import data from a file"), ui <- shiny::fluidPage(
shiny::fluidRow( # theme = bslib::bs_theme(version = 5L),
shiny::column( # theme = bslib::bs_theme(version = 5L, preset = "bootstrap"),
width = 4, shiny::tags$h3("Import data from a file"),
import_file_ui( shiny::fluidRow(
id = "myid", shiny::column(
file_extensions = c(".csv", ".tsv", ".txt", ".xls", ".xlsx", ".rds", ".sas7bdat", ".ods", ".dta"), width = 4,
layout_params = "dropdown" # "inline" # or "dropdown" import_file_ui(
id = "myid",
file_extensions = c(".csv", ".tsv", ".txt", ".xls", ".xlsx", ".rds", ".sas7bdat", ".ods", ".dta"),
layout_params = "dropdown" # "inline" # or "dropdown"
)
),
shiny::column(
width = 8,
shiny::tags$b("Import status:"),
shiny::verbatimTextOutput(outputId = "status"),
shiny::tags$b("Name:"),
shiny::verbatimTextOutput(outputId = "name"),
shiny::tags$b("Code:"),
shiny::verbatimTextOutput(outputId = "code"),
shiny::tags$b("Data:"),
shiny::verbatimTextOutput(outputId = "data")
) )
),
shiny::column(
width = 8,
shiny::tags$b("Import status:"),
shiny::verbatimTextOutput(outputId = "status"),
shiny::tags$b("Name:"),
shiny::verbatimTextOutput(outputId = "name"),
shiny::tags$b("Code:"),
shiny::verbatimTextOutput(outputId = "code"),
shiny::tags$b("Data:"),
shiny::verbatimTextOutput(outputId = "data")
) )
) )
) server <- function(input, output, session) {
imported <- import_file_server(
server <- function(input, output, session) { id = "myid",
imported <- import_file_server( show_data_in = "popup",
id = "myid", trigger_return = "change",
show_data_in = "popup", return_class = "data.frame",
trigger_return = "change", # Custom functions to read data
return_class = "data.frame", read_fns = list(
# Custom functions to read data ods = import_ods,
read_fns = list( dta = function(file) {
ods = import_ods, haven::read_dta(
dta = function(file) { file = file,
haven::read_dta( .name_repair = "unique_quiet"
file = file, )
.name_repair = "unique_quiet" },
) # csv = function(file) {
}, # readr::read_csv(
# csv = function(file) { # file = file,
# readr::read_csv( # na = consider.na,
# file = file, # name_repair = "unique_quiet"
# na = consider.na, # )
# name_repair = "unique_quiet" # },
# ) csv = import_delim,
# }, tsv = import_delim,
csv = import_delim, txt = import_delim,
tsv = import_delim, xls = import_xls,
txt = import_delim, xlsx = import_xls,
xls = import_xls, rds = function(file) {
xlsx = import_xls, readr::read_rds(
rds = function(file) { file = file,
readr::read_rds( name_repair = "unique_quiet"
file = file, )
name_repair = "unique_quiet" }
) )
}
) )
)
output$status <- shiny::renderPrint({ output$status <- shiny::renderPrint({
imported$status() imported$status()
}) })
output$name <- shiny::renderPrint({ output$name <- shiny::renderPrint({
imported$name() imported$name()
}) })
output$code <- shiny::renderPrint({ output$code <- shiny::renderPrint({
imported$code() imported$code()
}) })
output$data <- shiny::renderPrint({ output$data <- shiny::renderPrint({
imported$data() imported$data()
}) })
} }
if (FALSE) {
shiny::shinyApp(ui, server) shiny::shinyApp(ui, server)
} }