R get_ga function: filter component - r

I want to get Google analytic data from a specific list of cardnumbers. The component ga:dimension10 contains the cardnumbers. The following code works:
ga_datasubset <- subset(get_ga(id, Startdatum, Einddatum,
metrics = c("ga:sessions", " ga:pageviews","ga:sessionDuration"),
dimensions="ga:dimension10, ga:deviceCategory, ga:medium",
fetch.by ="day"),
dimension10 %in% Datatest[,1])
But I want to make this code without using the subset function. I tried the code below, but this doesn’t work.
ga_datasubset <- get_ga(id, Startdatum, Einddatum,
metrics = c("ga:sessions", " ga:pageviews","ga:sessionDuration"),
dimensions="ga:dimension10, ga:deviceCategory, ga:medium",
filters ="ga:dimension10 %in% Datatest[,1]" ,
fetch.by ="day")
Error: Invalid parameter: Invalid value 'ga:dimension10 %in% Datatest[,1]' for filters parameter.
Any help will be greatly appreciated

Related

googledrive::drive_mv gives error "Parent specified via 'path' is invalid: x Does not exist"

This is a weird one and I am hoping someone can figure it out. I have written a function that uses googlesheets4 and googledrive. One thing I'm trying to do is move a googledrive document (spreadsheet) from the base folder to a specified folder. I had this working perfectly yesterday so I don't know what happened as it just didn't when I came in this morning.
The weird thing is that if I step through the function, it works fine. It's just when I run the function all at once that I get the error.
I am using a folder ID instead of a name and using drive_find to get the correct folder ID. I am also using a sheet ID instead of a name. The folder already exists and like I said, it was working yesterday.
outFolder <- 'exact_outFolder_name_without_slashes'
createGoogleSheets <- function(
outFolder
){
folder_id <- googledrive::drive_find(n_max = 10, pattern = outFolder)$id
data <- data.frame(Name = c("Sally", "Sue"), Data = c("data1", "data2"))
sheet_id <- NA
nameDate <- NA
tempData <- data.frame()
for (i in 1:nrow(data)){
nameDate <- data[i, "Name"]
tempData <- data[i, ]
googlesheets4::gs4_create(name = nameDate, sheets = list(sheet1 = tempData)
sheet_id <- googledrive::drive_find(type = "spreadsheet", n_max = 10, pattern = nameDate)$id
googledrive::drive_mv(file = as_id(sheet_id), path = as_id(folder_id))
} end 'for'
} end 'function'
I don't think this will be a reproducible example. The offending code is within the for loop that is within the function and it works fine when I run through it step by step. folder_id is defined within the function but outside of the for loop. sheet_id is within the for loop. When I move folder_id into the for loop, it still doesn't work although I don't know why it would change anything. These are just the things I have tried. I do have the proper authorization for google drive and googlesheets4 by using:
googledrive::drive_auth()
googlesheets4::gs4_auth(token = drive_token())
<error/rlang_error>
Error in as_parent():
! Parent specified via path is invalid:
x Does not exist.
Backtrace:
global createGoogleSheets(inputFile, outPath, addNames)
googledrive::drive_mv(file = as_id(sheet_id), path = as_id(folder_id))
googledrive:::as_parent(path)
Run rlang::last_trace() to see the full context.
Backtrace:
x
-global createGoogleSheets(inputFile, outPath, addNames)
-googledrive::drive_mv(file = as_id(sheet_id), path = as_id(folder_id))
\-googledrive:::as_parent(path)
\-googledrive:::drive_abort(c(invalid_parent, x = "Does not exist."))
\-cli::cli_abort(message = message, ..., .envir = .envir)
\-rlang::abort(message, ..., call = call, use_cli_format = TRUE)
I have tried changing the folder_id to the exact path of my google drive W:/My Drive... and got the same error. I should mention I have also tried deleting the folder and re-creating it fresh.
Anybody have any ideas?
Thank you in advance for your help!
I can't comment because I don't have the reputation yet, but I believe you're missing a parenthesis in your for-loop.
You need that SECOND parenthesis below:
for (i in 1:nrow(tempData) ) {
...
}

Having problem with ggmap's mapdist() function

I have this code. I have my google API set up already, registered as well in R, Distance Matrix API has been initiated as well in the Google Cloud console.
Here is the dataframe I have, random 25 postal codes FROM and TO postal codes.
Dataset_test = data.frame(
FROM_POSTAL = c("V8A 0E5","T4G 6M4","V1N 8X3",
"C1B 5G1","R5H 2L4","H9S 8L4","L8E 4Y0","H2Y 7N6",
"K1B 7C0","G4A 5B0","E4P 3T2","E4V 5P4","H3J 1R5",
"G0B 4J7","E7A 6E7","E5B 2Y9","S4H 1T8","A2V 4G5",
"V8L 2A9","T9E 1M5","A5A 5M2","E4T 5B4","S2V 6C4",
"S9H 5P8","B1Y 0V0"),
TO_POSTAL = c("G0J 0B8","N0H 9N4","J9B 4Y4",
"L3Z 2Y7","E8K 4R4","B4P 7X9","S4H 2M0","A1Y 0B8",
"A1W 1E9","P9N 7X1","E4R 4B0","N0P 0M8","E1W 9Y7",
"T9W 8E2","G6X 4S9","A0E 0V4","J5X 7N8","N4N 8A1",
"V9K 0B9","L4G 3H7","E1W 0T2","G5R 9G3","L7C 9S2",
"E8P 2X6","E2A 2M1")
)
Here is the simple script I have to try to calculate the distance between the two postal codes by driving using Google's Distance Matrix API.
Driving_Distance = mapdist(from = Dataset_test[["FROM_POSTAL"]], to = Dataset_test[["TO_POSTAL"]], mode = c("driving")) %>% distinct()
When I run this, it throws an error in the Driving_Distance - says
Error: Argument 1 is a list, must contain atomic vectors
Your Canadian postal codes are hereby working with the mapdist() function.
The number of addresses used here were shortened for the sake of brevity.
A tibble was used instead of a dataframe so that the variables were character data types rather than factor data types. The actual Google API key that was used has been replaced with some text.
This was a good mapping question. The working code and output below:
library(ggmap)
library(plyr)
library(googleway)
library(tidyverse)
df = tibble(
FROM_POSTAL = c("V8A 0E5","T4G 6M4","V1N 8X3",
"C1B 5G1","R5H 2L4","H9S 8L4"),
TO_POSTAL = c("G0J 0B8","N0H 9N4","J9B 4Y4",
"L3Z 2Y7","E8K 4R4","B4P 7X9"))
dd <- apply(df, 1, function(x){
google_distance(origins = list(x["from"]),
destinations = list(x["to"]),
key="My_secret_key")
})
dd

how to round specific columns by function using R?

I want to round off specific columns with each column have different rounding values. I tried with the following code but it gives an error:
roundCols <-function(repo, namcol, digiround){
repo[,"namcol"] = round(repo[,"namcol"], digits = digiround)
round.staus = TRUE
return(round.staus)
}
round.staus = FALSE
ils <- config[13]$ignoreColumns
ils <- gsub("\\{|\\}", "", ils)
ils <- ils %>% str_replace_all("\\&", ",")
coldrp <- unlist(strsplit(ils, "\\,"))
coldrp = gsub("[^a-zA-Z]+", ".", coldrp)
td <- fread(config[13]$save.location,stringsAsFactors = FALSE,drop=coldrp,blank.lines.skip = TRUE)
col_rnm <- c(names(td[,2]),names(td[,3])) #it has 2 column who's will be round off
col_rd <- c(2,3) #it gives digits how much rounding off required
for (i in 1:length(col_rnm)) {
round.staus = roundCols(td,col_rnm,col_rd[i])
}
td
error is :
Error in [.data.table(repo, , "namcol") :
column(s) not found: namcol
I tried the same given in function on a console which gives an exact result.
Expected Output:
Account Chargeable.Capacity Expected.Capacity.in.30.days Deviation
Kishore 0.01 0.007 3.778268e-11
Initially My data :
Account Chargeable.Capacity Expected.Capacity.in.30.days Deviation
Kishore 0.007124108 0.007283185 3.778268e-11
above what is expected from the function given the code. Help me to solve that error. The effort will be appreciated.
Do this instead:
for (i in 1:length(col_rnm)) {
set(td, , col_rnm[i], round(td[, col_rnm[i], with = FALSE], col_rd[i]))
}
If you look at the help page for ?set (same help page as ?":="), you'll see it described as
set is a low-overhead loop-able version of :=
You'll find set used in many answers here, such as this one and this one.
Reasons your approach didn't work:
You're missing an i in your loop: roundCols(td,col_rnm,col_rd[i]) needs to use col_rnm[i]
Your roundCols function neither updates the data by reference using data.table syntax (either set() or :=), nor does it return the updated data, so any changes are local to the function
The string "namcol" with quotes is just a string. To use the argument namcol, you need to use it without quotes.
You don't need an extra function for this---the approach above with set is simpler.

Character string truncated when passed to function

I have a long list of Campaign names that I need to collapse to a character vector of length 1 and then pass as the "where" clause in a call to the Google AdWords API through the "RAdwords" package.
Creating this character string is not a problem until its length gets to a certain point that the values are truncated, which causes an error in AdWords API call.
Here is a sample of the setup that will not cause an error:
campaigns <- paste0("Campaign ", seq(1,5))
collapsed_campaigns <- paste0(campaigns, collapse = "','")
campaign_filter1 <- paste("CampaignName IN ['", collapsed_campaigns, "']")
And here is a setup that will cause an error:
campaigns <- paste0("Campaign ", seq(1,50))
collapsed_campaigns <- paste0(campaigns, collapse = "','")
campaign_filter2 <- paste("CampaignName IN ['", collapsed_campaigns, "']")
Inspecting the structure of each variable shows:
> str(campaign_filter1)
chr "CampaignName IN [' Campaign 1','Campaign 2','Campaign 3',
'Campaign 4','Campaign 5 ']"
> str(campaign_filter2)
chr "CampaignName IN [' Campaign 1','Campaign 2','Campaign 3',
'Campaign 4','Campaign 5','Campaign 6','Campaign 7','Campaign 8','Camp"| __truncated__
If I pass 'campaign_filter1' as my where clause in RAdwords, things run as expected.
If I pass 'campaign_filter2' as the where clause, I get this error:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><reportDownloadError>
<ApiError><type>QueryError.INVALID_WHERE_CLAUSE</type><trigger></trigger>
<fieldPath></fieldPath></ApiError></reportDownloadError>
It seems the "| truncated" is getting passed literally to the RAdwords function.
Here is the result of inspecting the structure of "traffic_data" in a failed call to RAdwords:
> str(traffic_data)
Classes ‘data.table’ and 'data.frame': 1 obs. of 1 variable:
$ ads: chr "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
<reportDownloadError><ApiError><type>QueryError.INVALID_WHERE_CLAU"| __truncated__
- attr(*, ".internal.selfref")=<externalptr>
Obviously, I could get around this some sort of looping function and call the data from the API one campaign at a time, but that would be horribly inefficient. How can I get the entirety of the character string to be passed to RAdwords?
One question upfront: Why don´t you donwload all campaign data and filter the result dataframe in R? With this strategy the whole string campaign name pasting process would become superfluous. You could filter the dataframe based on vector operations in R. This approach probably is more rubust and less vulnerable.
However, if you want to filter campaigns explicitly in your API call you can do it with this code:
# 1. Download all campaigns
# query all campaign names
body1 <- statement(select=c('CampaignName'),
report="CAMPAIGN_PERFORMANCE_REPORT",
start="2017-11-01",
end="2017-11-02")
# download all campaign names
campaigns <- getData(clientCustomerId = "***-***-****",
google_auth = google_auth,
statement = body,
apiVersion = "201710",
transformation = T,
changeNames = T)
# 2. Build query with all campaigns in where clause
# build string for where clause
cmp_string <- paste0(campaigns$Campaign, collapse = "','")
cmp_string <- paste("CampaignName IN ['", cmp_string, "']", sep = "")
# query all campaigns with where condition
body2 <- statement(select = c('CampaignName'),
where = cmp_string,
report = "CAMPAIGN_PERFORMANCE_REPORT",
start = "2017-11-01",
end = "2017-11-02")
# download all campaigns using the where clause
campaigns2 <- getData(clientCustomerId = "***-***-****",
google_auth = google_auth,
statement = body,
apiVersion = "201710",
transformation = T,
changeNames = T)
In the first part I download all campaign names to have data for the where clause. In the second part I demonstrate how to download all campaigns again utilizing the where clause with all campaigns as filter.
I tested the code above with over 200 campaigns. Neither there were any issues with the RAdwords package nor with the Adwords API.
I suspect there are issues with the string you pass into campaign_filter2. Within paste() you miss to set sep = "". Otherwise you end up with a space in the beginning of the first campaign name.

Nestled Loop not Working to gather data from NOAA

I'm using the R package rnoaa(along with it required other packages) to gather historical weather data. I wrote this nestled loop to gather all the data sets but I keep getting errors when I run it. It seems to run for a second fine
The loop:
require('triebeard')
require('bindr')
require('colorspace')
require('mime')
require('curl')
require('openssl')
require('R6')
require('urltools')
require('httpcode')
require('stringr')
require('assertthat')
require('bindrcpp')
require('glue')
require('magrittr')
require('pkgconfig')
require('rlang')
require('Rcpp')
require('BH')
require('plogr')
require('purrr')
require('stringi')
require('tidyselect')
require('digest')
require('gtable')
require('plyr')
require('reshape2')
require('lazyeval')
require('RColorBrewer')
require('dichromat')
require('munsell')
require('labeling')
require('viridisLite')
require('data.table')
require('rjson')
require('httr')
require('crul')
require('lubridate')
require('dplyr')
require('tidyr')
require('ggplot2')
require('scales')
require('XML')
require('xml2')
require('jsonlite')
require('rappdirs')
require('gridExtra')
require('tibble')
require('isdparser')
require('geonames')
require('hoardr')
require('rnoaa')
install.package('ncdf4')
install.packages("devtools")
library(devtools)
install_github("rnoaa", "ropensci")
library(rnoaa)
list <- buoys(dataset='wlevel')
lid <- data.frame(list$id)
foo <- for(range in 1990:2017){
for(bid in lid){
bid_range <- buoy(dataset = 'wlevel', buoyid = bid, year = range)
bid.year.data <- data.frame(bid.year$data)
write.csv(bid.year.data, file='cwind/bid_range.csv')
}
}
The response:
Using c1990.nc
Using
Error: length(url) == 1 is not TRUE
It saves the first data-set but it does not apply the for in the file name it just names it bid_range.csv.
This error message shows that there are no any data of a given station id in 1990. Because you were using for loop, once it gots an error, it stops.
Here I introduce the use of tidyverse to download the NOAA buoy data. A lot of the following functions are from the purrr package, which is part of the tidyverse.
# Load packages
library(tidyverse)
library(rnoaa)
Step 1: Create a "Grid" containing all combination of id and year
The expand function from tidyr can create the combination of different values.
data_list <- buoys(dataset = 'wlevel')
data_list2 <- data_list %>%
select(id) %>%
expand(id, year = 1990:2017)
Step 2: Create a "safe" version that does not break when there is no data.
Also make this function suitable for the map2 function
Because we will use map2 to loop through all the combination of id and year using the map2 function by its .x and .y argument. We modified the sequence of argument to create buoy_modify. We also use the safely function to create a safe version of buoy_modify. Now when it meets error, it will store the error message and moves to the next one rather than breaks.
# Modify the buoy function
buoy_modify <- function(buoyid, year, dataset, ...){
buoy(dataset, buoyid = buoyid, year = year, ...)
}
# Creare a safe version of buoy_modify
buoy_safe <- safely(buoy_modify)
Step 3: Apply the buoy_safe function
wlevel_data <- map2(data_list2$id, data_list2$year, buoy_safe, dataset = "wlevel")
# Assign name for the element in the list based on id and year
names(wlevel_data) <- paste(data_list2$id, data_list2$year, sep = "_")
After this step, all the data were downloaded in wlevel_data. Each element in wlevel_data has two parts. $result shows the data if the download is successful, otherwise, it shows NULL. $error shows NULL if the download is successful, otherwise, it shows the error message.
Step 4: Access the data
transpose can turn a list "inside out". So now wlevel_data2 has two elements: result and error. We can store these two and access the data.
# Turn the list "inside out"
wlevel_data2 <- transpose(wlevel_data)
# Get the error message
wlevel_error <- wlevel_data2$error
# Get he result
wlevel_result <- wlevel_data2$result
# Remove NULL element in wlevel_result
wlevel_result2 <- wlevel_result[!map_lgl(wlevel_result, is.null)]

Resources