csvTypeInfo: Get information about CSV columns and number of rows

csvTypeInfoR Documentation

Get information about CSV columns and number of rows

Description

This function reads the first few rows of a CSV (comma-separate value) file and determines the classes of its columns and also gets the number of rows in the file. This gives us information about the types and dimensions of the contents which we can use in static analysis.

This assumes the first rows are representative of the column types.

Usage

csvTypeInfo(file, nrow = 10, ...)

Arguments

file

the name of the CSV file, or a connection object, or a language/code object.

nrow

the number of rows to read to determine the types of the columns. The larger the number, the greater the certainty of the resulting types but the slower the computation.

...

additional arguments for methods.

Value

A list of class TabularTypeInfo with elements

dim

a length 2 integer vector giving the number of rows and columns. The number of rows may be NA.

elNames

the names of the columns in the would-be data.frame

elementClasses

a list of character vectors with an element for each of the column names in elNames.

elementTypes

a character vector with the corresponding typeof for each of the columns in the would-be data.frame.

Author(s)

Duncan Temple Lang

See Also

RTypeInference package at https://github.com/duncantl/RTypeInference.

Examples

f = system.file("sampleCode/simple.csv", package = "CodeAnalysis")
csvTypeInfo(f)

# Gymnastics here that we would not ordinarily have to do except for
# we are evaluating this from within a help file.

cur = getwd()
setwd(dirname(f))

csvTypeInfo(quote(read.csv("simple.csv", colClasses = c("numeric", "NULL", "character"))))

setwd(cur)

# again, gymnastics to create the expression for the help file.

e = substitute(read.csv(f, colClasses = c("numeric", "NULL", "character")), list(f = f))
csvTypeInfo(e)

duncantl/CodeAnalysis documentation built on March 1, 2025, 9:54 p.m.