Skip to content

Commit

Permalink
updated import file UI + generate code
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Apr 21, 2024
1 parent 7cc061b commit 1e22396
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ importFrom(shiny,reactiveValuesToList)
importFrom(shiny,removeModal)
importFrom(shiny,removeUI)
importFrom(shiny,renderPlot)
importFrom(shiny,renderTable)
importFrom(shiny,renderUI)
importFrom(shiny,req)
importFrom(shiny,selectizeInput)
Expand All @@ -145,7 +144,6 @@ importFrom(shiny,singleton)
importFrom(shiny,sliderInput)
importFrom(shiny,tabPanel)
importFrom(shiny,tabPanelBody)
importFrom(shiny,tableOutput)
importFrom(shiny,tabsetPanel)
importFrom(shiny,tagList)
importFrom(shiny,tags)
Expand Down
44 changes: 28 additions & 16 deletions R/import-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
#'
#' @name import-file
#'
#' @importFrom shiny NS fileInput tableOutput actionButton icon
#' @importFrom shiny NS fileInput actionButton icon
#' @importFrom htmltools tags tagAppendAttributes css tagAppendChild
#' @importFrom shinyWidgets pickerInput numericInputIcon textInputIcon dropMenu
#' @importFrom phosphoricons ph
#' @importFrom toastui datagridOutput2
#'
#' @example examples/from-file.R
import_file_ui <- function(id,
Expand Down Expand Up @@ -48,16 +49,16 @@ import_file_ui <- function(id,
),
class = "mb-0"
),
tags$p(phosphoricons::ph("gear", title = "parameters"), i18n("Parameters")),
fluidRow(
column(
width = 6,
numericInputIcon(
inputId = ns("skip_rows"),
label = i18n("Number of rows to skip before reading data:"),
label = i18n("Rows to skip before reading data:"),
value = 0,
min = 0,
icon = list("n ="),
size = "sm",
width = "100%"
),
tagAppendChild(
Expand All @@ -66,6 +67,7 @@ import_file_ui <- function(id,
label = i18n("Missing values character(s):"),
value = ",NA",
icon = list("NA"),
size = "sm",
width = "100%"
),
shiny::helpText(ph("info"), "if several use a comma (',') to separate them")
Expand All @@ -78,13 +80,15 @@ import_file_ui <- function(id,
label = i18n("Decimal separator:"),
value = ".",
icon = list("0.00"),
size = "sm",
width = "100%"
),
textInputIcon(
inputId = ns("encoding"),
label = i18n("Encoding:"),
value = "UTF-8",
icon = phosphoricons::ph("text-aa"),
size = "sm",
width = "100%"
)
)
Expand All @@ -110,10 +114,7 @@ import_file_ui <- function(id,
)
),
if (isTRUE(preview_data)) {
tagAppendAttributes(
tableOutput(outputId = ns("table")),
class = "datamods-table-container"
)
datagridOutput2(outputId = ns("table"))
},
uiOutput(
outputId = ns("container_confirm_btn"),
Expand Down Expand Up @@ -147,13 +148,14 @@ import_file_ui <- function(id,
#'
#' @importFrom shiny moduleServer
#' @importFrom htmltools tags tagList
#' @importFrom shiny reactiveValues reactive observeEvent removeUI req renderTable
#' @importFrom shiny reactiveValues reactive observeEvent removeUI req
#' @importFrom shinyWidgets updatePickerInput
#' @importFrom readxl excel_sheets
#' @importFrom rio import
#' @importFrom rlang exec fn_fmls_names is_named is_function
#' @importFrom tools file_ext
#' @importFrom utils head
#' @importFrom toastui renderDatagrid2 datagrid
#'
#' @rdname import-file
import_file_server <- function(id,
Expand Down Expand Up @@ -227,6 +229,7 @@ import_file_server <- function(id,
)
parameters <- parameters[which(names(parameters) %in% fn_fmls_names(read_fns[[extension]]))]
imported <- try(rlang::exec(read_fns[[extension]], !!!parameters), silent = TRUE)
code <- call2(read_fns[[extension]], !!!modifyList(parameters, list(file = input$file$name)))
} else {
if (is_excel(input$file$datapath)) {
req(input$sheet)
Expand All @@ -252,10 +255,13 @@ import_file_server <- function(id,
)
}
imported <- try(rlang::exec(rio::import, !!!parameters), silent = TRUE)
code <- call2("import", !!!modifyList(parameters, list(file = input$file$name)), .ns = "rio")
}

if (inherits(imported, "try-error"))
if (inherits(imported, "try-error")) {
imported <- try(rlang::exec(rio::import, !!!parameters[1]), silent = TRUE)
code <- call2("import", !!!list(file = input$file$name), .ns = "rio")
}

if (inherits(imported, "try-error") || NROW(imported) < 1) {

Expand All @@ -264,6 +270,7 @@ import_file_server <- function(id,
temporary_rv$status <- "error"
temporary_rv$data <- NULL
temporary_rv$name <- NULL
temporary_rv$code <- NULL

} else {

Expand All @@ -282,37 +289,42 @@ import_file_server <- function(id,
temporary_rv$status <- "success"
temporary_rv$data <- imported
temporary_rv$name <- input$file$name
temporary_rv$code <- code
}
}, ignoreInit = TRUE)

observeEvent(input$see_data, {
show_data(temporary_rv$data, title = i18n("Imported data"), type = show_data_in)
})

output$table <- renderTable({
output$table <- renderDatagrid2({
req(temporary_rv$data)
data <- head(temporary_rv$data, 5)
classes <- get_classes(data)
classes <- sprintf("<span style='font-style: italic; font-weight: normal; font-size: small;'>%s</span>", classes)
names(data) <- paste(names(data), classes, sep = "<br>")
data
}, striped = TRUE, bordered = TRUE, sanitize.colnames.function = identity, spacing = "xs")
datagrid(
data = head(temporary_rv$data, 5),
theme = "striped",
colwidths = "guess",
minBodyHeight = 250
)
})

observeEvent(input$confirm, {
imported_rv$data <- temporary_rv$data
imported_rv$name <- temporary_rv$name
imported_rv$code <- temporary_rv$code
})

if (identical(trigger_return, "button")) {
return(list(
status = reactive(temporary_rv$status),
name = reactive(imported_rv$name),
code = reactive(imported_rv$code),
data = reactive(as_out(imported_rv$data, return_class))
))
} else {
return(list(
status = reactive(temporary_rv$status),
name = reactive(temporary_rv$name),
code = reactive(temporary_rv$code),
data = reactive(as_out(temporary_rv$data, return_class))
))
}
Expand Down
5 changes: 5 additions & 0 deletions examples/from-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ ui <- fluidPage(
verbatimTextOutput(outputId = "status"),
tags$b("Name:"),
verbatimTextOutput(outputId = "name"),
tags$b("Code:"),
verbatimTextOutput(outputId = "code"),
tags$b("Data:"),
verbatimTextOutput(outputId = "data")
)
Expand Down Expand Up @@ -47,6 +49,9 @@ server <- function(input, output, session) {
output$name <- renderPrint({
imported$name()
})
output$code <- renderPrint({
imported$code()
})
output$data <- renderPrint({
imported$data()
})
Expand Down
5 changes: 5 additions & 0 deletions man/import-file.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/import-modal.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1e22396

Please sign in to comment.