Add Select columns dynamically in R with ends_with

2019-09-02 14:38发布

问题:

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)

回答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)
}