Re: [R] help with sockets in R
Hi, Thanks for the advice! My locale and encoding setting follow: > Sys.getlocale() [1] "en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8" > getOption("encoding") [1] "native.enc" I was indeed able to solve my immediate problem by using readLines to read the whole response and parse it later, following the example in an old version of OmegaHat's SSOAP package. The httpRequest package also reads the entire response as a unit, but does so using readBin and rawToChar in one case and read.socket in another. OmegaHat's RCurl library deserves further investigation and might be the smartest way to go. Thanks also for the pointer to tests/internet.R, which uses read.socket. The R Data Import/Export guide states that socketConnection is preferred over the make.socket/read.socket methods. But with several different options, it's hard to figure out which way to go. My understanding of HTTP is very limited, but might reading the response 'til the server closes the socket run afoul of some of the more advanced uses of HTTP? Thanks, -chris On Tue, Sep 21, 2010 at 11:43 PM, Prof Brian Ripley wrote: > readLines() is for a text-mode connection; readChar() is for a binary-mode > connection. Given that you asked for possible re-encoding by the 'encoding' > argument, you cannot safely mix them (text-mode access is re-encoded, > binary-mode is not). However, we don't know if re-encoding was active in > your case since we don't know your locale. > > Either don't specify an encoding and re-encode the response in R or use > readLines() to read the complete response and split it up later. > > For a different approach with read/write.socket() see tests/internet.R in > the R sources. > > Please do note that the posting guide asked you for 'at a minimum' > information (which includes the locale) and a reproducible example. > > On Tue, 21 Sep 2010, Christopher Bare wrote: > >> Hi R gurus, >> >> I'm trying to use a ReSTful web service from within R. Specifically, I >> need to make HTTP PUT requests. >> >> I'm able to make the request and that goes well enough, but I'm having >> trouble properly consuming the HTTP response. The problem comes in >> when I'm trying to parse out the response body. I get the length of >> the response body from the Content-Length header. I then try to use >> readChar(con, nchars=content.length). >> >> The result I'm seeing is that the first few characters of the response >> body are cut off. >> >> My code looks like this: >> >> >> http.request <- function(host, path, request, port=80) { >> >> con <- socketConnection(host=host, port=port, open="w+", >> blocking=TRUE, encoding="UTF-8") >> writeLines(request, con) >> >> write("wrote request", stderr()) >> flush(stderr()) >> >> # build response object >> response <- list() >> response$status <- readLines(con, n=1) >> response$headers <- character(0) >> repeat{ >> ss <- readLines(con, n=1) >> write(ss, stderr()) >> flush(stderr()) >> if (ss == "") break >> key.value <- strsplit(ss, ":\\s*") >> response$headers[key.value[[1]][1]] <- key.value[[1]][2] >> } >> >> if (any(names(response$headers)=='Content-Length')) { >> content.length <- >> as.integer(response$headers['Content-Length']) >> response$body <- readChar(con, nchars=content.length) >> } >> close(con) >> } >> >> >> response$body ends up with >> >> "e,\"id\":\"some_doc\",\"rev\":\"7-906e06a7744780ef93126adc6f8f10ef\"}\n" >> >> when it should be: >> >> >> "{\"ok\":true,\"id\":\"some_doc\",\"rev\":\"7-906e06a7744780ef93126adc6f8f10ef\"}" >> >> >> Is mixing readLines and readChars on the same connection causing bad >> juju? If it is, what's the recommended what to do such a thing? >> >> >> Thanks for any help!! >> >> -Chris >> >> __ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > -- > Brian D. Ripley, rip...@stats.ox.ac.uk > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > University of Oxford, Tel: +44 1865 272861 (self) > 1 South Parks Road, +44 1865 272866 (PA) > Oxford OX1 3TG, UK Fax: +44 1865 272595 > __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] help with sockets in R
Hi R gurus, I'm trying to use a ReSTful web service from within R. Specifically, I need to make HTTP PUT requests. I'm able to make the request and that goes well enough, but I'm having trouble properly consuming the HTTP response. The problem comes in when I'm trying to parse out the response body. I get the length of the response body from the Content-Length header. I then try to use readChar(con, nchars=content.length). The result I'm seeing is that the first few characters of the response body are cut off. My code looks like this: http.request <- function(host, path, request, port=80) { con <- socketConnection(host=host, port=port, open="w+", blocking=TRUE, encoding="UTF-8") writeLines(request, con) write("wrote request", stderr()) flush(stderr()) # build response object response <- list() response$status <- readLines(con, n=1) response$headers <- character(0) repeat{ ss <- readLines(con, n=1) write(ss, stderr()) flush(stderr()) if (ss == "") break key.value <- strsplit(ss, ":\\s*") response$headers[key.value[[1]][1]] <- key.value[[1]][2] } if (any(names(response$headers)=='Content-Length')) { content.length <- as.integer(response$headers['Content-Length']) response$body <- readChar(con, nchars=content.length) } close(con) } response$body ends up with "e,\"id\":\"some_doc\",\"rev\":\"7-906e06a7744780ef93126adc6f8f10ef\"}\n" when it should be: "{\"ok\":true,\"id\":\"some_doc\",\"rev\":\"7-906e06a7744780ef93126adc6f8f10ef\"}" Is mixing readLines and readChars on the same connection causing bad juju? If it is, what's the recommended what to do such a thing? Thanks for any help!! -Chris __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error in untar2(tarfile, files, list , exdir) : unsupported entry type ‘x’
For posterity, this thread was continued on the R-SIG-Mac mailing list under the same subject. Prof. Brian Ripley was kind enough to point out an easy work around: R CMD INSTALL now uses the internal untar() in package utils: this ensures that all platforms can install bzip2- and xz-compressed tarballs. In case this causes problems (as it has on some Windows file systems when run from Cygwin tools) it can be overridden by the environment variable R_INSTALL_TAR: setting this to a modern external tar program will speed up unpacking of large (tens of Mb or more) tarballs. I suspect that the origin of the non-standard tar block types was that user accounts and groups on my Mac are managed by Active Directory. The same commands work on another Snow Leopard Mac with locally managed accounts. -chris __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error in untar2(tarfile, files, list , exdir) : unsupported entry type ‘x’
Hi, I'm starting to think this is a Mac OS X Snow Leopard specific issue. The GNU docs on the tar format say the type flag 'x' means this: #define XHDTYPE 'x'/* Extended header referring to the next file in the archive */ Maybe, OSX is putting some bad juju in my files causing them to have these extended headers?? Aaarg! -chris __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Error in untar2(tarfile, files, list , exdir) : unsupported entry type ‘x’
Hi R gurus, I'm getting the following error when trying to build and install an R package: Error in untar2(tarfile, files, list, exdir) : unsupported entry type ‘x’ I build the package like so: R --no-init-file CMD build mypackage Then try to install it: sudo R --no-init-file CMD INSTALL mypackage.tar.gz ...which dies with the above error. I can extract the archive fine with tar -zxf, so I don't think the file has problems. R version 2.11.0 (2010-04-22) OSX 10.6.3 Any hints would be greatly appreciated! Thanks, -- chris __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] RSQLite column names underscores
Hi, When I use RSQLite's dbWriteTable(...) function, the columns in the db table frequently end up having "__1" (two underscores and a one) added to them. The names are unique within the table and contain no weird characters. For example a "position" column from a data.frame was written to the DB as "position__1". Does anyone understand how this name munging happens and how I can prevent or work around it? Having these suffixes added seemingly arbitrarily makes it hard to construct the next query. Thanks for any hints, -chris __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Ruuid missing Gtk glib.dylib
Hi, I get an error indicating a missing library from the package 'Ruuid'. I suppose this means I should install RGtk. I just thought I'd document the error. Maybe a dependency entry is missing? R 2.9.0 OS X 10.5.8 Thanks, - chris > biocLite('Ruuid') Using R version 2.9.0, biocinstall version 2.4.12. Installing Bioconductor version 2.4 packages: [1] "Ruuid" Please wait... Warning: unable to access index for repository http://brainarray.mbni.med.umich.edu/bioc/bin/macosx/universal/contrib/2.9 trying URL 'http://bioconductor.org/packages/2.4/bioc/bin/macosx/universal/contrib/2.9/Ruuid_1.22.0.tgz' Content type 'application/x-gzip' length 66343 bytes (64 Kb) opened URL == downloaded 64 Kb The downloaded packages are in /var/folders/n3/n3Xa3tJRED4hkXZd5y3QWk+++TI/-Tmp-//RtmpkHt9YC/downloaded_packages > library(Ruuid) Error in dyn.load(file, DLLpath = DLLpath, ...) : unable to load shared library '/Library/Frameworks/R.framework/Resources/library/Ruuid/libs/i386/Ruuid.so': dlopen(/Library/Frameworks/R.framework/Resources/library/Ruuid/libs/i386/Ruuid.so, 6): Library not loaded: /Library/Frameworks/GTK+.framework/Versions/2.14.X11/Resources/lib/libglib-2.0.0.dylib Referenced from: /Library/Frameworks/R.framework/Resources/library/Ruuid/libs/i386/Ruuid.so Reason: image not found Error: package/namespace load failed for 'Ruuid' Error in args(getuuid) : no function to return from, jumping to top level J. Christopher Bare Institute for Systems Biology __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Split rownames into factors
Hi, I'm not an R expert, but I thought I'd give your question a shot anyway. First, it looks like you're starting with a matrix, rather than a list. Let's hope I guessed that right: > m = matrix(c(324, 65, 543, 23, 54, 8765, 213, 43, 65)) > rownames(m) = c('X1Jan08', 'X1Jun08', 'X1Dec08', 'X2Jan08', 'X2Jun08', > 'X2Dec08', 'X3Jan08', 'X3Jun08', 'X3Dec08') > m [,1] X1Jan08 324 X1Jun08 65 X1Dec08 543 X2Jan08 23 X2Jun08 54 X2Dec08 8765 X3Jan08 213 X3Jun08 43 X3Dec08 65 You can pull the individual values out of the compound thing in row names using regular expressions like so: > gsub('([A-Z]\\d+)([A-Za-z]+)(\\d+)', '\\2 \\3', rownames(m), perl=T) With that, we can make a data.frame: > df = data.frame(Value=m[,1], Date=gsub('([A-Z]\\d+)([A-Za-z]+)(\\d+)', '\\2 > \\3', rownames(m), perl=T), Group=gsub('([A-Z]\\d+)([A-Za-z]+)(\\d+)', '\\1', > rownames(m), perl=T)) The old compound row names hold over from the matrix, but we can cure that easily enough: > rownames(df) = NULL > df Value Date Group 1 324 Jan 08X1 265 Jun 08X1 3 543 Dec 08X1 423 Jan 08X2 554 Jun 08X2 6 8765 Dec 08X2 7 213 Jan 08X3 843 Jun 08X3 965 Dec 08X3 Both Date and Group will be coerced to factors, which is probably what you want with Group and maybe not with Data. If I'm wrong and you really have a list, it's not that different. First, get a vector of values: > data.list = list(X1Jan08=324, X1Jun08=65, X1Dec08=543, X2Jan08=23, > X2Jun08=54, X2Dec08=8765, X3Jan08=213, X3Jun08=43, X3Dec08=65) > values = as.vector(data.list, mode="integer") The rest is very similar to what's above. I hope this helps, -chris On Mon, Jul 27, 2009 at 3:10 PM, jimdare wrote: > > Hi Guys, > > I was wondering how you would go about solving the following problem: > > I have a list where the grouping information is in the row names. > > Rowname [,1] > > X1Jan08 324 > X1Jun08 65 > X1Dec08 543 > X2Jan08 23 > X2Jun08 54 > X2Dec08 8765 > X3Jan08 213 > X3Jun08 43 > X3Dec08 65 > > How can I create the following dataframe: > Value Date Group > [1,] 324 Jan 08 X1 > [2,] 65 Jun 08 X1 > [3,] 543 Dec 08 X1 > etc. > > Thanks for your help! > James > > -- > View this message in context: > http://www.nabble.com/Split-rownames-into-factors-tp24689181p24689181.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Specifying package dependencies
Hi, I'm trying to put together an R package. My library has dependencies on three other libraries: RSQLite, gaggle (a component of Bioconductor), and another package that is available as a download from the author's website. Is it possible to specify each of those dependencies in such a way that they will be automatically installed when a user installs my library? I'd like to save the end user from a lengthy series of install steps, if possible. I've looked in the Writing R Extensions document, but am probably failing to understand most of it. If I put a bioconductor package in the Depends line of the DESCRIPTION file, should I expect that to just work? It seems like it doesn't. For the case where I need to download a .tgz file from the author's page, is there a way to fetch and install that when my library is being installed? Maybe in a configure script? Is this a practical thing to try and do? Thanks for any advise! -Chris __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.