knitr-ruby 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 15629ec29dc2e3bcf286cb15f3f322b5d97e43c0
4
+ data.tar.gz: 9161c1f351e318d2ac388b914e74b8a2992ea0d7
5
+ SHA512:
6
+ metadata.gz: 6c090ba7bde986d22bf656d5d9afc923e22ba7a47db7e91ce79c1735b86fba70d45ea9a9b7308ae3e7bd9b4327e9f2d81445dc8221e9ada5e498781a28f1a921
7
+ data.tar.gz: a04b9e056073ef4e1656d2347f6f161cbcfe73ff5edde41867a65464a35c5d06c51f8b3f6c60b1cfa95e093c9981d4f335521b4d5f166a09809ff929718efca7
@@ -0,0 +1,25 @@
1
+ require 'open3'
2
+ require 'ostruct'
3
+
4
+ module KnitrRuby
5
+ class Knitr < OpenStruct
6
+
7
+ def knit(content)
8
+ dir = File.expand_path File.dirname(__FILE__)
9
+ command = "./knitrscript.R --args #{options}"
10
+ Open3::popen3(command, chdir: dir) do |stdin, stdout, stderr, wait_thr|
11
+ stdin.puts content
12
+ stdin.close
13
+
14
+ raise StandardError, "Error knitting: #{stderr.read}" if wait_thr.value.exitstatus > 0
15
+ content = stdout.read
16
+ end
17
+ end
18
+
19
+ def options
20
+ opts = chunk_options || {}
21
+ opts.map {|k,v| "#{k}=#{v}" }.join(" ")
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/Rscript
2
+ library("knitr")
3
+
4
+ ## process the command line arguments -
5
+ ## - args are separate strings containing each arg=value pair
6
+ args <- commandArgs(TRUE)[-1] ## grab arguments
7
+
8
+ ## Example arguments arranged as this character vector
9
+ ## args <- c("root.dir=", "fig.show=hold", "fig.path=../docs-gh-pages/figures/",
10
+ ## "fig.width=10", "fig.height=7", "dev=svg", "warning=false")
11
+
12
+ ## writeLines("args after capture")
13
+ ## writeLines("<pre>")
14
+ ## print(args)
15
+ ## writeLines("</pre>")
16
+
17
+ ## Handle the arguments
18
+ spl <- strsplit(args, "=") ## split each arg=val pair into arg name & value
19
+ nams <- sapply(spl, `[`, 1) ## extract argument names
20
+ vals <- sapply(spl, `[`, 2) ## extract argument values
21
+ ind <- grepl("[0-9]+", vals) ## index the numeric values
22
+ VALS <- as.list(vals)
23
+ VALS[ind] <- as.numeric(vals[ind]) ## convert numeric strings to numeric values
24
+ ## handle logical values
25
+ logi <- vals %in% c("false", "true", "FALSE", "TRUE")
26
+ if (any(logi)) {
27
+ VALS[logi] <- as.logical(vals[logi])
28
+ }
29
+ ## Missing values for argument pairs like `"root.dir="` some through
30
+ ## strsplit() as NA, so handle
31
+ nas <- is.na(vals)
32
+ VALS[nas] <- list(NULL)
33
+ ## remaining must be characters
34
+ take <- !(ind | logi | nas) ## select argument values not numeric or logicals
35
+ ## add `' '` around string values
36
+ VALS[take] <- paste("'", vals[take], "'", sep = "")
37
+
38
+ ## Other handling
39
+ lose <- which(nams == "root.dir") ## temp: drop the root.dir argument
40
+
41
+ ## writeLines("Argument names and values")
42
+ ## writeLines("<pre>")
43
+ ## print(args)
44
+ ## print(nams)
45
+ ## print(vals)
46
+ ## writeLines("</pre>")
47
+
48
+ ## process argument value pairs back into a single string with R code...
49
+ call <- paste("list(", paste(nams[-lose], VALS[-lose], sep = "=",
50
+ collapse = ", "), ")", sep = "")
51
+ ## ... which we then parse and evaluate to create a list containing named components for each
52
+ ## argument name and the object in each component is the respective argument value
53
+ ## writeLines("<pre>")
54
+ ## print(call)
55
+ ## writeLines("</pre>")
56
+
57
+ args <- eval(parse(text = call))
58
+
59
+ ## writeLines("<pre>")
60
+ ## print(args)
61
+ ## writeLines("</pre>")
62
+
63
+ tmp <- do.call(opts_chunk$set, args) ## use this list as arguments to chunk options function
64
+ ## writeLines("<pre>")
65
+ ## print(opts_chunk$get())
66
+ ## writeLines("</pre>")
67
+
68
+ ## hardcoded knitr package options - until we work out how best to have both package &
69
+ ## chunk options supplied via command line argument
70
+ opts_knit$set(base.url="http://ropensci.github.io/docs/", base.dir=".", root.dir=NULL, self.contained=FALSE, verbose=FALSE)
71
+
72
+ ## knit the document being processed; takes inpu from stdin and outputs to stdout
73
+ tmp <- knit(text = readLines(file("stdin")), output=stdout(), quiet=TRUE)
74
+
75
+ ## note that objects `tmp` are just to stop objects returned by function that doesn't need
76
+ ## to be assigned (do.call, knitr) spitting NULLs into the stdout stream.
@@ -0,0 +1,3 @@
1
+ module KnitrRuby
2
+ VERSION = "0.0.2"
3
+ end
data/lib/knitr-ruby.rb ADDED
@@ -0,0 +1 @@
1
+ require 'knitr-ruby/knitr'
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knitr-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Martin Fenner
8
+ - Gavin Simpson
9
+ - Carl Boettiger
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2014-04-09 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rspec
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '2.6'
36
+ type: :development
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '2.6'
43
+ description: A Ruby wrapper for the knitr R package which processes markdown with
44
+ embedded code.
45
+ email: mf@martinfenner.org
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - lib/knitr-ruby.rb
51
+ - lib/knitr-ruby/knitr.rb
52
+ - lib/knitr-ruby/knitrscript.R
53
+ - lib/knitr-ruby/version.rb
54
+ homepage: https://github.com/ropensci/knitr-ruby
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 1.3.6
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.2.2
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Ruby wrapper for knitr R package
78
+ test_files: []