Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- introduce a back and forward iconv step for every conversion

- additional twitter_db example

	geändert:   R4CouchDB/DESCRIPTION
	geändert:   R4CouchDB/R/cdbAddDoc.R
	geändert:   R4CouchDB/R/cdbIni.R
	geändert:   R4CouchDB/R/cdbMakeDB.R
	geändert:   R4CouchDB/R/cdbUpdateDoc.R
	neue Datei:   R4CouchDB_0.2.tar.gz
	umbenannt:    example/example.R -> example/test-session.R
	neue Datei:   example/twitter-db.R
  • Loading branch information...
commit d38a5f461020e7c6f157749fe5316b1475172739 1 parent 24b0f11
@wactbprot authored
View
10 R4CouchDB/DESCRIPTION
@@ -1,21 +1,21 @@
Package: R4CouchDB
Type: Package
Title: A R convenience layer for CouchDB
-Version: 0.1.2
-Date: 2013/01/29
+Version: 0.2
+Date: 2013/05/19
Author: wactbprot
Maintainer: <thsteinbock@web.de>
Description: The R4CouchDB package provides a collection of functions for
basic database and document management operations such as add and
delete. Every cdbFunction() gets and emits a list containing the
- connection set up. Such a list can be generated by cdbIni().
+ connection setup. Such a list can be generated by cdbIni().
License: BSD
LazyLoad: yes
Depends:
R (>= 2.7.0),
bitops,
- RCurl(>= 1.6),
- RJSONIO (>= 0.95)
+ RCurl(>= 1.95),
+ RJSONIO (>= 1.0)
Collate:
'cdbIni.R'
'cdbAddAttachment.R'
View
7 R4CouchDB/R/cdbAddDoc.R
@@ -34,11 +34,12 @@ cdbAddDoc <- function( cdb){
cdb$DBName,"/",
cdb$id,
sep="")
-
+ pf <-cdb$toJSON(cdb$dataList)
+
res <- getURL(adrString,
customrequest = 'PUT',
- postfields = cdb$toJSON(cdb$dataList),
- httpheader=c('Content-Type: application/json'),
+ postfields = pf,
+ httpheader=c('Content-Type: application/json;charset=utf-8'),
.opts =cdb$opts(cdb))
res <- cdb$fromJSON( res )
View
28 R4CouchDB/R/cdbIni.R
@@ -71,11 +71,22 @@ cdbIni <- function(serverName="localhost",port="5984",dbname=""){
}
cdb$baseUrl <- function(cdb){
- baseUrl <- paste(cdb$prot,"://",cdb$serverName,":",cdb$port,"/", sep="")
- return(baseUrl)
+ return(paste(cdb$prot,
+ "://",
+ cdb$serverName,
+ ":",
+ cdb$port,
+ "/",
+ sep="")
+ )
}
cdb$fromJSON <- function(jsn){
+ jsn <- iconv(jsn,
+ cdb$serverEnc,
+ cdb$localEnc,
+ sub=cdb$encSub)
+
return(fromJSON(jsn,
nullValue = NA,
simplify = FALSE,
@@ -83,20 +94,17 @@ cdbIni <- function(serverName="localhost",port="5984",dbname=""){
}
cdb$toJSON <- function(lst){
- jsn <- toJSON(lst, collapse = "")
- ## couchdb has a right to get proper
- ## json. bad requests often came from
- ## stuff like \xa0\xbc\xed. Here we say
- ## gtfo and replace it with cdb$encSub
+ jsn <- toJSON(lst,
+ collapse = "")
jsn <- iconv(jsn,
cdb$localEnc,
cdb$serverEnc,
sub=cdb$encSub)
+
## one can {"a":"\r"} have in the
- ## database but one can not send it back
- ## in this way. A \r is here replaced by \\r
+ ## database but one can not send it
+ ## this way. A \r is here replaced by \\r
## resulting in \r in the database
-
jsn <- gsub("\\r","\\\\r",jsn)
return(jsn)
}
View
4 R4CouchDB/R/cdbMakeDB.R
@@ -29,13 +29,13 @@ cdbMakeDB <- function(cdb){
adrString <- paste(cdb$baseUrl(cdb),
cdb$newDBName,
sep="")
-
+
res <- getURL(adrString,
customrequest = "PUT",
curl=cdb$curl,
.opts =cdb$opts(cdb))
- cdb$DBName <- cdb$newDBName
+ cdb$DBName <- cdb$newDBName
cdb$newDBName <- ""
return(cdb$checkRes(cdb,res))
View
2  R4CouchDB/R/cdbUpdateDoc.R
@@ -47,7 +47,7 @@ cdbUpdateDoc <- function( cdb){
curl=cdb$curl,
url = adrString,
postfields = cdb$toJSON(cdb$dataList),
- httpheader=c('Content-Type: application/json'),
+ httpheader=c('Content-Type: application/json;charset=utf-8'),
.opts =cdb$opts(cdb))
cdb <- cdb$checkRes(cdb,res)
View
BIN  R4CouchDB_0.2.tar.gz
Binary file not shown
View
52 example/example.R → example/test-session.R
@@ -2,24 +2,27 @@
#' R4CouchDB example session
#'
#' @author wactbprot
-
-#' in this file we have no need to
-#' install the R4Couchdb package
-srcPath <- "../R4CouchDB/R/"
-fn <- list.files(srcPath, pattern="R$")
+#' load the R4Couchdb package
+#' by means of the source command
+#'
+srcPath <- "../R4CouchDB/R/"
+fn <- list.files(srcPath, pattern="R$")
for (k in 1:length(fn)){
source(paste(srcPath, fn[k], sep=""))
}
+
#' but we need the RCurl & RJSONIO lib
library(RCurl)
library(RJSONIO)
+#' At first one have to generate the ini list.
+#' This list (here ccc) contains everything
+#' you need for the connection.
ccc <- cdbIni()
-
-#' here one can do some auth settungs
+#' one can do some auth settings
#' ccc$uname <- "user.name"
#' ccc$pwd <- "pass.word"
-
+#'
#' database list please
ccc <- cdbListDB(ccc)
ccc$res
@@ -29,54 +32,61 @@ ccc$queryParam <- "count=10"
cdbGetUuidS(ccc)$res
#' reset queryParam e.g. for cdbGetView()
ccc$queryParam <- ""
-
+#'
#' writes the id to ccc$id
ccc <- cdbGetUuid(ccc)
ccc$id
+#'
#' the paste() with id makes sure that the db don't exist
-ccc$newDBName <- paste("r4couchdb_", ccc$id, sep="")
+#'
+ccc$newDBName <- paste("r4couchdb_",
+ ccc$id,
+ sep="")
ccc <- cdbMakeDB(ccc)
ccc$res
+#'
#' generate some data and add to dataList
ccc$dataList <- list(normalDistRand = rnorm(20))
ccc <- cdbAddDoc(ccc)
-
+#'
#' since v0.6 her ccc$dataList
#' contains the _id and _rev key
#' everything ok?
cdbGetDoc(ccc)$res
+#'
#' try cdbUpdateDoc(ccc)
ccc$dataList$Date <- date()
ccc <- cdbUpdateDoc(ccc)
-
+#'
#' make a png (stolen from ?persp)
x <- seq(-10, 10, length= 30)
y <- x
f <- function(x,y) { r <- sqrt(x^2+y^2); 10 * sin(r)/r }
z <- outer(x, y, f)
-
+#'
z[is.na(z)] <- 1
op <- par(bg = "white")
-ccc$fileName <- paste("../",ccc$DBName,".test.png", sep="")
-ccc$dataList <- list(data=list(x=x,y=y,z=z),
- date=ccc$date)
+ccc$fileName <- paste("../",
+ ccc$DBName,
+ ".test.png",
+ sep="")
-png(filename=ccc$fileName)
+#' make an png
+png(filename = ccc$fileName)
persp(x, y, z,
theta = 30,
phi = 30,
expand = 0.5,
col = "lightblue")
dev.off()
-
#' add the plot as attachment to the db
ccc <- cdbAddAttachment(ccc)
+#' remove the db when ready
if(FALSE){
- remove the db when ready
- ccc$removeDBName <- ccc$newDBName
- ccc <- cdbRemoveDB(ccc)
+ ccc$removeDBName <- ccc$DBName
+ cdbRemoveDB(ccc)$res
}
View
51 example/twitter-db.R
@@ -0,0 +1,51 @@
+#'
+#' R4CouchDB twitter example
+#'
+#' @author wactbprot
+#'
+#' example queries some search
+#' results from the twitter api
+#' and store them in a database
+#'
+library(RCurl)
+library(RJSONIO)
+
+srcPath <- "../R4CouchDB/R/"
+fn <- list.files(srcPath, pattern="R$")
+for (k in 1:length(fn)){
+ source(paste(srcPath, fn[k], sep=""))
+}
+
+#' search for:
+q <- "japan"
+#' generate connection list
+foo <- cdbIni()
+#' paste a db name
+foo$newDBName <- paste("twitter_",
+ q,
+ "_db",
+ sep="")
+#' build the db and use it
+foo$DBName <- foo$newDBName
+foo <- cdbMakeDB(foo)
+
+#' get 100 search results (per page, rpp)
+#' in json format
+u <- paste("http://search.twitter.com/search.json?rpp=100&q=",
+ q,
+ sep="")
+#' convert the results to a list
+res <- foo$fromJSON(getURLContent(u))
+#' loop over resultset and push it into
+#' the db
+for(i in 1:length(res$results)){
+ foo$dataList <- res$results[[i]]
+ print( cdbAddDoc(foo)$res$ok )
+}
+
+## lapply(res$results,
+## function(i){
+## foo$dataList <- i
+## cdbAddDoc(foo)$res
+## })
+
Please sign in to comment.
Something went wrong with that request. Please try again.