Permalink
Browse files

source_url and source_gist: code improvements

  • Loading branch information...
1 parent 0f9f129 commit f32929b72f4ae634ab3eaa1d5bec03502d16b491 @kohske kohske committed Jan 23, 2012
Showing with 53 additions and 28 deletions.
  1. +1 −1 NAMESPACE
  2. +24 −8 R/run-source.r
  3. +4 −4 man/source_gist.Rd
  4. +0 −15 man/source_https.Rd
  5. +24 −0 man/source_url.Rd
View
2 NAMESPACE
@@ -28,7 +28,7 @@ export(run_examples)
export(show_news)
export(show_rd)
export(source_gist)
-export(source_https)
+export(source_url)
export(test)
export(wd)
importFrom(RCurl,ftpUpload)
View
32 R/run-source.r
@@ -1,23 +1,39 @@
-#' Run a script via https
+#' Run a script through some protocols such as http, https, ftp, etc.
+#'
+#' Internally, \code{source_url} calls \code{\link{getURL}} in
+#' \code{RCurl} package and then read the contents by
+#' \code{\link{textConnection}}, which is then \code{\link{source}}ed.
+#' See \code{?getURL} for the available protocol.
#'
#' @param url url
#' @param ... other options passed to \code{\link{source}}
#' @importFrom RCurl getURL
#' @export
-source_https <- function(url, ...) {
- message("Running: ", url)
- source(textConnection(getURL(url)), ...)
+#' @examples
+#' \dontrun{
+#' source_url("https://raw.github.com/gist/1654919/8161f74fb0ec26d1ba9fd54473a96f768ed76f56/test2.r")
+#' }
+source_url <- function(url, ...) {
+ contents <- getURL(url)
+ handle <- textConnection(contents)
+ on.exit(close(handle))
+ source(handle, ...)
}
#' Run a script on gist
#'
-#' \dQuote{Gist is a simple way to share snippets and pastes with others. All gists are git repositories, so they are automatically versioned, forkable and usable as a git repository. \url{https://gist.github.com/}}
+#' \dQuote{Gist is a simple way to share snippets and pastes with others.
+#' All gists are git repositories, so they are automatically versioned,
+#' forkable and usable as a git repository.}
+#' \url{https://gist.github.com/}
#'
#' A gist entry can have multiple code blocks (one file for one block).
-#' Gist is based on git, which means gist has commit histories (i.e., revisions). You can specify a commit by ginving SHA.
+#' Gist is based on git, which means gist has commit histories (i.e., revisions).
+#' You can specify a commit by ginving SHA.
#'
-#' @param entry either full url (character), gist ID (numeric or character of numeric).
-#' If only an entry ID is specified and the entry has multiple code block, the first entry is sourced.
+#' @param entry either full url (character), gist ID (numeric or character of
+#' numeric). If only an entry ID is specified and the entry has multiple code
+#' block, the first entry is sourced.
#' @param ... other options passed to \code{\link{source}}
#' @export
#' @examples
View
8 man/source_gist.Rd
@@ -6,17 +6,17 @@
}
\arguments{
\item{entry}{either full url (character), gist ID
- (numeric or character of numeric). If only an entry ID
- is specified and the entry has multiple code block, the
+ (numeric or character of numeric). If only an entry ID is
+ specified and the entry has multiple code block, the
first entry is sourced.}
\item{...}{other options passed to \code{\link{source}}}
}
\description{
\dQuote{Gist is a simple way to share snippets and pastes
- with others. All gists are git repositories, so they are
+ with others. All gists are git repositories, so they are
automatically versioned, forkable and usable as a git
- repository. \url{https://gist.github.com/}}
+ repository.} \url{https://gist.github.com/}
}
\details{
A gist entry can have multiple code blocks (one file for
View
15 man/source_https.Rd
@@ -1,15 +0,0 @@
-\name{source_https}
-\alias{source_https}
-\title{Run a script via https}
-\usage{
- source_https(url, ...)
-}
-\arguments{
- \item{url}{url}
-
- \item{...}{other options passed to \code{\link{source}}}
-}
-\description{
- Run a script via https
-}
-
View
24 man/source_url.Rd
@@ -0,0 +1,24 @@
+\name{source_url}
+\alias{source_url}
+\title{Run a script through some protocols such as http, https, ftp, etc.}
+\usage{
+ source_url(url, ...)
+}
+\arguments{
+ \item{url}{url}
+
+ \item{...}{other options passed to \code{\link{source}}}
+}
+\description{
+ Internally, \code{source_url} calls \code{\link{getURL}}
+ in \code{RCurl} package and then read the contents by
+ \code{\link{textConnection}}, which is then
+ \code{\link{source}}ed. See \code{?getURL} for the
+ available protocol.
+}
+\examples{
+\dontrun{
+source_url("https://raw.github.com/gist/1654919/8161f74fb0ec26d1ba9fd54473a96f768ed76f56/test2.r")
+}
+}
+

0 comments on commit f32929b

Please sign in to comment.