🎉Community Raffle - Win $25

An exclusive raffle opportunity for active members like you! Complete your profile, answer questions and get your first accepted badge to enter the raffle.
Join and Win

"Problem with R-script results"

User: "ighyboo"
New Altair Community Member
Updated by Jocelyn
Hi everyone,
I used the R extension before and always managed to get the data out using "outdata <-as.data.frame(Results)"

I'm currently working on a script that download twitter followers using the twitter API, everything work fine and I managed to create the results data-frame but Rapidminer doesn't like it and spit out this error:

"In order to import an R Data Frame as example set the data frame must provide attribute names"

I'm confused because when I check my result variable with colnames(outdata), the column names are assigned as you can see here:

colnames(Results)
[1] "id_str"            "name"              "screen_name"     
[4] "url"              "profile_image_url" "description"     
[7] "location"          "followers_count"  "friends_count"   
[10] "statuses_count"    "created_at" 



Anyone else had this problem before?

Here's the process and the script:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<process version="5.3.015">
  <context>
    <input/>
    <output/>
    <macros/>
  </context>
  <operator activated="true" class="process" compatibility="5.3.015" expanded="true" name="Process">
    <process expanded="true">
      <operator activated="true" class="generate_data_user_specification" compatibility="5.3.015" expanded="true" height="60" name="Generate Data by User Specification" width="90" x="45" y="30">
        <list key="attribute_values">
          <parameter key="target" value="&quot;WRADARltd&quot;"/>
        </list>
        <list key="set_additional_roles"/>
      </operator>
      <operator activated="true" class="r:execute_script_r" compatibility="5.3.000" expanded="true" height="76" name="Execute Script (R)" width="90" x="179" y="30">
        <parameter key="script" value="#Install libraries&#10;install.packages(&quot;httr&quot;);&#10;install.packages(&quot;rjson&quot;);&#10;install.packages(&quot;data.table&quot;);&#10;library(httr);&#10;library(rjson);&#10;library(data.table);&#10;&#10;&#10;#Declare API keys and inputs&#10;consumer_key = &quot;UansBjFHOm8fpkB4jx5mSiDTu&quot;;&#10;consumer_secret = &quot;dKTbQ0QKCqHaejnLyHOW0PHuOzpn5PIhjVmOkGMzcyLuaFrA7p&quot;;&#10;target = indata$target;&#10;&#10;&#10;#Auth&#10;secret &lt;- RCurl::base64(paste(consumer_key, consumer_secret, sep = &quot;:&quot;));&#10;req &lt;- POST(&quot;https://api.twitter.com/oauth2/token&quot;,&#10;  config(httpheader = c(&#10;    &quot;Authorization&quot; = paste(&quot;Basic&quot;, secret),&#10;    &quot;Content-Type&quot; = &quot;application/x-www-form-urlencoded;charset=UTF-8&quot;&#10;  )),&#10;  body = &quot;grant_type=client_credentials&quot;,&#10;  encode = &quot;multipart&quot;&#10;);&#10;&#10;&#10;#Extract the access token&#10;token &lt;- paste(&quot;Bearer&quot;, content(req)$access_token)&#10;&#10;&#10;#Request Followers count&#10;url &lt;- paste(&quot;https://api.twitter.com/1.1/users/lookup.json?screen_name=&quot;,target,sep=&quot;&quot;);&#10;req &lt;- GET(url, config(httpheader = c(&quot;Authorization&quot; = token)));&#10;json &lt;- content(req, as = &quot;text&quot;);&#10;tmp &lt;- fromJSON(json);&#10;num_followers &lt;- tmp[[1]]$followers_count;&#10;&#10;&#10;#If more than 5000 followers split ID request otherwise retrieve all in one request&#10;if (num_followers&lt;5000) {&#10;&#10;&#9;#Request &lt;5000 IDs &#10;&#9;url &lt;- paste(&quot;https://api.twitter.com/1.1/followers/ids.json?screen_name=&quot;,target,sep=&quot;&quot;);&#10;&#9;req &lt;- GET(url, config(httpheader = c(&quot;Authorization&quot; = token)));&#10;&#9;json &lt;- content(req, as = &quot;text&quot;);&#10;&#9;IDs &lt;- fromJSON(json);&#10;&#9;IDlist &lt;- data.frame(IDs$ids);&#10;&#9;&#10;&#9;} else {&#10;&#9;&#10;&#9;#Request batches of IDs&#10;&#9;batches &lt;- ceiling(num_followers/5000);&#10;&#9;batch_size &lt;- ceiling(num_followers/batches);&#10;&#9;curs &lt;- &quot;-1&quot;;&#10;&#9;IDlist &lt;- data.frame();&#10;&#9;for (i in 1:batches) {&#10;&#9;&#9;url &lt;- paste(&quot;https://api.twitter.com/1.1/followers/ids.json?cursor=&quot;,curs,&quot;&amp;screen_name=&quot;,target,&quot;&amp;count=&quot;,batch_size,sep=&quot;&quot;);&#10;&#9;&#9;req &lt;- GET(url, config(httpheader = c(&quot;Authorization&quot; = token)));&#10;&#9;&#9;json &lt;- content(req, as = &quot;text&quot;);&#10;&#9;&#9;IDs &lt;- fromJSON(json);&#10;&#9;&#9;tmp &lt;- data.frame(IDs$ids);&#10;&#9;&#9;IDlist &lt;- rbind(IDlist, tmp);&#10;&#9;&#9;curs &lt;- IDs$next_cursor_str;&#10;&#9;&#9;Sys.sleep(60);&#10;&#9;&#9;};&#10;&#9;};&#10;&#10;&#10;#Loop through IDs in batches to retrieve full profiles&#10;batches &lt;- ceiling(num_followers/100);&#10;batch_size &lt;- ceiling(num_followers/batches);&#10;BatchIDcounter &lt;- 0;&#10;FollowersList &lt;- data.frame();&#10;for (i in 1:batches) {&#10;&#9;start &lt;- BatchIDcounter;&#10;&#9;end &lt;- BatchIDcounter+batch_size;&#10;&#9;if (end&gt;length(IDlist[,1])) {&#10;&#9;&#9;end &lt;-length(IDlist[,1]);&#10;&#9;&#9;batch_size &lt;- (end-start);&#10;&#9;};&#10;&#9;BatchIDs &lt;- paste(IDlist[start:end,1],collapse=&quot;,&quot;);&#10;&#9;url &lt;- paste(&quot;https://api.twitter.com/1.1/users/lookup.json?include_entities=false&amp;user_id=&quot;,BatchIDs,sep=&quot;&quot;);&#10;&#9;req &lt;- GET(url, config(httpheader = c(&quot;Authorization&quot; = token)));&#10;&#9;json &lt;- content(req, as = &quot;text&quot;);&#10;&#9;profiles &lt;- fromJSON(json);&#10;&#9;for (j in 1:batch_size) {&#10;&#9;&#9;tmp &lt;- data.table(id_str=&quot;&quot;,name=&quot;&quot;,screen_name=&quot;&quot;,url=&quot;&quot;,profile_image_url=&quot;&quot;,description=&quot;&quot;,location=&quot;&quot;,followers_count=&quot;&quot;,friends_count=&quot;&quot;,statuses_count=&quot;&quot;,created_at=&quot;&quot;);&#10;&#9;&#9;if (length(profiles[]$id_str)==0) {tmp$id_str=&quot;&quot;} else{tmp$id_str=head(profiles[]$id_str[1])};&#10;&#9;&#9;if (length(profiles[]$name)==0) {tmp$name=&quot;&quot;} else{tmp$name=head(profiles[]$name[1])};&#10;&#9;&#9;if (length(profiles[]$screen_name)==0) {tmp$screen_name=&quot;&quot;} else{tmp$screen_name=head(profiles[]$screen_name[1])};&#10;&#9;&#9;if (length(profiles[]$url)==0) {tmp$url=&quot;&quot;} else{tmp$url=head(profiles[]$url[1])};&#10;&#9;&#9;if (length(profiles[]$profile_image_url)==0) {tmp$profile_image_url=&quot;&quot;} else{tmp$profile_image_url=head(profiles[]$profile_image_url[1])};&#10;&#9;&#9;if (length(profiles[]$description)==0) {tmp$description=&quot;&quot;} else{tmp$description=head(profiles[]$description[1])};&#10;&#9;&#9;if (length(profiles[]$location)==0) {tmp$location=&quot;&quot;} else{tmp$location=head(profiles[]$location[1])};&#10;&#9;&#9;if (length(profiles[]$url)==0) {tmp$url=&quot;&quot;} else{tmp$url=head(profiles[]$url[1])};&#10;&#9;&#9;if (length(profiles[]$followers_count)==0) {tmp$followers_count=&quot;&quot;} else{tmp$followers_count=head(profiles[]$followers_count[1])};&#10;&#9;&#9;if (length(profiles[]$friends_count)==0) {tmp$friends_count=&quot;&quot;} else{tmp$friends_count=head(profiles[]$friends_count[1])};&#10;&#9;&#9;if (length(profiles[]$statuses_count)==0) {tmp$statuses_count=&quot;&quot;} else{tmp$statuses_count=head(profiles[]$statuses_count[1])};&#10;&#9;&#9;if (length(profiles[]$created_at)==0) {tmp$created_at=&quot;&quot;} else{tmp$created_at=head(profiles[]$created_at[1])};&#10;&#9;&#9;FollowersList &lt;- rbind(FollowersList, tmp);&#10;&#9;};&#10;&#9;BatchIDcounter &lt;- end;&#10;&#9;Sys.sleep(5);&#10;};&#10;&#10;&#10;&#10;#Output&#10;outdata &lt;- as.data.frame(FollowersList);&#10;"/>
        <enumeration key="inputs">
          <parameter key="name_of_variable" value="indata"/>
        </enumeration>
        <list key="results">
          <parameter key="outdata" value="Data Table"/>
        </list>
      </operator>
      <operator activated="true" breakpoints="before" class="write_excel" compatibility="5.3.015" expanded="true" height="76" name="Write Excel" width="90" x="447" y="30">
        <parameter key="excel_file" value="C:\Documents and Settings\menghii1\Desktop\first.xlsx"/>
        <parameter key="file_format" value="xlsx"/>
      </operator>
      <connect from_op="Generate Data by User Specification" from_port="output" to_op="Execute Script (R)" to_port="input 1"/>
      <connect from_op="Execute Script (R)" from_port="output 1" to_op="Write Excel" to_port="input"/>
      <connect from_op="Write Excel" from_port="through" to_port="result 1"/>
      <portSpacing port="source_input 1" spacing="0"/>
      <portSpacing port="sink_result 1" spacing="0"/>
      <portSpacing port="sink_result 2" spacing="0"/>
    </process>
  </operator>
</process>

Find more posts tagged with