library(datamods)
When working with shiny, uploading data is a bit of a hassle. There are a number of corner cases one has to be mindful of. For example:
checking that the uploaded file is of the correct format
checking if R can load the file and prevent errors
while dealing with excel files, providing an option to choose the sheet number.
while using Googlesheets, the hassle of authentication and stuff.
The {datamods} package helps to easily load and use data in shiny
apps with the help of modules.
There are 5 modules that can load data from various different sources.
The modules with a short intro are described below
Imports data from the user’s global environment or a package environment to retrieve included in it. It searches for data sets in the global environment and lets the user choose the data to use.
# UI
import_globalenv_ui("myid")
# Server
<- import_globalenv_server("myid") imported
Imports data from an external file. The file can be of any format, csv, xlsx, tsv etc.. Import is performed by package rio. In case of Excel files, it gives an option to choose the sheet.
# UI
import_file_ui("myid")
# Server
<- import_file_server("myid") imported
Imports data via copy/paste. Simply copy and paste data from any source.
# UI
import_copypaste_ui("myid")
# Server
<- import_copypaste_server("myid") imported
Imports data from a Googlesheet. Use the shareable link to read data.
# UI
import_googlesheets_ui("myid")
# Server
<- import_googlesheets_server("myid") imported
Imports data from a URL. Only flat data in any format supported by package rio.
# UI
import_url_ui("myid")
# Server
<- import_url_server("myid") imported
All modules are used in the same way in a Shiny application, here is an example:
library(shiny)
library(datamods)
<- fluidPage(
ui $h3("Import data with copy & paste"),
tagsfluidRow(
column(
width = 4,
import_copypaste_ui("myid")
),column(
width = 8,
$b("Imported data:"),
tagsverbatimTextOutput(outputId = "status"),
verbatimTextOutput(outputId = "data")
)
)
)
<- function(input, output, session) {
server
<- import_copypaste_server("myid")
imported
$status <- renderPrint({
output$status()
imported
})$data <- renderPrint({
output$data()
imported
})
}
shinyApp(ui, server)
All modules have the same return value server-side, a
list
with three slots:
reactive
function returning
the status: NULL
, error
or
success
.reactive
function returning
the name of the imported data as character
.reactive
function returning
the imported data.frame
.All modules can be launched at once in a modal window:
Launch the modal server-side with:
observeEvent(input$launch_modal, {
import_modal(
id = "myid",
title = "Import data to be used in application"
) })
See ?import_modal
for a complete example.
This module allow to dynamically select, rename and convert variables of a dataset.
Some options for converting to date and numeric are available in a dropdown menu.
Return value of the module is a reactive
function with
the update data.
When importing data into an application it can be useful to check that data respect some expectations: number of rows/columns, existence of a variable, … This module allow to validate rules defined with package validate.
# UI
validation_ui("validation", display = "inline")
# Server
<- validation_server(
results id = "validation",
data = dataset,
n_row = ~ . > 20, # more than 20 rows
n_col = ~ . >= 3, # at least 3 columns
rules = myrules
)
# Rules are defined as follow:
<- validator(
myrules is.character(Manufacturer) | is.factor(Manufacturer),
is.numeric(Price),
> 12, # we should use 0 for testing positivity, but that's for the example
Price !is.na(Luggage.room),
in_range(Cylinders, min = 4, max = 8),
%in% c("Yes", "No")
Man.trans.avail
)# Add some labels
label(myrules) <- c(
"Variable Manufacturer must be character",
"Variable Price must be numeric",
"Variable Price must be strictly positive",
"Luggage.room must not contain any missing values",
"Cylinders must be between 4 and 8",
"Man.trans.avail must be 'Yes' or 'No'"
)# you can also add a description()
Validation results can be displayed in a dropdown menu (above left) or inline where the module is called.
The return value server-side is a list with the following items:
Interactively filter a data.frame
and generate code to
reproduce filters applied:
# UI
filter_data_ui("filtering", max_height = "500px")
# Server
<- filter_data_server(
res_filter id = "filtering",
data = reactive(mtcars),
name = reactive("mtcars"),
vars = reactive(names(mtcars)),
widget_num = "slider",
widget_date = "slider",
label_na = "Missing"
)
You can select variables for which to create a filter and choose widgets used to create the UI filter.
The return value server-side is a list with the following items:
reactive
function
returning the data filtered.reactive
function returning
the dplyr pipeline to filter data.reactive
function returning an
expression to filter data.