Class: Datasets::RdatasetList

Inherits:
Dataset
  • Object
show all
Defined in:
lib/datasets/rdataset.rb

Defined Under Namespace

Classes: Record

Instance Attribute Summary

Attributes inherited from Dataset

#metadata

Instance Method Summary collapse

Methods inherited from Dataset

#clear_cache!, #to_table

Constructor Details

#initializeRdatasetList

Returns a new instance of RdatasetList.



19
20
21
22
23
24
25
26
27
# File 'lib/datasets/rdataset.rb', line 19

def initialize
  super
  @metadata.id = "rdataset-list"
  @metadata.name = "Rdataset"
  @metadata.url = "https://vincentarelbundock.github.io/Rdatasets/"
  @metadata.licenses = ["GPL-3"]
  @data_url = "https://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/datasets.csv"
  @data_path = cache_dir_path + "datasets.csv"
end

Instance Method Details

#each(&block) ⇒ Object



46
47
48
# File 'lib/datasets/rdataset.rb', line 46

def each(&block)
  filter(&block)
end

#filter(package: nil, dataset: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/datasets/rdataset.rb', line 29

def filter(package: nil, dataset: nil)
  return to_enum(__method__, package: package, dataset: dataset) unless block_given?

  conds = {}
  conds["Package"] = package if package
  conds["Item"]    = dataset if dataset
  if conds.empty?
    each_row {|row| yield Record.new(*row.fields) }
  else
    each_row do |row|
      if conds.all? {|k, v| row[k] == v }
        yield Record.new(*row.fields)
      end
    end
  end
end