Permalink
Browse files

example with testcache

  • Loading branch information...
1 parent a016431 commit b5fc66ddfa690416a3d10c299f3245fe6e660416 @cboettig committed Feb 12, 2013
View
20 testcache/DESCRIPTION
@@ -0,0 +1,20 @@
+Package: testcache
+Description: test authentication caching
+Title: test of authentication caching
+Version: 0.0-1
+License: CC0
+Authors@R: person("Carl", "Boettiger", role = c("aut", "cre"), email =
+ "cboettig@gmail.com")
+VignetteBuilder: knitr
+Suggests:
+ knitr
+Depends:
+ R (>= 2.15)
+Imports:
+ RJSONIO,
+ httr (>= 0.2),
+ plyr,
+ XML
+Collate:
+ 'fs_auth.R'
+ 'zzz.R'
View
2 testcache/NAMESPACE
@@ -0,0 +1,2 @@
+export(auth)
+export(get_auth)
View
25 testcache/R/fs_auth.R
@@ -0,0 +1,25 @@
+
+#' Auth caching demo function
+#'
+#' @param x a parameter value x
+#' @return stores x in envirnoment AuthCache and returns value of x invisbily
+#' @export
+auth <-
+function(x){
+ assign('x', x, envir=AuthCache)
+ invisible(x)
+}
+
+
+
+#' Auth caching demo function
+#'
+#' @return returns x from envirnoment AuthCache
+#' @export
+get_auth <-
+function(){
+ get('x', envir=AuthCache)
+}
+
+
+
View
1 testcache/R/zzz.R
@@ -0,0 +1 @@
+AuthCache <- new.env(hash=TRUE)
View
60 testcache/inst/doc/example.Rmd
@@ -0,0 +1,60 @@
+# Example of authentication with caching credentials
+
+<!--
+%\VignetteEngine{knitr}
+%\VignetteIndexEntry{Caching Tutorial}
+-->
+
+* Create the file [zzz.R]() that initiates the cache:
+
+```r
+AuthCache <- new.env(hash=TRUE)
+```
+
+
+* Create the file [auth.R]() that provides the functions to assign and get from cache:
+
+```r
+#' cache demo fn
+#' @export
+auth <-
+function(x){
+ assign('x', x, envir=AuthCache)
+ invisible(x)
+}
+```
+
+```r
+#' cache demo extract fn
+#' @export
+get_auth <-
+function(){
+ get('x', envir=AuthCache)
+}
+```
+
+
+
+Note that after loading the package, we can immediately assign and recall from the cache object:
+
+```{r}
+library(testcache)
+auth(5)
+get_auth()
+```
+
+Note that while the internal functions can access this cache just fine, we cannot access the cache object directly ourselves (just as we cannot access internal functions without prepending the namespace).
+
+This fails:
+
+```{r}
+ get('x', envir=AuthCache)
+```
+
+While this works.
+
+```{r}
+ get('x', envir=testcache:::AuthCache)
+```
+
+
View
73 testcache/inst/doc/example.md
@@ -0,0 +1,73 @@
+# Example of authentication with caching credentials
+
+* Create the file [zzz.R]() that initiates the cache:
+
+```r
+AuthCache <- new.env(hash=TRUE)
+```
+
+
+* Create the file [auth.R]() that provides the functions to assign and get from cache:
+
+```r
+#' cache demo fn
+#' @export
+auth <-
+function(x){
+ assign('x', x, envir=AuthCache)
+ invisible(x)
+}
+```
+
+```r
+#' cache demo extract fn
+#' @export
+get_auth <-
+function(){
+ get('x', envir=AuthCache)
+}
+```
+
+
+
+Note that after loading the package, we can immediately assign and recall from the cache object:
+
+
+```r
+library(testcache)
+auth(5)
+get_auth()
+```
+
+```
+## [1] 5
+```
+
+
+Note that while the internal functions can access this cache just fine, we cannot access the cache object directly ourselves (just as we cannot access internal functions without prepending the namespace).
+
+This fails:
+
+
+```r
+get("x", envir = AuthCache)
+```
+
+```
+## Error: object 'AuthCache' not found
+```
+
+
+While this works.
+
+
+```r
+get("x", envir = testcache:::AuthCache)
+```
+
+```
+## [1] 5
+```
+
+
+
View
17 testcache/man/auth.Rd
@@ -0,0 +1,17 @@
+\name{auth}
+\alias{auth}
+\title{Auth caching demo function}
+\usage{
+ auth(x)
+}
+\arguments{
+ \item{x}{a parameter value x}
+}
+\value{
+ stores x in envirnoment AuthCache and returns value of x
+ invisbily
+}
+\description{
+ Auth caching demo function
+}
+
View
13 testcache/man/get_auth.Rd
@@ -0,0 +1,13 @@
+\name{get_auth}
+\alias{get_auth}
+\title{Auth caching demo function}
+\usage{
+ get_auth()
+}
+\value{
+ returns x from envirnoment AuthCache
+}
+\description{
+ Auth caching demo function
+}
+

0 comments on commit b5fc66d

Please sign in to comment.