Unable to start Selenium session in R - r

I've been trying to set up a Selenium session with Rwebdriver, but so far with no success. I believe I have initiated a Selenium server, but whenever I run
`> Rwebdriver::start_session(root = "http://localhost:4444/wd/hub/")`
I get the following error:
Error in serverDetails$value[[1]] : subscript out of bounds
After I set options(error = recover) to run a debug, I get the following result:
function (root = NULL, browser = "firefox", javascriptEnabled = TRUE,
takesScreenshot = TRUE, handlesAlerts = TRUE, databaseEnabled = TRUE,
cssSelectorsEnabled = TRUE)
{
server <- list(desiredCapabilities = list(browserName = browser,
javascriptEnabled = javascriptEnabled, takesScreenshot = takesScreenshot,
handlesAlerts = handlesAlerts, databaseEnabled = databaseEnabled,
cssSelectorsEnabled = cssSelectorsEnabled))
newSession <- getURL(paste0(root, "session"), customrequest = "POST",
httpheader = c(`Content-Type` = "application/json;charset=UTF-8"),
postfields = toJSON(server))
serverDetails <- fromJSON(rawToChar(getURLContent(paste0(root,
"sessions"), binary = TRUE)))
sessionList <- list(time = Sys.time(), sessionURL = paste0(root,
"session/", serverDetails$value[[1]]$id))
class(sessionList) <- "RSelenium"
print("Started new session. sessionList created.")
seleniumSession <<- sessionList
}
The issue is at paste0(root, "session/", serverDetails$value[[1]]$id)), and true enough, whenever I print serverDetails$value, all that appears is list(). Does anyone know how to fix the issue? Thank you for your attention and patience in advance.

Related

RevoScaleR Save reportProgress Output

I am using RevoScaleR rxDataStep function in RStudio to write data to a SQL table. The process works fine yet I am unable to save the output from the reportProgress argument when set to any value, 2 or 3. The results show up in the Console but I can't figure out where the objects exist so I can save them for future use. How can I save the progress outputs?
out <- rxDataStep(inData = data, outFile = obj, varsToKeep = NULL, varsToDrop = NULL,
rowSelection = NULL, transforms = NULL, transformObjects = NULL,
transformFunc = NULL, transformVars = NULL,
transformPackages = NULL, transformEnvir = NULL,
append = "rows", overwrite = TRUE, rowVarName = NULL,
removeMissingsOnRead = FALSE, removeMissings = FALSE,
computeLowHigh = TRUE, maxRowsByCols = 50000000,
rowsPerRead = -1, startRow = 1, numRows = -1,
returnTransformObjects = F,
blocksPerRead = 10000,
reportProgress = 2)
This is what shows up in console:
Rows Processed: 100Total Rows written: 100, Total time: 0.384
Maybe this is a more general R console question, how to save any message from the console which is not available otherwise

Extracting replies to a tweet using academictwitteR

