tomoakin / RPostgreSQL

Automatically exported from code.google.com/p/rpostgresql
64 stars 20 forks source link

bugfix: extract the correct variable if oldenc is a data.frame #86

Closed ThierryO closed 6 years ago

ThierryO commented 7 years ago

When oldenc <- dbGetQuery(new.con, "SHOW client_encoding") returns a data.frame, sql5 <- paste("SET CLIENT_ENCODING TO '", oldenc, "'", sep="") returns the error below

Error in paste("SET CLIENT_ENCODING TO '", oldenc, "'", sep = "") : 
   non-string argument to internal 'paste'

This pull request converts oldenc to a character when it is a data.frame.

tomoakin commented 7 years ago

While oldenc should always be a data.frame, the condition to emit the error you mentioned is not clear.

> oldenc <- dbGetQuery(con, "SHOW client_encoding")
> oldenc
  client_encoding
1            UTF8
> paste("SET CLIENT_ENCODING TO '", oldenc, "'", sep="")
[1] "SET CLIENT_ENCODING TO 'UTF8'"
ThierryO commented 6 years ago

The problem is due to a function as.character.data.frame() is my own package

default R

> oldenc <- data.frame(client_encoding = "UTF-8", stringsAsFactors = FALSE)
> str(as.character(oldenc))
 chr "UTF-8"
> paste("SET CLIENT_ENCODING TO '", oldenc, "'", sep="")
[1] "SET CLIENT_ENCODING TO 'UTF-8'"

with my function

> as.character.data.frame <- function(x, ...){
+   factors <- sapply(x, is.factor)
+   for (i in which(factors)) {
+     x[, i] <- levels(x[, i])[x[, i]]
+   }
+   return(x)
+ }
> oldenc <- data.frame(client_encoding = "UTF-8", stringsAsFactors = FALSE)
> str(as.character(oldenc))
'data.frame':   1 obs. of  1 variable:
 $ client_encoding: chr "UTF-8"
> paste("SET CLIENT_ENCODING TO '", oldenc, "'", sep="")
Error in paste("SET CLIENT_ENCODING TO '", oldenc, "'", sep = "") : 
  non-string argument to internal 'paste'