How to use botornot function in R tweetbotornot package? - r

I am unable to even run the example code given on the botrnot documentation. Unsure what's happening.
# libraries
library(rtweet)
library(tweetbotornot)
# authentication for twitter API
auth <- rtweet_app()
auth_setup_default()
users <- c("kearneymw", "geoffjentry", "p_barbera",
"tidyversetweets", "rstatsbot1234", "RStatsStExBot")
## get most recent 10 tweets from each user
tmls <- get_timeline(users, n = 10)
## pass the returned data to botornot()
data <- botornot(tmls)
Expecting data frame titled data should be created and have an additional column that is the probability of the user being a bot. Instead I have this error.
?Error in botornot.data.frame(tmls) : "user_id" %in% names(x) is not TRUE
The table in the bottom of the documentation is what I'm hoping to achieve.
https://www.rdocumentation.org/packages/botrnot/versions/0.0.2

Related

I'm trying to take the full list of followers on Twitter of each US Senator starting from a database and using rtweet

Starting from a data frame containing the full list of usernames of the US Senators, I'm trying to take the full list of their followers using rtweet 0.7.0 version and the function get_followers with the retryonratelimit option set TRUE. If I use the column of the data frame R gives me an error:
Error in get_followers_(user = c("LisaMurkowski", "SenDanSullivan", "SenTuberville", :
isTRUE(length(user) == 1) it's not TRUE
While if I just use one user name with the same option, it gives me just 5000 users and not the full list.
Using the last version of rtweet gives me the same problem in both cases.
library(rtweet)
data <- read.csv("data.csv", sep=";")
follower <- get_followers(data$twitter, retryonratelimit = T)
library(rtweet)
data <- read.csv("data.csv", sep=";")
follower <- get_followers("LisaMurkowski", retryonratelimit = T)
I tried those two chunks with both versions of rtweet but I failed to find a solution.
The function lookup_users says that there are 57271024 accounts (the sum of the number of followers of each US Senator). I would like to have this full list to build a network.

Using R and an API to extract multiple stock market data

I have setup an API access key with a data provider of stock market data. With this key i am able to extract stock market data based on ticker code (E.g. APPL: Apple, FB: Facebook etc).
I am able to extract stock data on an individual ticker basis using R but I want to write a piece of code that extracts data based on the multiple stock tickers and puts them all in one data frame (the structure is the same for all stocks). I m not sure how to create a loop that updates the data frame each time stock data is extracted. I get an error called 'No encoding supplied: defaulting to UTF-8' which does not tell me much. A point in the right direction would be helpful.
I have the following code:
if (!require("httr")) {
install.packages("httr")
library(httr)
}
if (!require("jsonlite")) {
install.packages("jsonlite")
library(jsonlite)
}
stocks <- c("FB","APPL") #Example stocks actual stocks removed
len <- length(stocks)
url <- "URL" #Actual url removed
access_key <- "MY ACCESS KEY" #Actual access key removed
extraction <- lapply(stocks[1:len],function(i){
call1 <- paste(url,"?access_key=",access_key,"&","symbols","=",stocks[i],sep="")
get_prices <- GET(call1)
get_prices_text <- content(get_prices, "text")
get_prices_json <- fromJSON(get_prices_text, flatten = TRUE)
get_prices_df <- as.data.frame(get_prices_json)
return(get_prices_df)
}
)
file <- do.call(rbind,extraction)
I realised that this is not the most efficient way of doing this. A better way is to update the url to include multiple stocks rather then using a lapply function. I am therefore closing the question.

R: Extracting json data from an OData API gives inconsistent results?

I'm attempting to extract data from the Wales government statistics services OData API. Details on the API can be found here, including an example of how to filter the data.
However I seem to be getting a non-deterministic subset of the data i.e. each attempt results in a different number of records returned.
Below is a simple reproducible example.
Additionally I have also tried:
using RJSONIO::fromJSON(), which has the same result.
attempting to use the odata.nextLink url, if it is returned in the json object, to keep extracting more data. Again, each attempt results in a different sized object.
Any insight will be much appreciated.
## preliminaries
library(jsonlite)
# prepare filters
filter1 <- "Column_ItemName_ENG"
filter1.value <- "Gross%20expenditure"
filter2 <- "Row_ItemName_ENG"
filter2.value <- "Parking%20of%20vehicles"
query <- paste0("http://open.statswales.gov.wales/en-gb/dataset/lgfs0009?$filter=",
filter1, "%20eq%20%27", filter1.value, "%27%20and%20",
filter2, "%20eq%20%27", filter2.value, "%27")
# test 1
test1 <- jsonlite::fromJSON(query)
test1 <- test1[[2]]
# test 2
test2 <- jsonlite::fromJSON(query)
test2 <- test2[[2]]
# test 3
test3 <- jsonlite::fromJSON(query)
test3 <- test3[[2]]
# compare results
nrow(test1)
nrow(test2)
nrow(test3)
PS: this question is cross-posted from RStudio Community, and I promise to update either post with relevant solutions found on the other one.

How to get company description, statistics using R from eg. Yahoo Finance?

I am looking for ways to get company description, key statistics, chairman name from Yahoo Finance (or other financial website) using R, for example package quantmod.
There is oodles of info how to get current and historical prices etc, but this is not what I want.
best,
This R package does not support queries for Asian bourses. The problem appears to be with the underlying Yahoo APIs.
You can get that using Intrinio's API. Their data tag directory allows you to look up the tags you want, in your case, "long_description" and "ceo" will get you the data you want:
#Install httr, which you need to request data via API
install.packages("httr")
require("httr")
#Create variables for your usename and password, get those at intrinio.com/login
username <- "Your_API_Username"
password <- "Your_API_Password"
#Making an api call for roic. This puts together the different parts of the API call
base <- "https://api.intrinio.com/"
endpoint <- "data_point"
stock <- "T"
item1 <- "long_description"
item2 <- "ceo"
#Pasting them together to make the API call
call1 <- paste(base,endpoint,"?","ticker","=", stock, "&","item","=",item1, sep="")
call2 <- paste(base,endpoint,"?","ticker","=", stock, "&","item","=",item2, sep="")
#Now we use the API call to request the data from Intrinio's database
ATT_description <- GET(call1, authenticate(username,password, type = "basic"))
ATT_CEO <- GET(call2, authenticate(username,password, type = "basic"))
#That gives us the ROIC value, but it isn't in a good format so we parse it
test1 <- unlist(content(ATT_description,"parsed"))
test2 <- unlist(content(ATT_CEO,"parsed"))
#Then make your data frame:
df1 <- data.frame(test1)
df2 <- data.frame(test2)
#From here you can rbind or cbind, and create loops to get the same data for many tickers
You can get your API keys here. Full API documentation here. This is what the CEO dataframe looks like:

Programmatically download ROIC and other financial data in R

Is there some place I can programmatically download the ROIC and other data typically reported in a company's quarter report?
I know that I can access the daily price data of a stock from http://chart.yahoo.com/table.csv, but I can't find anything about the financial performance.
Thanks!
Intrinio provides that data in R using the httr package. You can follow the instructions here, I'll modify them here to get ROIC:
#Install httr, which you need to request data via API
install.packages("httr")
require("httr")
#Create variables for your usename and password, get those at intrinio.com/login
username <- "Your_API_Username"
password <- "Your_API_Password"
#Making an api call for roic. This puts together the different parts of the API call
base <- "https://api.intrinio.com/"
endpoint <- "data_point"
stock <- "T"
item1 <- "roic"
call1 <- paste(base,endpoint,"?","ticker","=", stock, "&","item","=",item1, sep="")
#Now we use the API call to request the data from Intrinio's database
ATT_roic <- GET(call1, authenticate(username,password, type = "basic"))
#That gives us the ROIC value, but it isn't in a good format so we parse it
test1 <- unlist(content(ATT_roic,"parsed"))
df <- data.frame(test1)
You can modify that code for any US ticker, and you can change roic out for hundreds of other financial metrics. If you want to pull historical roic, or roic for a specific date range, see the instructions I posted at the start of this answer.

Resources