Skip to content

Instantly share code, notes, and snippets.

@keqiang
Created May 15, 2017 19:43
Show Gist options
  • Save keqiang/cd0de191c24bc6982b4a0d67f5fcfc2a to your computer and use it in GitHub Desktop.
Save keqiang/cd0de191c24bc6982b4a0d67f5fcfc2a to your computer and use it in GitHub Desktop.
A general R Shiny module to import Comma or Tab Separated Files
fileImportUI <- function(id, label = "Comma or Tab Separated File") {
ns <- NS(id)
tagList(
# browse to select file to import
fileInput(ns("selectedFile"), label, accept = c(
"text/csv",
"text/comma-separated-values",
"text/plain",
".csv",
".tsv",
".txt"
)),
# checkbox to indicate if the original file has column headers
checkboxInput(ns("hasColumnHeaders"), label = "Has column headers", value = TRUE),
# combobox to specify quote type
selectInput(ns("quoteType"), label = "Quote type", c(
"None" = "",
"Double quote" = "\"",
"Single quote" = "'"
)),
# specify value separator
selectInput(ns("separator"), label = "Separator type", c(
"Comma" = ",",
"Tab" = "\t"
)),
# server will provide options after parsing the table.
# this is to specify which column should be used as unique row names for R data frame
uiOutput(ns("columnAsRowNamesControl")),
# a preview table to help user get the right format
box(title = "Data Preview",
status = "primary",
solidHeader = TRUE,
collapsible = TRUE,
width = 12,
DT::dataTableOutput(ns("previewTable"))
),
# the import button
actionButton(ns("importButton"), "Import")
)
}
fileImport <- function(input, output, session, stringsAsFactors = FALSE) {
# user selected file
verifiedSelectedFile <- reactive({
validate(need(input$selectedFile, message = FALSE))
input$selectedFile
})
# to give a correct list of column names, need to use NULL for param row.names
tableColumnsOptions <- reactive({
dataFrameWithAllColumnsAsData <- head(read.table(verifiedSelectedFile()$datapath,
header = input$hasColumnHeaders,
quote = input$quoteType,
sep = input$separator,
stringsAsFactors = stringsAsFactors,
row.names = NULL))
colNames <- colnames(dataFrameWithAllColumnsAsData)
tableColumns <- c(1 : length(colNames))
names(tableColumns) <- colNames
tableColumns
})
# render the preview table with actual imported data frame, using the specified param row.names
output$previewTable <- DT::renderDataTable(head(instantDataFrame()), options = list(scrollX = TRUE))
# render a combobox for user to select which column to use as row.names param
output$columnAsRowNamesControl <- renderUI({
# for server side defined UI element, has to retrieve ns function and wrap the input's id
ns <- session$ns
selectInput(ns("columnAsRowNames"),
label = "Choose a column as unique row names",
choices = tableColumnsOptions())
})
# the current data frame imported regarding to the use specified options
instantDataFrame <- reactive({
columnIndex <- input$columnAsRowNames
# the selectinput ui gives a string rather than integer, so needs to be converted
if (!is.null(columnIndex)) {
columnIndex <- as.integer(columnIndex)
}
read.table(verifiedSelectedFile()$datapath,
header = input$hasColumnHeaders,
quote = input$quoteType,
sep = input$separator,
stringsAsFactors = stringsAsFactors,
row.names = columnIndex)
})
# return the imported data only when the import button clicked
dataToBeImported <- eventReactive(input$importButton, {
instantDataFrame()
})
return(dataToBeImported)
}
library(shiny)
library(shinydashboard)
library(DT)
source("fileImportModule.R")
shinyServer(function(input, output) {
importedData <- callModule(fileImport, "fileImport1")
output$debug <- renderPrint({
print(importedData())
})
})
dashboardPage(
dashboardHeader(title = "File Importing Module Example"),
dashboardSidebar(),
dashboardBody(
fluidRow(
box(title = "Data Import",
status = "primary",
solidHeader = TRUE,
width = 12,
fileImportUI("fileImport1"),
verbatimTextOutput("debug")
)
)
)
)
@keqiang
Copy link
Author

keqiang commented May 15, 2017

All the required libraries are indicated in global.R

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment