0

I created a module to help me accept 1)an excel file 2)a text input for the sheet name and 3) a text input for the range.

I want to be able to use this module in an App such that each time I click on the action button (AddExcelDataButton in the code below), it allows me to input a different file. I also need to be able to extract the contents of the file later.

I tried the below code in the main App, but it is throwing me the following errors error1: The UI is linking all the inputs "inside the button" error2: I am unable to figure out how to "access" or retrieve the filenames later in the code.

Any help in doing this the right way is highly appreciated please!

CODE FOR THE MODULE:

importExceldataUI <- function(importExceldata){
  tagList(
    
    tags$div(
      HTML(paste0("<b>", "Enter Your Data Here"))
    ),
    
    tags$div(
    fileInput(inputId = "ImportExcelFile",
              label = "Excel File",
              multiple=FALSE),
    style = "display:inline-block; vertical-align:top"
    ),# end of tags$div for fileInput ImportExcelFile
    
    tags$div(
    textInput(inputId = "ExcelSheetName",
              label = "Sheet",
              value="Data",),
    style = "display:inline-block; vertical-align:top"
     ),#end of tags$Div for texinput-ExcelSheetName
    
    tags$div(
    textInput(inputId = "ExcelSheetRange",
              label = "Range",
              value = "C5:BN1000"),
    style = "display:inline-block"
    )#end of tags$div for textInput - sheetrange
 )
}


importExceldataServer <- function(importExceldata){
  moduleServer(importExceldata, function(input, output, session){
 })  
}

CODE FOR MAIN APP

importExceldataApp <- function(){
  ui <- fluidPage(
    mainPanel(
     actionButton(inputId = "AddExcelDataButton", label = "Click here to add Excel Data"),
     
  )#emd pf mainpanel
  )
  
  server <- function(input, output, session){
       observeEvent(input$AddExcelDataButton, {
        insertUI(selector = "#AddExcelDataButton",
                 ui = importExceldataUI(paste0("file",input$AddExcelDataButton))) 
       })#end of observeEvent
    
  }
  
  shinyApp(ui, server)
}


importExceldataApp()

1 Answer 1

1

There are some errors you need to fix:

  1. to use modules, you must have IDs for both UI and server. For each pair, they must have the same ID.
  2. For ids in module UI, you must use namespace NS.
  3. For insertUI, default is insert into the selector, apparently, you don't want to insert inside a button, you need to add after the button, so you need have where argument, please read the help file of this function.

You should read more about shiny modules standards

Here is the working code:

library(shiny)

importExceldataUI <- function(id){
    ns <- NS(id)
    tagList(
        tags$div(
            HTML(paste0("<b>", "Enter Your Data Here"))
        ),
        tags$div(
            fileInput(inputId = ns("ImportExcelFile"),
                      label = "Excel File",
                      multiple=FALSE),
            style = "display:inline-block; vertical-align:top"
        ),# end of tags$div for fileInput ImportExcelFile
        
        tags$div(
            textInput(inputId = ns("ExcelSheetName"),
                      label = "Sheet",
                      value="Data"),
            style = "display:inline-block; vertical-align:top"
        ),#end of tags$Div for texinput-ExcelSheetName
        
        tags$div(
            textInput(inputId = ns("ExcelSheetRange"),
                      label = "Range",
                      value = "C5:BN1000"),
            style = "display:inline-block"
        )#end of tags$div for textInput - sheetrange
    )
}


importExceldataServer <- function(id){
    moduleServer(id, function(input, output, session){
        observeEvent(input$ImportExcelFile, {
            req(input$ImportExcelFile)
            print(input$ImportExcelFile$datapath)
        })
    })  
}

importExceldataApp <- function(){
    ui <- fluidPage(
        mainPanel(
            actionButton(inputId = "AddExcelDataButton", label = "Click here to add Excel Data"),
        )#emd pf mainpanel
    )
    
    server <- function(input, output, session){
        observeEvent(input$AddExcelDataButton, {
            insertUI(selector = "#AddExcelDataButton", where = "afterEnd",
                     ui = importExceldataUI(paste0("file",input$AddExcelDataButton))) 
            importExceldataServer(paste0("file",input$AddExcelDataButton))
        })#end of observeEvent
        
    }
    
    shinyApp(ui, server)
}


importExceldataApp()

So to read the path of uploaded file, just use input$ImportExcelFile$datapath, datapath is the file location. Here in my code, I just print it out, you can do other things.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.