0

Using stackoverflow, I created a shiny app which uploads a csv file and then displays a datatable.

After selecting columns dynamically, where some columns have "_down" end.

I require help in shortening the dataframe (as in the code below) and also remove duplicates by ID column (if present).

    # install.packages("shiny")
    # install.packages("DT")
    # install.packages("shinycssloaders")
    library(DT)
    library(shiny)
    library(shinycssloaders)

UI code

    ##Creating the UI as a fluidPage,
    ##fluidPage allows scaling components of the browser in realtime to fill all available broswer width
    ##This is standard
    ui <- fluidPage(
      
      # Title of app
      titlePanel("Upload file to table"),
      
        
        # Main panel for displaying outputs
        mainPanel(
          
          #fileInput with acceptance of text/csv and more
          fileInput('file', 'Choose file to upload',
                    accept = c(
                      'text/csv',
                      'text/comma-separated-values',
                      'text/tab-separated-values',
                      'text/plain',
                      '.csv',
                      '.tsv',
                      '.html'
                    )),
          
          
          # Output: datatable
          DT::dataTableOutput("data_as_table")%>%withSpinner(),
          
          #Download button
          downloadButton("downloadData", "Download")
          
        )
    )
    
    

Server Code

Creating server

    server <- function(input, output) {
    
    
      #Data is a reactive element meaning it will update when the reactive input inside it change
      #Data will update when input$file changes
      #input$file is the uploaded file (se fileInput in ui)
      data <-reactive({
        
        #Store input$file as inFile 
        inFile <- input$file
    
        #if its empty return nothing
        if (is.null(inFile))
          return(NULL)
        
        #read in the file as a csv, with headers, comma seperated
        dd = read.csv(inFile$datapath, header = T,
                 sep = ",")
        dd  = as.data.frame(dd)
 
        #Shortening dataframe 
        #dd= dd[apply(dd[, endsWith(colnames(dd), "_down")], 1, function(x) any(x == "TRUE")), ]


        #Remove duplicates by ID column, and show unique
        #xxx
        return(dd)
      })
    

     
    #Make the output data_as_table a datatable containing the reactive element data
     output$data_as_table<-DT::renderDataTable({
       data()
     })
    
    
     # Downloadable csv of reactive data() object
     output$downloadData <- downloadHandler(
       filename = function() {
         paste("Download", Sys.date(), ".csv", sep = "")
       },
       content = function(file) {
         write.csv(data(), file, row.names = FALSE)
       }
     )
    }
    
    #Launch shiny app
    shinyApp(ui = ui, server = server)
Community
  • 1
  • 1
Beginner
  • 262
  • 1
  • 4
  • 12

1 Answers1

1

You can remove duplicates using dplyr::distinct. It'll only keep the first instance of the ID and remove others. In your case add this before return(dd) in data reactive -

if("ID" %in% names(dd)) {
    dd <- dplyr::distinct(dd, ID, .keep_all = T)
}
Shree
  • 10,835
  • 1
  • 14
  • 36
  • thank you. Unfortunately I get error.67: 68: for("ID" ^ Possible missing comma at: 68: for("ID" in names(dd)) { – Beginner Oct 18 '18 at 14:44
  • there was a typo - I used `in` instead of `%in%`. Try the updated code. – Shree Oct 18 '18 at 14:45
  • it works :). How do I make this work in the code as well, as I struggled---> dd= dd[apply(dd[, endsWith(colnames(dd), "_down")], 1, function(x) any(x == "TRUE")), ] – Beginner Oct 18 '18 at 14:55