Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import file dev #101

Merged
merged 2 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
135 changes: 71 additions & 64 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 All @@ -37,60 +38,58 @@ import_file_ui <- function(id,
class = "datamods-import",
html_dependency_datamods(),
title,
tags$div(
class = "datamods-file-import",
tags$div(
fileInput(
inputId = ns("file"),
label = i18n("Upload a file:"),
buttonLabel = i18n("Browse..."),
placeholder = i18n("No file selected"),
accept = file_extensions,
width = "100%"
)
tagAppendAttributes(
fileInput(
inputId = ns("file"),
label = i18n("Upload a file:"),
buttonLabel = i18n("Browse..."),
placeholder = i18n("No file selected"),
accept = file_extensions,
width = "100%"
),
tags$div(
tags$label(
class = "control-label",
style = css(visibility = "hidden", width = "100%", marginBottom = "0.5rem"),
"Parameters",
`for` = ns("settings")
class = "mb-0"
),
fluidRow(
column(
width = 6,
numericInputIcon(
inputId = ns("skip_rows"),
label = i18n("Rows to skip before reading data:"),
value = 0,
min = 0,
icon = list("n ="),
size = "sm",
width = "100%"
),
dropMenu(
placement = "bottom-end",
actionButton(
inputId = ns("settings"),
label = phosphoricons::ph("gear", title = "parameters"),
class = "btn-block"
),
numericInputIcon(
inputId = ns("skip_rows"),
label = i18n("Number of rows to skip before reading data:"),
value = 0,
min = 0,
icon = list("n =")
),
tagAppendChild(
textInputIcon(
inputId = ns("na_label"),
label = i18n("Missing values character(s):"),
value = ",NA",
icon = list("NA")
),
shiny::helpText(ph("info"), "if several use a comma (',') to separate them")
),
tagAppendChild(
textInputIcon(
inputId = ns("dec"),
label = i18n("Decimal separator:"),
value = ".",
icon = list("0.00")
inputId = ns("na_label"),
label = i18n("Missing values character(s):"),
value = ",NA",
icon = list("NA"),
size = "sm",
width = "100%"
),
textInputIcon(
inputId = ns("encoding"),
label = i18n("Encoding:"),
value = "UTF-8",
icon = phosphoricons::ph("text-aa")
)
shiny::helpText(ph("info"), "if several use a comma (',') to separate them")
)
),
column(
width = 6,
textInputIcon(
inputId = ns("dec"),
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 @@ -115,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 @@ -152,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 @@ -232,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 @@ -257,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 @@ -269,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 @@ -287,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
2 changes: 1 addition & 1 deletion examples/modal.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ library(datamods)

ui <- fluidPage(
# Try with different Bootstrap version
# theme = bslib::bs_theme(version = 5, preset = "bootstrap"),
theme = bslib::bs_theme(version = 5, preset = "bootstrap"),
fluidRow(
column(
width = 4,
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.

Loading