Module: R::Arrow

Defined in:
lib/R_interface/r_arrow.rb

Class Method Summary collapse

Class Method Details

.dataset(path) ⇒ R::Object

Open a Parquet/Feather directory or file as an Arrow Dataset using arrow::open_dataset().

Parameters:

  • path (String)

    file or directory path visible to R

Returns:

  • (R::Object)

    handle to an Arrow Dataset in R



71
72
73
# File 'lib/R_interface/r_arrow.rb', line 71

def self.dataset(path)
  R.arrow___open_dataset(path)
end

.from_ruby_batches(enum_or_array) ⇒ R::Object

Build an Arrow table in R from Ruby-produced batches.

Input shapes:

  • Array: each hash is a row
  • Array<Array>: each inner array is a row batch
  • Any Enumerable yielding Hash rows or Array batches

This keeps batch construction on the Ruby side and creates one R data.frame from the merged column vectors, then converts it to Arrow Table.

Parameters:

  • enum_or_array (Enumerable)

Returns:

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/R_interface/r_arrow.rb', line 26

def self.from_ruby_batches(enum_or_array)
  rows = []
  enum_or_array.each do |batch|
    if batch.is_a?(Hash)
      rows << batch
    elsif batch.respond_to?(:each)
      batch.each do |row|
        unless row.is_a?(Hash)
          raise ArgumentError, 'each row must be a Hash'
        end
        rows << row
      end
    else
      raise ArgumentError, 'batches must yield Hash rows or arrays of Hash rows'
    end
  end

  raise ArgumentError, 'from_ruby_batches requires at least one row' if rows.empty?

  keys = rows.flat_map(&:keys).map(&:to_s).uniq
  columns = keys.each_with_object({}) { |k, h| h[k.to_sym] = [] }

  rows.each do |row|
    key_map = row.each_with_object({}) { |(k, v), h| h[k.to_s] = v }
    keys.each { |k| columns[k.to_sym] << key_map[k] }
  end

  df = R::Support.exec_function('data.frame', columns)
  table_from(df)
end

.read_feather(path) ⇒ R::DataFrame

Read a Feather file into an R data.frame/tibble using read_feather().

Parameters:

  • path (String)

Returns:



79
80
81
# File 'lib/R_interface/r_arrow.rb', line 79

def self.read_feather(path)
  R.arrow___read_feather(path)
end

.read_parquet(path) ⇒ R::DataFrame

Read a Parquet file into an R data.frame/tibble using read_parquet().

Parameters:

  • path (String)

Returns:



97
98
99
# File 'lib/R_interface/r_arrow.rb', line 97

def self.read_parquet(path)
  R.arrow___read_parquet(path)
end

.table_from(r_df) ⇒ R::Object

Create an Arrow Table from an R data.frame (or tibble) handle. Uses arrow::as_arrow_table() on the R side.

Parameters:

Returns:

  • (R::Object)

    handle to an Arrow Table in R



62
63
64
# File 'lib/R_interface/r_arrow.rb', line 62

def self.table_from(r_df)
  R.arrow___as_arrow_table(r_df)
end

.write_feather(r_df, path) ⇒ nil

Write an R data.frame/tibble to a Feather file using write_feather().

Parameters:

Returns:

  • (nil)


88
89
90
91
# File 'lib/R_interface/r_arrow.rb', line 88

def self.write_feather(r_df, path)
  R.arrow___write_feather(r_df, path)
  nil
end

.write_parquet(r_df, path) ⇒ nil

Write an R data.frame/tibble to a Parquet file using write_parquet().

Parameters:

Returns:

  • (nil)


106
107
108
109
# File 'lib/R_interface/r_arrow.rb', line 106

def self.write_parquet(r_df, path)
  R.arrow___write_parquet(r_df, path)
  nil
end