The dqmagic package provides an R interface for the libmagic file type identification library similar to the Unix file command. This allows for file type identification based on a file’s content instead of it’s extension.
In order to install and use dqmagic you will need the libmagic
library
and headers:
- Debian/Ubuntu:
sudo apt-get install libmagic-dev
- Fedora/CentOS/RHEL:
sudo yum install file-devel
- OSX with homebrew:
brew install libmagic
At the moment dqmagic is not on CRAN, but you can install the current version via drat:
if (!requireNamespace("drat", quietly = TRUE)) install.packages("drat")
drat::addRepo("daqana")
install.packages("dqmagic")
This is a basic example which shows you how to determine the type of a file:
library(dqmagic)
file_type("DESCRIPTION")
#> [1] "ASCII text"
file_type("src/file.cpp")
#> [1] "C source, ASCII text"
And the same for the MIME type and encoding:
library(dqmagic)
file_type("DESCRIPTION", mime_type = TRUE, mime_encoding = TRUE)
#> [1] "text/plain; charset=us-ascii"
file_type("src/file.cpp", mime_type = TRUE, mime_encoding = TRUE)
#> [1] "text/x-c; charset=us-ascii"