How to upload several xpt files and filter the rows with specific values in R shiny

Viewed 21

The R shiny app below reads a single xpt file and shows the datatable. There is also a search box (default) that can be used to filter with any column values.

I would like to upload numerous xpt files instead of a single one, and I would like to keep the same formatting (search field for filtering rows with any specific values).

example xpts below:

xpt1:

ID   SUB_CATEGORY      SUB_VALUE
1           a            2.5
2           b            4.5
3           c            10

xpt2:

ID   MAIN_CATEGORY    MAIN_POINTS
1           a              10
2           d              9.6
3           e              8.8

It should display the datatable in the format below after uploading both xpts (xpt1 and xpt2).

Expected Datatable display

ID   SUB_CATEGORY      SUB_VALUE
1           a            2.5
2           b            4.5
3           c            10
ID   MAIN_CATEGORY    MAIN_POINTS
1           a              10
2           d              9.6
3           e              8.8

The expected result while searching/filtering with value '10':

3           c            10
1           a            10

Code

library(tidyverse)
library(shiny)
library(DT)
library(shinyWidgets)
library(haven)

ui <- fluidPage(
  
  h1("Data"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File",
                multiple = TRUE,
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv", ".xpt"
                )),
      downloadButton("download", "Download  csv")
    ),
    mainPanel(
      h4("Table 1: xpt"),
      dataTableOutput("xpt_dto")
    )
  )
)

server <- function(input, output, session) {
  xpt_dto <- reactive({
    file <- input$file1
    ext <- tools::file_ext(file$datapath)
    
    req(file)
    validate(need(ext == "xpt", "upload xpt file"))
    
    read_xpt(file$datapath)
  })
  
  thedata <- reactive({
    xpt_dto()
    
  })
  
  output$xpt_dto <- renderDataTable({
    thedata() %>%
      datatable(
        extensions = "Buttons",
        options = list(
          
          
        ),
        
        rownames = FALSE
      )
  })
  
  output$download1 <- downloadHandler(
    filename = function() {
      paste("study_", Sys.Date(), ".csv", sep = "")
    },
    content = function(file) {
      write.csv(thedata(), file)
    }
  )
}

shinyApp(ui, server)

Does someone assist me with this?

0 Answers
Related