Home > Back-end >  Turn off warning specific warning in R
Turn off warning specific warning in R

Time:07-29

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?

CodePudding user response:

Those are messages, not warnings, so you should use

suppressMessages({
  imported_templates <- lapply(files, read_excel_allsheets)
})

CodePudding user response:

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)

CodePudding user response:

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)
  •  Tags:  
  • r
  • Related