Skip to content
This repository has been archived by the owner on Jun 7, 2023. It is now read-only.

Strings returned as factors #21

Open
ax42 opened this issue Oct 21, 2015 · 5 comments
Open

Strings returned as factors #21

ax42 opened this issue Oct 21, 2015 · 5 comments

Comments

@ax42
Copy link
Contributor

ax42 commented Oct 21, 2015

> columnTest <- rforcecom.query(session, "select Id, Name, Amount, CloseDate, IsWon from Opportunity limit 1")
> sapply(columnTest, class)
       Id      Name    Amount CloseDate     IsWon 
 "factor"  "factor" "numeric"  "factor"  "factor" 

I would expect:

> sapply(columnTest, class)
       Id             Name   Amount     CloseDate   IsWon 
 "character"  "character" "numeric"  "character"  "factor" 

And absolute bonus marks for actually getting CloseDate as a date.

Given how nasty "stringsAsFactors = TRUE" is in read.csv, I would suggest at least mimicking the behaviour of "stringsAsFactors = FALSE". Unfortunately, it will probably need a defaulted parameter, defaulted to the current way (else it will break old code).

@StevenMMortimer
Copy link
Contributor

The issue is using the function type.convert() https://github.com/hiratake55/RForcecom/blob/master/R/rforcecom.query.R#L59. In the documentation for that function it says:

Given a character vector, it attempts to convert it to logical, integer, numeric or complex, and failing that converts it to factor unless as.is = TRUE. The first type that can accept all the non-missing values is chosen

There are a few options, I guess:

  1. Set the as.is argument of type.convert() equal to !default.stringsAsFactors() and then it's controlled globally
  2. Add a new argument to rforcecom.query that would allow user to specify function behavior
  3. Pull down Salesforce metadata on the fields and then format each column according to its Salesforce type (this would require a lot more overhead in the function but ensures consistency betwen the two systems)
  4. Use something other than type.convert

@Breza
Copy link

Breza commented Jul 21, 2017

This isn't a solution, but my workaround is to pipe the output of rforcecom.query into mutate_if(is.factor, as.character), which turns all of the factors into characters.

@StevenMMortimer
Copy link
Contributor

@Breza This may or may not be fixed depending on your version of RForcecom. Looking at current GitHub source code, it automatically returns strings as characters: https://github.com/hiratake55/RForcecom/blob/master/R/rforcecom.utils.R#L54

@rrmn
Copy link

rrmn commented May 13, 2019

@StevenMMortimer This issue is not solved in the current CRAN and Github versions of RForcecom.

I can confirm that rforcecom.query() indeed returns factor variables instead of characters if options(stringsAsFactors = T) — which it is in a default R environment.

Add a new argument to rforcecom.query that would allow user to specify function behavior

This would be a fantastic solution imho that does not require mucking around with global settings.

@StevenMMortimer
Copy link
Contributor

StevenMMortimer commented May 15, 2019

@RomanAbashin I have created my own fork of the package and the functionality you mention is available there: https://github.com/StevenMMortimer/salesforcer

More specifically, there is an argument in the sf_query() function called guess_types that is a logical allowing you to keep all of the columns as character datatypes or use the col_guess() magic of the readr package. That functionality is not yet on CRAN, but will be soon.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants