Skip to contents

Attaches a dataset to the UD state supplied.

Usage

UD_attach_ds(
  state,
  clean = NULL,
  isgood = TRUE,
  load_msg = NULL,
  data_file_local = NULL,
  data_file_ext = NULL,
  data_file = NULL,
  sheet = NULL,
  sheets = NULL,
  code = "",
  object_name = NULL,
  contents = NULL
)

Arguments

state

UD state module.

clean

Boolean switch to determine if the headers in the loaded dataset was cleaned.

isgood

Boolean object indicating if the file was successfully loaded.

load_msg

Text message indicated the success or any problems encountered when uploading the file.

data_file_local

Full path to the data file on the server.

data_file_ext

File extension of the uploaded file.

data_file

Dataset file name without the path.

sheet

If the uploaded file is an excel file, this is the currently selected sheet.

sheets

If the uploaded file is an excel file, this is a character vector of the sheets present in that file.

code

Code to load dataset.

object_name

Name of the dataset object created when code is evaluated.

contents

Data frame containting the contents of the data file.

Value

state with data set attached

Examples

# We need a module state object to use this function:
id="UD"
sess_res = UD_test_mksession(session=list())
#> → UD: including file
#> → UD:   source: file.path(system.file(package="onbrand"), "templates", "report.docx")
#> → UD:   dest:   file.path("config","report.docx")
#> → UD: including file
#> → UD:   source: file.path(system.file(package="onbrand"), "templates", "report.pptx")
#> → UD:   dest:   file.path("config","report.pptx")
#> → UD: including file
#> → UD:   source: file.path(system.file(package="onbrand"), "templates", "report.yaml")
#> → UD:   dest:   file.path("config","report.yaml")
#> → UD: State initialized
#> → UD: module checksum updated:897d952fecbc804999396a96f9df4b20
state = sess_res$state

# This is the full path to a test data file:
data_file_local  =  system.file(package="formods", "test_data", "TEST_DATA.xlsx")

# Excel file extension
data_file_ext    = "xlsx"

# Base file name
data_file        = "TEST_DATA.xlsx"

# Excel files need a sheet specification:
sheet           = "DATA"

# We will also attach the sheets along with it
sheets = readxl::excel_sheets(data_file_local)

ds_read_res = UD_ds_read(state,
  data_file_ext   = data_file_ext,
  data_file_local = data_file_local,
  data_file       = data_file,
  sheets          = sheets,
  sheet          = sheet)

# This would contain the loading code that will cascade down
# to the other modules when generating snippets and
# reproducible scripts
code = ds_read_res$code

# This is the R Object name that is used internally
# and in generated scripts. Should be the same as in
# the code above
object_name = ds_read_res$object_name

# This is the actual dataset:
contents   = ds_read_res$contents

state =  UD_attach_ds(
         state,
         data_file_local = data_file_local,
         data_file_ext   = ".xlsx",
         data_file       = data_file,
         sheet           = sheet,
         sheets          = sheets,
         code            = code,
         object_name     = object_name,
         contents        = contents)

state
#> $yaml
#> $yaml$FM
#> $yaml$FM$include
#> $yaml$FM$include$files
#> $yaml$FM$include$files[[1]]
#> $yaml$FM$include$files[[1]]$file
#> $yaml$FM$include$files[[1]]$file$source
#> [1] "file.path(system.file(package=\"onbrand\"), \"templates\", \"report.docx\")"
#> 
#> $yaml$FM$include$files[[1]]$file$dest
#> [1] "file.path(\"config\",\"report.docx\")"
#> 
#> 
#> 
#> $yaml$FM$include$files[[2]]
#> $yaml$FM$include$files[[2]]$file
#> $yaml$FM$include$files[[2]]$file$source
#> [1] "file.path(system.file(package=\"onbrand\"), \"templates\", \"report.pptx\")"
#> 
#> $yaml$FM$include$files[[2]]$file$dest
#> [1] "file.path(\"config\",\"report.pptx\")"
#> 
#> 
#> 
#> $yaml$FM$include$files[[3]]
#> $yaml$FM$include$files[[3]]$file
#> $yaml$FM$include$files[[3]]$file$source
#> [1] "file.path(system.file(package=\"onbrand\"), \"templates\", \"report.yaml\")"
#> 
#> $yaml$FM$include$files[[3]]$file$dest
#> [1] "file.path(\"config\",\"report.yaml\")"
#> 
#> 
#> 
#> 
#> 
#> $yaml$FM$deployed
#> [1] FALSE
#> 
#> $yaml$FM$code
#> $yaml$FM$code$theme
#> [1] "vibrant_ink"
#> 
#> $yaml$FM$code$showLineNumbers
#> [1] TRUE
#> 
#> $yaml$FM$code$gen_file
#> [1] "run_analysis.R"
#> 
#> $yaml$FM$code$gen_preamble
#> [1] "# formods automated output ------------------------------------------------\n# https://formods.ubiquity.tools/\nrm(list=ls())"
#> 
#> $yaml$FM$code$packages
#> [1] "onbrand" "writexl"
#> 
#> 
#> $yaml$FM$notifications
#> $yaml$FM$notifications$config
#> $yaml$FM$notifications$config$success
#> $yaml$FM$notifications$config$success$useFontAwesome
#> [1] FALSE
#> 
#> $yaml$FM$notifications$config$success$useIcon
#> [1] FALSE
#> 
#> $yaml$FM$notifications$config$success$background
#> [1] "#5bb85b"
#> 
#> 
#> $yaml$FM$notifications$config$failure
#> $yaml$FM$notifications$config$failure$useFontAwesome
#> [1] FALSE
#> 
#> $yaml$FM$notifications$config$failure$useIcon
#> [1] FALSE
#> 
#> $yaml$FM$notifications$config$failure$background
#> [1] "#d9534f"
#> 
#> 
#> $yaml$FM$notifications$config$info
#> $yaml$FM$notifications$config$info$useFontAwesome
#> [1] FALSE
#> 
#> $yaml$FM$notifications$config$info$useIcon
#> [1] FALSE
#> 
#> $yaml$FM$notifications$config$info$background
#> [1] "#5bc0de"
#> 
#> 
#> $yaml$FM$notifications$config$warning
#> $yaml$FM$notifications$config$warning$useFontAwesome
#> [1] FALSE
#> 
#> $yaml$FM$notifications$config$warning$useIcon
#> [1] FALSE
#> 
#> $yaml$FM$notifications$config$warning$background
#> [1] "#f0ac4d"
#> 
#> 
#> 
#> 
#> $yaml$FM$reporting
#> $yaml$FM$reporting$enabled
#> [1] TRUE
#> 
#> $yaml$FM$reporting$content_init
#> $yaml$FM$reporting$content_init$xlsx
#> [1] "rpt = list(summary = NULL,\n           sheets  = list())"
#> 
#> $yaml$FM$reporting$content_init$docx
#> [1] "rpt  = onbrand::read_template(\n  template = file.path(\"config\", \"report.docx\"),\n  mapping  = file.path(\"config\", \"report.yaml\"))"
#> 
#> $yaml$FM$reporting$content_init$pptx
#> [1] "rpt  = onbrand::read_template(\n  template = file.path(\"config\", \"report.pptx\"),\n  mapping  = file.path(\"config\", \"report.yaml\"))"
#> 
#> 
#> $yaml$FM$reporting$phs
#> $yaml$FM$reporting$phs[[1]]
#> $yaml$FM$reporting$phs[[1]]$name
#> [1] "HEADERLEFT"
#> 
#> $yaml$FM$reporting$phs[[1]]$location
#> [1] "header"
#> 
#> $yaml$FM$reporting$phs[[1]]$value
#> [1] ""
#> 
#> $yaml$FM$reporting$phs[[1]]$tooltip
#> [1] "left header text"
#> 
#> 
#> $yaml$FM$reporting$phs[[2]]
#> $yaml$FM$reporting$phs[[2]]$name
#> [1] "HEADERRIGHT"
#> 
#> $yaml$FM$reporting$phs[[2]]$location
#> [1] "header"
#> 
#> $yaml$FM$reporting$phs[[2]]$value
#> [1] ""
#> 
#> $yaml$FM$reporting$phs[[2]]$tooltip
#> [1] "right header text"
#> 
#> 
#> $yaml$FM$reporting$phs[[3]]
#> $yaml$FM$reporting$phs[[3]]$name
#> [1] "FOOTERLEFT"
#> 
#> $yaml$FM$reporting$phs[[3]]$location
#> [1] "footer"
#> 
#> $yaml$FM$reporting$phs[[3]]$value
#> [1] ""
#> 
#> $yaml$FM$reporting$phs[[3]]$tooltip
#> [1] "left footer text"
#> 
#> 
#> 
#> $yaml$FM$reporting$phs_formatting
#> $yaml$FM$reporting$phs_formatting$width
#> [1] "100%"
#> 
#> $yaml$FM$reporting$phs_formatting$tt_position
#> [1] "left"
#> 
#> $yaml$FM$reporting$phs_formatting$tt_size
#> [1] "medium"
#> 
#> 
#> 
#> $yaml$FM$ui
#> $yaml$FM$ui$button_style
#> [1] "fill"
#> 
#> $yaml$FM$ui$select_size
#> [1] 10
#> 
#> $yaml$FM$ui$color_green
#> [1] "#00BB8A"
#> 
#> $yaml$FM$ui$color_red
#> [1] "#FF475E"
#> 
#> $yaml$FM$ui$color_blue
#> [1] "#0088FF"
#> 
#> $yaml$FM$ui$color_purple
#> [1] "#bd2cf4"
#> 
#> 
#> $yaml$FM$data_meta
#> $yaml$FM$data_meta$data_header
#> [1] "<span style='color:===COLOR==='><b>===NAME===</b><br/><font size='-3'>===LABEL===</font></span>"
#> 
#> $yaml$FM$data_meta$subtext
#> [1] "===LABEL===: ===RANGE==="
#> 
#> $yaml$FM$data_meta$many_sep
#> [1] ",⋅⋅⋅,"
#> 
#> $yaml$FM$data_meta$data_types
#> $yaml$FM$data_meta$data_types$character
#> $yaml$FM$data_meta$data_types$character$color
#> [1] "#DD4B39"
#> 
#> $yaml$FM$data_meta$data_types$character$label
#> [1] "text"
#> 
#> 
#> $yaml$FM$data_meta$data_types$double
#> $yaml$FM$data_meta$data_types$double$color
#> [1] "#3C8DBC"
#> 
#> $yaml$FM$data_meta$data_types$double$label
#> [1] "num"
#> 
#> 
#> $yaml$FM$data_meta$data_types$integer
#> $yaml$FM$data_meta$data_types$integer$color
#> [1] "#3C8DBC"
#> 
#> $yaml$FM$data_meta$data_types$integer$label
#> [1] "num"
#> 
#> 
#> $yaml$FM$data_meta$data_types$other
#> $yaml$FM$data_meta$data_types$other$color
#> [1] "black"
#> 
#> $yaml$FM$data_meta$data_types$other$label
#> [1] "other"
#> 
#> 
#> 
#> 
#> $yaml$FM$labels
#> $yaml$FM$labels$default_ds
#> [1] "Original data set"
#> 
#> 
#> $yaml$FM$user_files
#> $yaml$FM$user_files$use_tmpdir
#> [1] TRUE
#> 
#> 
#> $yaml$FM$logging
#> $yaml$FM$logging$enabled
#> [1] TRUE
#> 
#> $yaml$FM$logging$timestamp
#> [1] TRUE
#> 
#> $yaml$FM$logging$timestamp_fmt
#> [1] "%Y-%m-%d %H:%M:%S"
#> 
#> $yaml$FM$logging$log_file
#> [1] "formods_log.txt"
#> 
#> $yaml$FM$logging$console
#> [1] TRUE
#> 
#> 
#> 
#> 
#> $MC
#> $MC$compact
#> $MC$compact$code
#> [1] FALSE
#> 
#> $MC$compact$preview
#> [1] TRUE
#> 
#> 
#> $MC$reporting
#> $MC$reporting$enabled
#> [1] FALSE
#> 
#> $MC$reporting$priority
#> [1] 1
#> 
#> 
#> $MC$labels
#> $MC$labels$default_ds
#> [1] "Original data set"
#> 
#> $MC$labels$upload_button
#> [1] "Select file"
#> 
#> $MC$labels$tab_main
#> [1] "Upload file"
#> 
#> $MC$labels$tab_code
#> [1] "Code"
#> 
#> $MC$labels$tab_preview
#> [1] "Data preview"
#> 
#> $MC$labels$switch_clean
#> [1] "Clean column names on upload"
#> 
#> $MC$labels$msg_bad_extension
#> [1] "Incorrect file extension (===EXT===). Unable to load file  ===FILE===.\nOnly the following extesions are allowed: ===ALLOWEDEXT===."
#> 
#> 
#> $MC$tooltips
#> $MC$tooltips$include
#> [1] TRUE
#> 
#> $MC$tooltips$show_code
#> [1] "Show data reading code"
#> 
#> 
#> $MC$formatting
#> $MC$formatting$preview
#> $MC$formatting$preview$width
#> [1] 800
#> 
#> $MC$formatting$preview$height
#> [1] 500
#> 
#> 
#> $MC$formatting$code
#> $MC$formatting$code$width
#> [1] 500
#> 
#> $MC$formatting$code$height
#> [1] 200
#> 
#> 
#> $MC$formatting$switch_clean
#> $MC$formatting$switch_clean$tooltip
#> [1] "Strange characters in column names can cause problems. When switched on, this will clean up the column headers and fix those that might break things."
#> 
#> $MC$formatting$switch_clean$tooltip_position
#> [1] "bottom"
#> 
#> 
#> 
#> $MC$allowed_extensions
#> [1] "csv"  "tsv"  "xls"  "xlsx"
#> 
#> $MC$code
#> $MC$code$packages
#> [1] "readxl"  "readr"   "janitor"
#> 
#> $MC$code$readOnly
#> [1] TRUE
#> 
#> $MC$code$mode
#> [1] "r"
#> 
#> $MC$code$preamble
#> [1] "# Loading data ------------------------------------------------------------"
#> 
#> 
#> $MC$ds_object_name
#> [1] "myDS"
#> 
#> $MC$clean_data
#> $MC$clean_data$enabled
#> [1] TRUE
#> 
#> $MC$clean_data$default
#> [1] TRUE
#> 
#> $MC$clean_data$code
#> [1] "# Make sure you put whatever packages you use here in the packages section\n# above.\n#\n# contents - data frame containing the loaded dataset.\n# code     - code used to load the dataset you need to append any cleaning\n#            code you generate  here.\n# object_name - Name of the dataset object used in generated code.\nif(system.file(package=\"janitor\") != \"\"){\n  contents = janitor::clean_names(contents, case=\"none\")\n  code = paste0(code, '\\n', object_name, ' = janitor::clean_names(', object_name,', case=\"none\")')\n}"
#> 
#> 
#> 
#> $UD
#> $UD$isgood
#> [1] TRUE
#> 
#> $UD$clean
#> NULL
#> 
#> $UD$load_msg
#> NULL
#> 
#> $UD$data_file_local
#> [1] "/private/var/folders/l9/6pm7xq5j1hn6l00zrs22j6mc0000gn/T/RtmpEfG5in/temp_libpath6d4076dedebf/formods/test_data/TEST_DATA.xlsx"
#> 
#> $UD$data_file_ext
#> [1] ".xlsx"
#> 
#> $UD$data_file
#> [1] "TEST_DATA.xlsx"
#> 
#> $UD$sheet
#> [1] "DATA"
#> 
#> $UD$sheets
#> [1] "DATA" "DDT" 
#> 
#> $UD$code
#> [1] "myDS = readxl::read_excel(path=\"TEST_DATA.xlsx\", sheet=\"DATA\")\nmyDS = janitor::clean_names(myDS, case=\"none\")"
#> 
#> $UD$object_name
#> [1] "myDS"
#> 
#> $UD$checksum
#> [1] "897d952fecbc804999396a96f9df4b20"
#> 
#> $UD$contents
#> # A tibble: 10,680 × 33
#>       ID TIME_DY TIME_HR NTIME_DY NTIME_HR  TIME     AMT    DV RECDESC     DOSE
#>    <dbl>   <dbl>   <dbl>    <dbl>    <dbl> <dbl>   <dbl> <dbl> <chr>      <dbl>
#>  1     1       0       0        0        0     0 3000000    0  dose     3000000
#>  2     1       0       0        0        0     0       0 1250  BM_ng_ml 3000000
#>  3     1       0       0        0        0     0       0    0  C_ng_ml  3000000
#>  4     1       1      24        1       24    24       0 1647  BM_ng_ml 3000000
#>  5     1       1      24        1       24    24       0  690. C_ng_ml  3000000
#>  6     1       2      48        2       48    48       0 1947  BM_ng_ml 3000000
#>  7     1       2      48        2       48    48       0  588. C_ng_ml  3000000
#>  8     1       3      72        3       72    72       0 2170  BM_ng_ml 3000000
#>  9     1       3      72        3       72    72       0  506. C_ng_ml  3000000
#> 10     1       4      96        4       96    96       0 2332  BM_ng_ml 3000000
#> # ℹ 10,670 more rows
#> # ℹ 23 more variables: DOSE_STR <chr>, Cohort <chr>, ROUTE <chr>, WT <dbl>,
#> #   SUBTYPE <chr>, SEX <chr>, F1 <dbl>, ka <dbl>, CL <dbl>, Vc <dbl>, Vp <dbl>,
#> #   Q <dbl>, BM_IC <dbl>, kdeg_BM <dbl>, Emax <dbl>, EC50 <dbl>, CMT <chr>,
#> #   DUR <dbl>, EVID <dbl>, NDose <chr>, DOSE_NUM <dbl>, SUBTYPE_ID <dbl>,
#> #   SEX_ID <dbl>
#> 
#> 
#> $MOD_TYPE
#> [1] "UD"
#> 
#> $id
#> [1] "UD"
#> 
#> $FM_yaml_file
#> [1] "/private/var/folders/l9/6pm7xq5j1hn6l00zrs22j6mc0000gn/T/RtmpEfG5in/temp_libpath6d4076dedebf/formods/templates/formods.yaml"
#> 
#> $MOD_yaml_file
#> [1] "/private/var/folders/l9/6pm7xq5j1hn6l00zrs22j6mc0000gn/T/RtmpEfG5in/temp_libpath6d4076dedebf/formods/templates/UD.yaml"
#> 
#> $shiny_token
#> [1] "non_shiny"
#>