<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>R | AKitsche Blog</title><link>https://akitsche.netlify.app/tag/r/</link><atom:link href="https://akitsche.netlify.app/tag/r/index.xml" rel="self" type="application/rss+xml"/><description>R</description><generator>Wowchemy (https://wowchemy.com)</generator><language>en-us</language><copyright>© 2021 Andreas Kitsche</copyright><lastBuildDate>Thu, 23 Jul 2015 21:13:14 -0500</lastBuildDate><image><url>https://akitsche.netlify.app/images/icon_hufdd866d90d76849587aac6fbf27da1ac_464_512x512_fill_lanczos_center_2.png</url><title>R</title><link>https://akitsche.netlify.app/tag/r/</link></image><item><title>Connecting R with Salesforce.com</title><link>https://akitsche.netlify.app/post/r_salesforce/r_salesforce/</link><pubDate>Thu, 23 Jul 2015 21:13:14 -0500</pubDate><guid>https://akitsche.netlify.app/post/r_salesforce/r_salesforce/</guid><description>
&lt;script src="https://akitsche.netlify.app/rmarkdown-libs/header-attrs/header-attrs.js">&lt;/script>
&lt;p>Hey folks,&lt;/p>
&lt;p>this blog post will give a short introduction on how to connect &lt;a href="https://www.salesforce.com">salesforce.com&lt;/a> with &lt;a href="https://cran.r-project.org/">R&lt;/a>.
According to &lt;a href="http://de.gravatar.com/hiratake55">Takekatsu Hiramura&lt;/a>&lt;/p>
&lt;blockquote>
&lt;p>R statistical computing environment is the most populous statistical computing software, and Salesforce.com is the world´s most innovative cloud-computing based SaaS (Software-as-a-Service) CRM package.&lt;/p>
&lt;/blockquote>
&lt;p>&lt;img src="https://akitsche.netlify.app/img/R_salesforce_blog_picture.png" />&lt;/p>
&lt;p>To connect R with salesforce.com Takekatsu Hiramura developed the R add-on package &lt;a href="https://cran.r-project.org/web/packages/RForcecom/">RForcecom&lt;/a>. The source code of the package is available from &lt;a href="https://github.com/hiratake55/RForcecom">github&lt;/a>. You can install the package from &lt;a href="http://cran.r-project.org/">CRAN&lt;/a>:&lt;/p>
&lt;pre class="r">&lt;code>install.packages(&amp;quot;RForcecom&amp;quot;)
library(RForcecom)&lt;/code>&lt;/pre>
&lt;p>To sign in to salesforce.com we can use the &lt;code>rforcecom.login()&lt;/code> function. We have to set the &lt;code>username&lt;/code>, &lt;code>password&lt;/code>, &lt;code>instanceURL&lt;/code>, &lt;code>apiVersion&lt;/code> arguments as follows:&lt;/p>
&lt;pre class="r">&lt;code>username &amp;lt;- &amp;quot;yourname@yourcompany.com&amp;quot;
password &amp;lt;- &amp;quot;YourPasswordSECURITY_TOKEN&amp;quot;
instanceURL &amp;lt;- &amp;quot;https://login.salesforce.com/&amp;quot;
apiVersion &amp;lt;- &amp;quot;26.0&amp;quot;
(session &amp;lt;- rforcecom.login(username, password, instanceURL, apiVersion))&lt;/code>&lt;/pre>
&lt;p>Please note: do not forget your security token in the password field. The &lt;a href="https://success.salesforce.com/answers?id=90630000000glADAAY">Salesforce community&lt;/a> shows how to reset the security token.&lt;/p>
&lt;p>Unfortunatly we probably get an error: &lt;em>Error in curl::curl_fetch_memory(url, handle = handle) : Couldn’t resolve host name&lt;/em>, because the corporate network environment uses HTTP Proxy. To pick out the proxy setting we can use the &lt;code>ie_proxy_info()&lt;/code> function from the &lt;a href="https://cran.r-project.org/web/packages/curl/index.html">curl&lt;/a> R add-on package.:&lt;/p>
&lt;pre class="r">&lt;code>#install.packages(&amp;quot;curl&amp;quot;)
library(curl)
ie_proxy_info()&lt;/code>&lt;/pre>
&lt;p>Obviously the corporate network environment uses a global &lt;a href="https://en.wikipedia.org/wiki/Proxy_auto-config">proxy auto-configuration (PUC)&lt;/a> file that defines how web browsers and other user agents can automatically choose the appropriate proxy server. We can get the info of the proxy for a specific URL using the &lt;code>ie_get_proxy_for_url()&lt;/code> function:&lt;/p>
&lt;pre class="r">&lt;code>ie_get_proxy_for_url(target_url = &amp;quot;https://login.salesforce.com/&amp;quot;)&lt;/code>&lt;/pre>
&lt;p>The function returns a character string with two proxies and their corresponding port. We will further use the first proxy displayed: **. Now we have to set the proxy configurations with the &lt;code>set_config()&lt;/code> and &lt;code>use_proxy()&lt;/code> functions from the &lt;a href="https://cran.r-project.org/web/packages/httr/index.html">httr&lt;/a> R add-on package:&lt;/p>
&lt;pre class="r">&lt;code>#install.packages(&amp;quot;httr&amp;quot;)
library(httr)
proxy_for_url &amp;lt;- &amp;quot;your proxy&amp;quot; #from ie_get_proxy_for_url(target_url = &amp;quot;https://login.salesforce.com/&amp;quot;)
port_for_url &amp;lt;- 8080
set_config(
use_proxy(url = proxy_for_url&amp;quot;,
port = port_for_url,
username = &amp;quot;windows_username&amp;quot;,
password = &amp;quot;windows_password&amp;quot;,
auth = &amp;quot;ntlm&amp;quot;)
)&lt;/code>&lt;/pre>
&lt;p>Under some circumstances you will get the following error message at this point: &lt;em>Error in curl::curl_fetch_memory(url, handle = handle) : Problem with the SSL CA cert (path? access rights?)&lt;/em>.
In this case your security certificate, e.g. &lt;em>curl-ca-bundle.crt&lt;/em> is located in the wrong folder. You have to paste the file into &lt;em>C:/mingw64/ssl/certs&lt;/em>.&lt;/p>
&lt;p>Et voilà, we reached our goal to connect R with salesforce.com:&lt;/p>
&lt;pre class="r">&lt;code>session &amp;lt;- rforcecom.login(username = username, password = password, loginURL = instanceURL, apiVersion = apiVersion)
session&lt;/code>&lt;/pre>
&lt;p>Use the function &lt;code>rforcecom.retrieve()&lt;/code> to retrieve a dataset:&lt;/p>
&lt;pre class="r">&lt;code>rforcecom.retrieve(session = session,
objectName = &amp;quot;Contact&amp;quot;,
fields = c(&amp;quot;ID&amp;quot;, &amp;quot;Name&amp;quot;),
limit = 50)&lt;/code>&lt;/pre>
&lt;p>After setting up our environment these procedures are very easy and are very useful for projects using R and Salesforce.com. Please let me know if you have any further questions or suggestions.&lt;/p></description></item></channel></rss>