Module: TopStmtToR
- Includes:
- BlockToRSupport
- Defined in:
- lib/statsailr/block_to_r/sts_block_to_r.rb,
lib/statsailr/block_to_r/top_stmt/top_stmt_to_r_func.rb
Class Method Summary collapse
- .create_function(blk) ⇒ Object
- .create_r_func_for_getwd(opts) ⇒ Object
- .create_r_func_for_read(opts) ⇒ Object
- .create_r_func_for_save(opts) ⇒ Object
- .create_r_func_for_setwd(opts) ⇒ Object
- .read_csv(data_path, opts) ⇒ Object
- .read_rda(data_path, opts) ⇒ Object
- .read_rds(data_path, opts) ⇒ Object
- .save_csv(r_var, data_path) ⇒ Object
- .save_rda(r_var, data_path) ⇒ Object
- .save_rds(r_var, data_path) ⇒ Object
Class Method Details
.create_function(blk) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/statsailr/block_to_r/sts_block_to_r.rb', line 9 def self.create_function( blk ) r_func = nil case blk.command when /^(\w+)$/ method_to_create_r_function = "create_r_func_for_" + $1.downcase if respond_to? method_to_create_r_function r_func = send( method_to_create_r_function , blk.opts) else raise "#{method_to_create_r_function} cannot be found. Unknown top level command: #{blk.command}" end else raise "Invalid TOPLEVEL command name: " + blk.command end puts "The following options are not used in " + blk.command + " : " + blk.opts.keys.join(" ") if ! blk.opts.empty? return r_func end |
.create_r_func_for_getwd(opts) ⇒ Object
163 164 165 166 167 |
# File 'lib/statsailr/block_to_r/top_stmt/top_stmt_to_r_func.rb', line 163 def self.create_r_func_for_getwd(opts) puts "Current working directory" r_func = RBridge.create_function_call( "print", {"x" => RBridge.create_function_call( "getwd" , {} )}) return r_func end |
.create_r_func_for_read(opts) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/statsailr/block_to_r/top_stmt/top_stmt_to_r_func.rb', line 60 def self.create_r_func_for_read(opts) case when builtin_data_name = opts["builtin"] # builtin opts.delete("builtin"); opts.delete("file") puts "Built-in dataset becomes available using data(): #{builtin_data_name.to_s}" r_func = RBridge.create_function_call( "data" , {"" => RBridge.create_strvec([ builtin_data_name.to_s ])} ) when file_path = opts["file"] # csv opts.delete("builtin"); opts.delete("file") if type = opts["type"] opts.delete("type") case type.downcase when "rds" r_func = read_rds(file_path, opts) when "rdata" r_func = read_rda(file_path, opts) when "csv" r_func = read_csv(file_path, opts) else raise "TOPLEVEL READ statement currently supports rds(single r object), rdata or csv for type option. #{type} is specified." end else case file_path when /\.(rds)$/i # i is option to ignore cases r_func = read_rds(file_path, opts) when /\.(rdata|rda)$/i r_func = read_rda(file_path, opts) when /\.csv$/i r_func = read_csv(file_path, opts) else raise "TOP level READ statement tried to idenfity file type from 'file=' option, but failed. File name preferably ends with .rds, .RData(.rdata) or .csv, or specify file type." end end else raise "TOPLEVEL READ statement requires 'builtin=' or 'file=' option." end end |
.create_r_func_for_save(opts) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/statsailr/block_to_r/top_stmt/top_stmt_to_r_func.rb', line 121 def self.create_r_func_for_save(opts) if opts["file"] file_path = opts["file"] else raise "TOPLEVEL SAVE statement require 'file=' option. 'type=' option is optional when file extension matches file type." end if opts["type"] type = opts["type"].downcase end opts.delete("file"); opts.delete("type") if ! opts.empty? dataset_to_save = opts.filter(){|k,v| v.nil? }.keys dataset_to_save.each{|elem| opts.delete(elem) } else raise "Dataset should be specified" end case type when "rds" r_func = save_rds(dataset_to_save, file_path) when "rdata" r_func = save_rda(dataset_to_save, file_path) when "csv" r_func = save_csv(dataset_to_save, file_path) else case file_path when /\.rds$/i r_func = save_rds(dataset_to_save, file_path) when /\.(rda|rdata)$/i r_func = save_rda(dataset_to_save, file_path) when /\.csv$/i r_func = save_csv(dataset_to_save, file_path) else raise "TOP level SAVE statement tried to idenfity file type from file option, but failed. File name preferably ends with .rds, .rda (.rdata) or .csv, or specify file type." end end return r_func end |
.create_r_func_for_setwd(opts) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/statsailr/block_to_r/top_stmt/top_stmt_to_r_func.rb', line 169 def self.create_r_func_for_setwd(opts) opts.select!(){|k,v| v.nil? } if opts.empty? raise "SETWD requires String keyword that specifies directory to which working diretory moves." end puts "Setting new working directory" dir_path = opts.keys[0] opts.delete(dir_path) r_func = RBridge.create_function_call( "setwd" , {"dir" => RBridge.create_strvec([dir_path])} ) return r_func end |
.read_csv(data_path, opts) ⇒ Object
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 56 57 58 |
# File 'lib/statsailr/block_to_r/top_stmt/top_stmt_to_r_func.rb', line 28 def self.read_csv(data_path, opts) opt_hash = {} if header = opts["header"] opts.delete("header") opt_hash = {"header" => RBridge.create_lglvec([header]) } else opt_hash = {"header" => RBridge.create_lglvec([true]) } end available_opts = ["sep","na.strings","skip"] opts.filter!(){|k,v| if available_opts.include?(k) opt_hash[k] = RBridge.create_vec( [v] ) end ! available_opts.include?(k) } if as_name = opts["as"] opts.delete("as") puts "Read #{data_path} using read.csv(), which uses #{as_name} for dataset name." read_func = RBridge.create_function_call( "read.csv" , {"file" => RBridge.create_strvec([data_path])}.merge(opt_hash) ) r_func = RBridge.create_assign_function( as_name.to_s , read_func ) else basename = File.basename(data_path, File.extname(data_path)) as_name = basename.gsub(/\s/, '_').downcase puts "Read #{data_path} using read.csv(), which uses #{as_name} for dataset name (created from filename)." read_func = RBridge.create_function_call( "read.csv" , {"file" => RBridge.create_strvec([data_path])}.merge(opt_hash) ) r_func = RBridge.create_assign_function( as_name , read_func ) end end |
.read_rda(data_path, opts) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/statsailr/block_to_r/top_stmt/top_stmt_to_r_func.rb', line 18 def self.read_rda(data_path, opts) if as_name = opts["as"] opts.delete("as") puts "Read #{data_path} using laod(), which does not rename object when importing. 'as' option is ignored." end r_func = RBridge.create_function_call( "load" , {"file" => RBridge.create_strvec([data_path])} ) return(r_func) end |
.read_rds(data_path, opts) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/statsailr/block_to_r/top_stmt/top_stmt_to_r_func.rb', line 2 def self.read_rds(data_path, opts) if as_name = opts["as"] opts.delete("as") puts "Read #{data_path} using readRDS(), which uses #{as_name} for dataset name." read_func = RBridge.create_function_call( "readRDS" , {"file" => RBridge.create_strvec([data_path])} ) r_func = RBridge.create_assign_function( as_name.to_s , read_func ) else basename = File.basename(data_path, File.extname(data_path)) as_name = basename.gsub(/\s/, '_').downcase puts "Read #{data_path} using readRDS(), which uses #{as_name} for dataset name (created from filename)." read_func = RBridge.create_function_call( "readRDS" , {"file" => RBridge.create_strvec([data_path])} ) r_func = RBridge.create_assign_function( as_name , read_func ) end return(r_func) end |
.save_csv(r_var, data_path) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/statsailr/block_to_r/top_stmt/top_stmt_to_r_func.rb', line 111 def self.save_csv(r_var, data_path) puts "SAVE #{data_path} using write.csv()" if(r_var.size != 1) puts "Only one dataset can be specified. Others are ignored." end r_var = r_var[0] save_csv_func = RBridge.create_function_call( "write.csv" , {"x" => RBridge::SymbolR.new(r_var).to_r_symbol, "file" => RBridge.create_strvec([data_path])} ) return save_csv_func end |
.save_rda(r_var, data_path) ⇒ Object
106 107 108 109 110 |
# File 'lib/statsailr/block_to_r/top_stmt/top_stmt_to_r_func.rb', line 106 def self.save_rda(r_var, data_path) puts "SAVE #{data_path} using save()" save_rds_func = RBridge.create_function_call( "save" , {"" => RBridge::create_strvec(r_var), "file" => RBridge.create_strvec([data_path])} ) return save_rds_func end |
.save_rds(r_var, data_path) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/statsailr/block_to_r/top_stmt/top_stmt_to_r_func.rb', line 97 def self.save_rds(r_var, data_path) puts "SAVE #{data_path} using saveRDS()" if(r_var.size != 1) puts "Only one dataset can be specified. Others are ignored." end r_var = r_var[0] save_rds_func = RBridge.create_function_call( "saveRDS" , {"object" => RBridge::SymbolR.new(r_var).to_r_symbol, "file" => RBridge.create_strvec([data_path])} ) return save_rds_func end |