knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(shiny.CSVImport) library(knitr)
The CSV import module provides a standard user interface to read and convert a CSV file according to the users' and the apps' needs. Having selected a file, the module displays the available columns and the associated data types. The user can change the data type and specify the required format required to interpret the data correctly.
Users can specify the general format of the CSV file:
The package comes along with it's own translations. These languages are currently available:
The Spanish and Russian translation most likely need some improvement but should be clear enough to use (no promises, though).
When calling the module an application can provide many options as arguments.
UiLng
): use international language codes to set the desired language.Available data types are based on the capabilities of the readr package:
character
. An arbitrary character string.number
. An arbitrary number (R type numeric
). Correct interpretation may depend on the thousands and decimal separator.integer
. Integer value. Correct interpretation may depend on the thousands separator.factor
. an R
factor.logical
. a boolean/logical value.date
, time
, and datetime
. Correct interpretation depends on a format string (see strptime
).The type double
is available but impractical to use when importing from CSV. The module will only be able to parse these numbers when they do not have a thousands separator. It is safer to use number
.
Two further data types have been defined that search for certain patterns in the data and return a logical value depending on the result of the search:
find
. Finds the text given in the format string.regexfind
. Get a pattern match based on a given regular expression. The module uses Perl-compatible regular expressions because they are more commonly known compared to the R
syntax.The call is simple and (consistent with Shiny) you can choose any module Id
.
ModuleImportUI("ProjectDataFile")
ModuleImportServer("ProjectDataFile")
{width="75%"}
The module depends on the capabilities of R
. Thus, it uses the POSIX syntax to specify the expected format of date and time values.
TimeFormats <- read.table(sep = ";", header = TRUE, text = "Code;Meaning; Example %a;Abbreviated weekday;Mon %A;Full weekday; Monday %b;Abbreviated month; Jan %B;Full month; January %c;Locale-specific date and time; %d;Decimal date; %H;Decimal hours (24 hour); 14 %I;Decimal hours (12 hour); 02 %j;Decimal day of the year; %m;Decimal month; 6 (June) %M;Decimal minute; 22 %p;Locale-specific AM/PM; %S;Decimal second; 56 %U;Decimal week of the year (starting on Sunday); 3 %w;Decimal Weekday (0=Sunday); 0 %W;Decimal week of the year (starting on Monday); 1 %x;Locale-specific Date; %X;Locale-specific Time; %y;2-digit year; 12 %Y;4-digit year; 2012 %z;Offset from GMT; %Z;Time zone (character);UST") knitr::kable( TimeFormats )
A few examples:
x <- strptime(c("2020-01-24 16:07:52.12345"), "%Y-%m-%d %H:%M:%OS") # Dates format(x, "%Y-%m-%d") # yyyy-mm-dd format(x, "%Y-%j") # yyyy-[day of year] format(x, "%Y-W%V-%u") # format(x, "%B, %d is in CW%V") # dddd, dd is in CW[week] # Time format(x, "%H:%M:%S") format(x, "%I:%M:%OS4 %p") # "am/pm" will be missing because this has run on a German system # Test am/pm format(x, "%G-W%V-%u")
{width="25%"}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.