I am building a shiny application that takes a CSV file input and uses the data to display a datatable and some plots. I want to pull out a datatable module and a select input module with a list of select inputs. I have done this already. The problem I am facing now is I want these 2 modules to access the reactive data on a global scale.
If I declare the reactive data in each module server I get an error object 'input' not found but if I declare it in only one of the server modules like for the datatable it works.
If I declare It generally in the app server I get the same error too.
How can I declare this generally so that if I use the reactive data in any of my modules it works?
Also as you can see in my code I set a default dataset (iris) for the reactive data which loads perfectly when I start the app but now when I upload a new dataset from my PC using the file input, it doesn't refresh the table and the select inputs to show the information of that dataset.
How can I solve these problems?
Here is my code.
Server
source("modules.R")
shinyServer(function(input, output) {
options(shiny.maxRequestSize = 30*1024^4)
data = reactive({
file <- input$Files
if( is.null (file) ){ iris }
else { read_csv (file = file$datapath) }
# read_rds (file = file$datapath)
})
sideP_iServer("plot_graph")
DataTableServer("data_table")
})
Ui
source("modules.R")
# Define UI for application that draws a histogram
shinyUI(
navbarPage(
title = "Data Explorer",
theme = bslib::bs_theme(version = 5, bootswatch = "flatly"),
tabPanel(title = "Home",icon = icon("house"),
sidebarLayout(
sidebarPanel(width = 3,style = "height: 93vh;",
fileInput("Files", "Upload Your File",
buttonLabel=list(icon("file-import")),
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv",".rds")),
sideP_iUI("plot_graph")
),
mainPanel(width = 9,style = "height: 95vh; overflow-y: auto;",
tabsetPanel(
tabPanel("DataTable",
DataTableUI("data_table")
)
)
)
)
)
)
)
Module R
date_num <- function(x){
is.numeric(x)|is.Date(x)
}
############################### DataTable module
DataTableUI = function(id){
ns= NS(id)
DT::dataTableOutput(ns("data_table"))
}
DataTableServer = function(id){
moduleServer(id, function(input, output, session) {
output$data_table = DT::renderDataTable(
data(), options =list(pageLength = 10,
scrollX = TRUE,
dom = 'Bfrtip',
buttons = c('pdf','csv', 'excel')
),
extensions = 'Buttons',
selection = 'single',
filter = 'bottom',
rownames =TRUE
)
})
}
################################## SideBar Select Input module
sideP_iUI = function(id){
uiOutput(NS(id,"plot_graph"))
}
sideP_iServer = function(id){
moduleServer(id, function(input, output, session) {
output$plot_graph = renderUI({
if( is.null( data() ) ){return()}
else{
numeric_df = data() [ sapply (data(), date_num)]
species = as.vector( unique (data() [!sapply (data(), date_num)]))
if(length(species) == FALSE) {species = c('All')} else {species = as.vector(unique(unlist(species)))}
spec = data()[!sapply (data(), is.numeric)]
if(length(spec) == 0) {spec = c('Every')}
list(
checkboxGroupButtons( inputId = "PlotA",
label = h6("Select Your Plot"),
choices = c("Bar","Scatter"),
selected = c("Scatter"),
checkIcon = list(
yes = icon("square-check")
)),
checkboxGroupButtons( inputId = "PlotB",
choices = c("Box","Correlation"),
selected = NULL,
checkIcon = list(
yes = icon("square-check")
)),
selectInput( inputId = "xvalue",
label = h6("Select X:"),
choices = names(numeric_df),
selected =NULL),
selectInput( inputId = "yvalue",
label = h6("Select Y:"),
choices = names(numeric_df),
selected = names(numeric_df)[2]),
selectInput( inputId = "bvalue",
label = h6("Selct X For One Way Plot:"),
choices = names(spec),
selected = NULL),
pickerInput( inputId = "picker",
label = h6("Select Group:"),
choices = species,
selected = species[1],
multiple = TRUE,
options = list(
`actions-box` = TRUE,
`deselect-all-text` = "None",
`select-all-text` = " All ",
`none-selected-text` = "Select Category",
`live-search` = TRUE,
`virtual-scroll` = 10,
`multiple-separator` = "\n",
size = 10
))
)
}
})
})
}###### end of sidePanel Inputs