need to import multiple CSVs file with selected columns with fread() function

Hi Team,

I have suppose 15 CSV files under a folder with same number of columns and with the same name and I want to import all the CSV at one go with fread() function.

I use the below code:

library(data.table)
df <- 
  list.files(path = "/Users/admin/apps/csv-courses/", pattern = "*.csv") %>% 
  map_df(~fread(.))
df

In the above fread() way all columns are coming into the DF. Please help me where to put the Select option inside the function, also please explain me why there is dot(.) inside the braces, bcoz if I am removing that, the query not all working. Please help.

Thanks,
Preetiranjan

I would avoid map_df (or map_dfr) for this case:

library(data.table)

df <- rbindlist(
  lapply(
    list.files(path = "/Users/admin/apps/csv-courses/", pattern = "*.csv"), 
    fread, 
    select = c("col1", "col5")
  )
)
1 Like

Wow, Martin it works perfectly. Thanks a lot.

Hey @martin.R ,

Can you please help me with a suggestion...I am trying to webout a Pivot table through R-shiny and trying to export it to excel format.

Can you please help me in the below code how I can make an use of write_xlsx() function instead of write_xlsx2() function.

library(tidyverse)
library(shiny)
library(shinydashboard)
library(rpivotTable)
library(shinyjs)
library(dplyr)
library(rvest)
library(writexl)
library(readxl)

ui <-
  dashboardPage(
    skin = "green",
    
    dashboardHeader(
      title      = "Car Data",
      titleWidth = 280
    ),
    
    dashboardSidebar(
      width = 280,
      sidebarMenu(
        menuItem(text = "Output", tabName = "Out1")
      ),
      
      hr(),
      useShinyjs(),
      actionButton(inputId = "btnExport", "Export Table")
      
      
    ),
    dashboardBody(
      tabItems(
        tabItem(
          tabName = "Out1",
          fluidRow(column(width = 10, strong("Data")), align = "center"),
          br(),
          fluidRow(rpivotTableOutput("Data1"))
        ) 
      )
    )
  )


server <- 
  function(input, output){
    #library(rpivotTable)
    
    output$Data1 <- 
      renderRpivotTable(
        rpivotTable(
          data = mtcars
          , rows = "cyl"
          , cols = "gear"
          , height = "780px"
        )
      )
    
    
    observeEvent(input$btnExport,{
      
      runjs(
        "
        var tblhtml=document.getElementsByClassName('pvtRendererArea')[0].innerHTML;
        console.log(tblhtml)
        //set shiny Input value to read reactively from R 
        Shiny.setInputValue('tblvar_shiny', tblhtml);
        "
      )
      
    } )
    
     #save pivot table to xls file 
    observeEvent(input$tblvar_shiny,
                 { 
                     minimal_html(input$tblvar_shiny) %>% 
                     html_element("table")   %>% 
                     html_table() %>% 
                     as.data.frame() %>% 
                     **write.xlsx2(file="000pivot_final.xls")**
                   })
  } 

runApp(
  list(ui = ui, server = server)  , launch.browser = TRUE
)

I think mostly people will go for JDK installation in order to resolve this, but I just wanted to know can we use write_xlsx() inside shiny.

Thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.