Related to my previous question, I wonder now how I can trigger an event without change of the reactive sh()
here
openxlsx::read.xlsx(isolate(datafile()$datapath[1]), sh())
For example I upload an excel file with sheet name "table 1". Now, I upload another excel as replacement, but it's first sheet name is also "table 1". It won't trigger an event.
Here is the full code:
# EXCEL FILE SELECTION MODULE UI ------------------------------------------------------------
.input.xlsxUI <- function(id) {
fileInput(inputId = NS(id, "file"), label = "Test-Upload (.xlsx format)")
}
# EXCEL FILE SELECTION MODULE SERVER ------------------------------------------------------------
.input.xlsxServer = function(id) {
moduleServer(id, function(input, output, session) {
# The selected file, if any
reactive({
# If no file is selected, don't do anything
req(input$file)
input$file
})
})
}
# SHEET SELECTION MODULE UI ------------------------------------------------------------
.input.sheetUI = function(id) {
selectInput(NS(id, "sheet"), choices = NULL, label = "Sheet")
}
# SHEET SELECTION MODULE SERVER ------------------------------------------------------------
.input.sheetServer = function(id, datafile) {
stopifnot(is.reactive(datafile))
moduleServer(id, function(input, output, session) {
#excel-file is uploaded --> update selectInput of available sheets
observeEvent(datafile(), {
choices_list = openxlsx::getSheetNames(datafile()$datapath[1])
updateSelectInput(session = session,
inputId = "sheet",
choices = choices_list)
})
reactive(input$sheet)
})
}
# UPLOAD MODULE UI --------------------------------------------------------
.ExcelUI = function(id) {
tagList(.input.xlsxUI(id = NS(id, "xlsxfile")), # upload input
.input.sheetUI(id = NS(id, "sheet")), # sheet select
)
}
# UPLOAD MODULE SERVER ------------------------------------------------------------
.ExcelServer <- function(id) {
moduleServer(id, function(input, output, session) {
datafile <- .input.xlsxServer("xlsxfile")
sh <- .input.sheetServer("sheet", datafile)
# when sheet is selected, upload Excel
t = reactive({
req(sh())
openxlsx::read.xlsx(isolate(datafile()$datapath[1]), sh())
})
})
}
library(shiny)
library(shinyjs)
ui <- fluidPage(useShinyjs(),
wellPanel(.ExcelUI( "upld")),
wellPanel(verbatimTextOutput("out")))
server <- function(input, output, session) {
xlsxfile = .ExcelServer("upld")
output$out <- renderPrint(xlsxfile(), width = 40)
}
shinyApp(ui, server)