require(shiny)
require(shinydashboard)
inModule <- function(input, output, session) {
data <- reactive({
inFile <- input$datafile
if (is.null(inFile)){
list(name = NA, data = NA)
} else {
data <- read.table(
inFile$datapath,
header=as.logical(input$dataHeader),
sep=input$dataSep)
return(list(name = input$datafile$name, data = data))
}
})
return(data)
}
inModuleUI <- function(id, show=FALSE, n=1){
ns <- NS(id)
if (show){
box(
h4('Select data file to upload (<30MB)'),
fileInput(inputId = ns('datafile'),
label = NULL,
multiple = FALSE
),
radioButtons(inputId = ns('dataSep'),
label = 'Separator',
choices = c('Comma' = ',', 'Semi-colon' = ';', 'One space' = ' ',
'One tab' = '\t', 'Any white space' = ''),
selected = ',',
inline = TRUE
),
radioButtons(inputId = ns('dataHeader'),
label = 'Does the file contain headers?',
choices = c('Yes' = TRUE, 'No' = FALSE),
selected = TRUE,
inline = TRUE
),
title = ifelse(is.null(n), 'Dataset', sprintf('Dataset %d', n)),
collapsible = FALSE,
width = 4,
status = 'primary',
solidHeader = TRUE
)
}
}
outModule <- function(input, output, session){
ns <- session$ns
output$dataLoadUI1 <- renderUI(
inModuleUI(ns('datafile1'), show = TRUE, n = 1)
)
dataDf1 <- callModule(inModule, 'datafile1')
output$dataLoadUI2 <- renderUI({
inModuleUI(ns('datafile2'), show = !is.na(dataDf1()$name), n = 2)
})
dataDf2 <- callModule(inModule, 'datafile2')
output$dataLoadUI3 <- renderUI({
inModuleUI(ns('datafile3'), show = !is.na(dataDf2()$name), n = 3)
})
dataDf3 <- callModule(inModule, 'datafile3')
output$dataDfFilenames <- renderPrint({
cat('Content of dataDf1()$name\n')
print(dataDf1()$name)
cat('Content of dataDf2()$name\n')
print(dataDf2()$name)
cat('Content of dataDf3()$name\n')
print(dataDf3()$name)
})
output$dataTabBox <- renderUI({
fluidRow(
verbatimTextOutput(ns('dataDfFilenames')),
uiOutput(ns('dataLoadUI1')),
uiOutput(ns('dataLoadUI2')),
uiOutput(ns('dataLoadUI3'))
)
})
}
outModuleUI <- function(id){
ns <- NS(id)
fluidRow(
uiOutput(ns('dataTabBox'))
)
}
server <- function(input, output, session){
dataInfo <- callModule(outModule, 'getData')
}
ui <- dashboardPage(
header=dashboardHeader(
title='Dummy'
),
sidebar=dashboardSidebar(
sidebarMenu(
menuItem('Data', tabName = 'dataTab', icon = icon('table'), selected = TRUE)
)
),
body=dashboardBody(
tabItems(
tabItem(tabName = 'dataTab',
outModuleUI('getData')
)
)
),
title='Explore',
skin='blue'
)
shinyApp(ui = ui, server = server)