Having trouble with grabbing tweets using conversation_id. I have academic license and get_bearer() is working fine.
I'm running:
devtools::install_github("cjbarrie/academictwitteR", build_vignettes = TRUE, force = T)
get_bearer()
myquery<-build_query(conversation_id = "1392547623073030144")
myquery
get_all_tweets(query = myquery,
start_tweets = "2020-03-13",
end_tweets = "2021-03-13",
n = Inf,
data_path = "tweet_data",
bind_tweets = FALSE)
and I am getting the error
Error in make_query(url = endpoint_url, params = params, bearer_token = bearer_token, :
something went wrong. Status code: 400
Any help would be greatly appreciated.

AzureCognitive R package translate service returns http 400: "body of request is not valid JSON."

I've tried to get the translate service to work using the AzureCognitive R package, but I get a HTTP 400 error and I can't seem to fix it.
# Initial setup
# I've already did the create login
# az <- AzureRMR::create_azure_login(tenant = "<tenant>")
az <- AzureRMR::get_azure_login()
sub <- az$get_subscription(id = "<id>")
rg <- sub$get_resource_group("<resource-group>")
# retrieve it
cogsvc <- rg$get_cognitive_service(name = "<name>")
# getting the endpoint from the resource object
endp <- cogsvc$get_endpoint()
############################
# This is where it fails
############################
call_cognitive_endpoint(endpoint = endp,
operation = "translate",
options = list('api-version' = "3.0",
'from' = 'en',
'to' = 'dk'),
body = "[{'Text':'Hello, what is your name?'}]",
http_verb = "POST")
call_cognitive_endpoint(endpoint = endp,
operation = "translate",
options = list('api-version' = "3.0",
'from' = 'en',
'to' = 'dk'),
body = list(text = "Hello, what is your name"),
http_verb = "POST")
Can someone see if this is a bug or me doing something wrong?
As per this example, you can pass the body as body=list(list(text = "Hello, what is your name"")),
call_cognitive_endpoint(endpoint = endp,
operation = "translate",
options = list('api-version' = "3.0",
'from' = 'en',
'to' = 'dk'),
body = list(list(text = "Hello, what is your name")),
http_verb = "POST")

How to use "tryCatch" to skip errors in a nested loop in R?

I am trying to load a few data with a nested-loop using pageviews. You already helped me get this result:
library("pageviews")
lang = c("it.wikipedia")
bm = c("ECB","Christine Lagarde")
x <- list(
list(),
list(),
list(),
list(),
list()
) # store results
for (i in seq_along(lang)) {
for (j in seq_along(bm)) {
x[[i]][[j]] = article_pageviews(project = lang[i], article = bm[j], platform = "all", user_type = "user", start = "2015100100", end = today(), reformat = TRUE, granularity = "daily")
}
}
The last step I need to do, however, involves reading some article for which project doesn't exist. Find an example below:
lang = c("it.wikipedia")
bm = c("Philip Lane")
x = article_pageviews(project = lang, article = bm, platform = "all", user_type = "user", start = "2015100100", end = today(), reformat = TRUE, granularity = "daily")
# Error in FUN(X[[i]], ...) :
# The date(s) you used are valid, but we either do not have data for those date(s), or the project you asked for is not loaded yet. Please check https://wikimedia.org/api/rest_v1/?doc for more information.
I would like to add this to the loop. I tried a few solutions but I don't manage to make the loop skip over if there is an error. I post below one mistaken attempt:
lang = c("it.wikipedia")
bm = c("ECB", "Christine Lagarde", "Philip Lane")
for (i in seq_along(lang)) {
for (j in seq_along(bm)) {
skip_to_next <- FALSE
tryCatch(x[[i]][[j]] = article_pageviews(project = lang[i], article = bm[j], platform = "all", user_type = "user", start = "2015100100", end = today(), reformat = TRUE, granularity = "daily"), error = function(e) {skip_to_next <<- TRUE})
if(skip_to_next) { next }
}
}
Can anyone help me run the loop and skip whenever it meets an error?
Thanks a lot!
You can use tryCatch as :
library(pageviews)
library(purrr)
lang = c("it.wikipedia")
bm = c("ECB", "Christine Lagarde", "Philip Lane")
map_df(lang, function(x) map_df(bm, function(y)
tryCatch(article_pageviews(project = x, article = y, platform = "all", user_type = "user", start = "2015100100", end = today(), reformat = TRUE, granularity = "daily"),
error = function(e) {}))) -> result

Pagination loop is stuck at x > 99

I am scraping some data from an API, and my code works just fine as long as I extract pages 0 to 98. Whenever my loop reaches 99, I get an error Error: Internal Server Error (HTTP 500)..
Tried to find an answer but I am only proficient in R and C# and cannot understand Python or other.
keywords = c('ABC OR DEF')
parameters <- list(
'q' = keywords,
num_days = 1,
language = 'en',
num_results = 100,
page = 0,
'api_key' = '123456'
)
response <- httr::GET(get_url, query = parameters)
# latest_page_number <- get_last_page(parsed)
httr::stop_for_status(response)
content <- httr::content(response, type = 'text', encoding = 'utf-8')
parsed <- jsonlite::fromJSON(content, simplifyVector = FALSE, simplifyDataFrame = TRUE)
num_pages = round(parsed[["total_results"]]/100)
print(num_pages)
result = parsed$results
for(x in 1:(num_pages))
{
print(x)
parameters <- list(
'q' = keywords,
page = x,
num_days = 7,
language = 'en',
num_results = 100,
'api_key' = '123456'
)
response <- httr::GET(get_url, query = parameters)
httr::stop_for_status(response)
content <- httr::content(response, type = 'text', encoding = 'utf-8')
# content <- httr::content(response)
parsed <- jsonlite::fromJSON(content, simplifyVector = FALSE, simplifyDataFrame = TRUE)
Sys.sleep(0.2)
result = rbind(result,parsed$results[,colnames(result)])
}

Resources