Query matched 0 results, verify your query - r

I'm trying to develop an dashboard in Excel, using R studio.
I have now the code:
session01 <- Init(start.date = '2015-02-01',
end.date = '2015-04-01',
dimensions = 'ga:date,ga:userType',
metrics = 'ga:sessions',
sort = 'ga:date',
segments = NULL,
max.results = 10000,
start.index = NULL,
table.id = 'ga:XXXXXXX')
session01 <- QueryBuilder(session01)
report02 <- GetReportData(session01, oauth_token_LO)
However, it get this message:
> report02 <- GetReportData(session01, oauth_token_LO)
Your query matched 0 results. Please verify your query using the Query Feed Explorer and re-run it.
Error in GetDataFeed(query.uri) :
no loop for break/next, jumping to top level
Does anybody know how to solve this?

Related

Insert R list into RPostgreSQL query

I'm running a postgreSQL query based on an automated list of ID's stored in an R list. I'm trying to determine how to include that R list in my query so I don't have to hard-code the ID's each time I run my query.
For example, I have a script that produces the list
id <- c("001","002","003")
and my query looks something like this:
SELECT *
FROM my_query
WHERE my_query.id_col IN ('001', '002', '003')
which I run using Rpostgres:
library(Rpostgres)
snappConnection <- DBI::dbConnect(RPostgres::Postgres(),
host = "host",
dbname = "dbname",
user = "user",
password = "pword",
port = 0000)
core.data <- dbGetQuery(conn = snappConnection,statement = SELECT * FROM my_query WHERE my_query.id_col IN ('001', '002', '003'))
Is there a way to reference my "id" list from R in my query so that when "id" updates to new values, the query also updates to those new values?
glue_sql from glue package should work:
query <- glue::glue_sql("
SELECT *
FROM my_query
WHERE my_query.id_col IN ({id*})
", .con = snappConnection)
core.data <- dbGetQuery(conn = snappConnection, statement = query)
#dave-edison's answer solved my problem. Concurrent to trying his, I got this to work.
I saved the query below as "my_query.sql"
SELECT *
FROM my_query
WHERE my_query.id_col IN ('string_to_replace')
then created a string and used gsub on the string.
library(tidyverse)
temp.script <- read_file("my_query.sql")
core.data.script <- gsub('string_to_replace',paste0(id,collapse = "', '"),temp.script)
From there I just ran my RPostgres script like above.

Write to Snowflake VARIANT column from R

I am trying to load data to snowflake using the following code, but getting an error.
con <- DBI::dbConnect(
drv = odbc::odbc(),
driver = "SnowflakeDSIIDriver",
server = "<>",
authenticator = 'externalbrowser',
warehouse = "<>",
database = "<>",
UID = "<>",
role = "<>"
)
DBI::dbAppendTable(con, name = DBI::Id(schema = "<>", table = "<>"), value = tmp[1:2,])
tmp was downloaded from Snowflake, the same table using RStudio:
```{sql connection=con, output.var = 'tmp'}
select top 10 *
FROM <>
```
The error seems to be stemming from a VARIANT column where I store a JSON string.
Error in new_result(connection#ptr, statement, immediate) :
nanodbc/nanodbc.cpp:1374: 22000: SQL compilation error:
Expression type does not match column data type, expecting VARIANT but got VARCHAR(2) for column FEATURES
I had this once and it was an invalid JSON (missing brackets somewhere). Probably this helps.

How to import 11 million row table into Rstudio from Google BigQuery? [code included]

I am trying to do some data exploration for this dataset I have. The table I want to import is 11 million rows. Here is the script and output
#Creating a variable for our BQ project space
project_id = 'project space'
#Query
Step1 <-
"
insertquery
"
#Executing the query from the variable above
Step1_df <- query_exec(Step1, project = project_id, use_legacy_sql = FALSE, max_pages = Inf,page_size = 99000)
Error:
Error in curl::curl_fetch_memory(url, handle = handle) :
Operation was aborted by an application callback
Is there a different bigquery library I can use ? Looking to also speed up the upload time .

How to trade multiple equities at once using Ibrokers in R

Using the Ibrokers account I know how to place a trade with one ticker which , in the example below I place a trade with "DAL"
library(IBrokers)
tws=twsConnect(clientId = 1,host = "localhost", port = 7497 )
contract=twsEquity(symbol = "DAL", exch = "SMART" )
order=twsOrder(action = "BUY", totalQuantity ="10", tif = "OPG" )
placeOrder(twsconn = tws, Contract = contract, Order = order)
However I am interested in trading multiple tickers at once for example how can I place an order to buy "DAL" and "AAL". How can I put multiple orders into IBrokers in R?
I doubt your code even works now. To place another order just place another order. Note that they must have separate ids and the ids have to increase every order.
library(IBrokers)
tws=twsConnect(clientId = 1,host = "localhost", port = 7497 )
id <- tws.reqids(1)
contract=twsEquity(symbol = "AAL", exch = "SMART" )
# give an order id
order=twsOrder(id, action = "BUY", totalQuantity ="10", tif = "OPG" )
placeOrder(twsconn = tws, Contract = contract, Order = order)
# increment id
id <- id+1
contract=twsEquity(symbol = "DAL", exch = "SMART" )
order=twsOrder(id,action = "BUY", totalQuantity ="10", tif = "OPG" )
placeOrder(twsconn = tws, Contract = contract, Order = order)
I don't think that code will work. The defaults for order look to be LMT with a price of 0. You may want to try orderType = "MKT". Check the documentation for IBrokers.
I would suggest using a different API, it doesn't look like you have used the R package much so it's not a big deal to switch. It probably won't work for much longer. I think it is unsupported and the API has changed since the last update.

Making an iterative query to an SQL database

I'm trying to get to make a query from R to a MySQL database. The query iterates over a list, and therefore changes dynamically. Each query based on each element in the list will also in general result in multiple rows being extract. The database I'm using can be downloaded from here: http://www.ghtorrent.org/msr14.html
In the end of the day all the results should end up in the same output, and look like this:
pull_req_id,user,action,created_at
12359,arthurnn,opened,1380126837
12359,rafaelfranca,discussed,1380127245
12359,arthurnn,discussed,1380127676
...
The code that I have now looks like this, but it's not working:
library(DBI)
library(RMySQL)
m <- dbDriver("MySQL");
con <- dbConnect(m, user='msr14', password='msr14', host='localhost', dbname='msr14');
all_rails_projects <- dbGetQuery(con, 'SELECT * FROM projects WHERE name = "rails";')
all_rails_prs <- dbGetQuery(con, 'SELECT id FROM pull_requests WHERE base_repo_id = 78852;')
out <- nrow(all_rails_prs)
names(out) <- as.list(all_rails_prs)
df <- c('pull_req_id', 'user', 'action', 'created_at')
out <- numeric(length(df))
names(out) <- df
for (i in nrow(all_rails_prs)) {
SQL <- paste("select user, action, created_at from
(
select prh.action as action, prh.created_at as created_at, u.login as user
from pull_request_history prh, users u
where prh.pull_request_id ='", all_rails_prs[i,], "'",
" and prh.actor_id = u.id
union
select ie.action as action, ie.created_at as created_at, u.login as user
from issues i, issue_events ie, users u
where ie.issue_id = i.id
and i.pull_request_id ='", all_rails_prs[i,], "'",
" and ie.actor_id = u.id
union
select 'discussed' as action, ic.created_at as created_at, u.login as user
from issues i, issue_comments ic, users u
where ic.issue_id = i.id
and u.id = ic.user_id
and i.pull_request_id ='", all_rails_prs[i,], "'",
"union
select 'reviewed' as action, prc.created_at as created_at, u.login as user
from pull_request_comments prc, users u
where prc.user_id = u.id
and prc.pull_request_id ='", all_rails_prs[i,], "'",
") as actions
order by created_at;", sep = "")
res <- dbGetQuery(con, SQL)
out[i] <- dbFetch(res, n = -1)
}
This generates the following error message:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘dbFetch’ for signature ‘"data.frame"’
In addition: Warning message:
In mysqlExecStatement(conn, statement, ...) :
RS-DBI driver warning: (unrecognized MySQL field type 7 in column 2 imported as character)
I've tried different variants, but they all result in some kind of error, so it seems as if I'm simply not setting up the query structure the right way. Anyone has any advice?
According to the docs, dbGetQuery calls fetch by default if the query is successful.
res is already a database and you can put it into out directly without having to fetch the records.
Also, if you want to store the results in a dataframe and not a list, you might want to try:
#get the results
res<-dbGetQuery(con, SQL)
#if it's not null, add the request id and rbind it to the out dataframe
if(!is.null(res)){
out<-rbind(out,cbind(rep(all_rails_prs[i,],nrow(res)),res))
}
There might also be an error in your for syntax, you might need for (i in 1:nrow(all_rails_prs))

Resources