2

Below you can see my function that imports data from different types of Excel sheets.

library(dplyr)
library(readxl)
library(data.table)
library(purrr)
library(tidyr)

        read_excel_allsheets <- function(filename) { 
          sheets <- readxl::excel_sheets(filename) 
          x <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X)) 
          names(x) <- sheets 
          x 
        } 
        files <- list.files(path = "C:/Data", 
                            pattern = "*.xlsx",
                            full.names = TRUE)
        imported_templates <- lapply(files, read_excel_allsheets)
        names(imported_templates) <- basename(files)

When I imported data from Excel sheets I receive a message like the message below: New names:

* `` -> ...4
* `` -> ...5
* `` -> ...7
* `` -> ...8
* `` -> ...10
* ...

This is very boring because I need to import around 1000 tables. I tried to turn off this message with this line of code but the message is still here.

library(readxl, warn.conflicts=FALSE)

So can anybody help me how to solve this problem and turn off this warning?

silent_hunter
  • 2,224
  • 1
  • 12
  • 30
  • https://stackoverflow.com/questions/16194212/how-to-suppress-warnings-globally-in-an-r-script: `suppressWarnings({ your code })` – Julian Jul 28 '22 at 07:58
  • Already tried it before posting.Is not work for me – silent_hunter Jul 28 '22 at 08:00
  • "Boring" does not mean uninformative. Best practice would be to modify your code to handle situations that generate the message. But we have no details of your input files, so cannot provide concrete advice. – Limey Jul 28 '22 at 08:11

3 Answers3

3

Those are messages, not warnings, so you should use

suppressMessages({
  imported_templates <- lapply(files, read_excel_allsheets)
})
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
2

It is not a warning, it is a message from vctrs::vec_as_names(), you can turn it off with

options(rlib_name_repair_verbosity='quiet')

Default behaviour can later be restored with

options(rlib_name_repair_verbosity=NULL)
Robert Hacken
  • 3,878
  • 1
  • 13
  • 15
1

Try this

library(dplyr)
library(readxl)
library(data.table)
library(purrr)
library(tidyr)
oldw <- getOption("warn")
options(warn = -1)

read_excel_allsheets <- function(filename) { 
  sheets <- readxl::excel_sheets(filename) 
  x <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X)) 
  names(x) <- sheets
  x
} 
files <- list.files(path = "C:/R/data", 
                    pattern = "*.xlsx",
                    full.names = TRUE)

imported_templates <- suppressMessages(lapply(files, read_excel_allsheets) )
names(imported_templates) <- basename(files)

options(warn = oldw)