Skip to content

Commit ab02b4e

Browse files
author
kdaily
committed
lint
1 parent ba57394 commit ab02b4e

File tree

1 file changed

+66
-66
lines changed

1 file changed

+66
-66
lines changed

R/lib.R

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
#' @importFrom lubridate %m+%
33

44
# Theme for plots
5-
mytheme <- ggplot2::theme_bw() + ggplot2::theme(axis.text=ggplot2::element_text(size=16),
6-
axis.title.x=ggplot2::element_text(size=18),
7-
axis.title.y=ggplot2::element_text(size=18, angle=90))
5+
mytheme <- ggplot2::theme_bw() + ggplot2::theme(axis.text = ggplot2::element_text(size = 16),
6+
axis.title.x = ggplot2::element_text(size = 18),
7+
axis.title.y = ggplot2::element_text(size = 18, angle = 90))
88

99
# queryDict <- c('downloads'='select CLIENT,NORMALIZED_METHOD_SIGNATURE,PROJECT_ID,BENEFACTOR_ID,PARENT_ID,ENTITY_ID,AR.TIMESTAMP,RESPONSE_STATUS,DATE,USER_ID,NODE_TYPE,N.NAME from ACCESS_RECORD AR, PROCESSED_ACCESS_RECORD PAR, NODE_SNAPSHOT N, (select distinct ID from NODE_SNAPSHOT where PROJECT_ID = "%s") NODE where AR.TIMESTAMP Between %s AND %s and AR.SESSION_ID = PAR.SESSION_ID and AR.TIMESTAMP = PAR.TIMESTAMP and PAR.ENTITY_ID = NODE.ID and N.ID = NODE.ID and (PAR.NORMALIZED_METHOD_SIGNATURE = "GET /entity/#/file" or PAR.NORMALIZED_METHOD_SIGNATURE = "GET /entity/#/version/#/file");',
1010
# 'webAccess'='select NORMALIZED_METHOD_SIGNATURE,PROJECT_ID,BENEFACTOR_ID,PARENT_ID,ENTITY_ID,CONVERT(AR.TIMESTAMP, CHAR) AS TIMESTAMP,RESPONSE_STATUS,DATE,USER_ID,NODE_TYPE,N.NAME from ACCESS_RECORD AR, PROCESSED_ACCESS_RECORD PAR, NODE_SNAPSHOT N, (select distinct ID from NODE_SNAPSHOT where PROJECT_ID = "%s") NODE where AR.TIMESTAMP Between %s AND %s and AR.SESSION_ID = PAR.SESSION_ID and AR.TIMESTAMP = PAR.TIMESTAMP and PAR.ENTITY_ID = NODE.ID and N.ID = NODE.ID and CLIENT = "WEB" AND (PAR.NORMALIZED_METHOD_SIGNATURE = "GET /entity/#/bundle" OR PAR.NORMALIZED_METHOD_SIGNATURE = "GET /entity/#/version/#/bundle" OR PAR.NORMALIZED_METHOD_SIGNATURE = "GET /entity/#/wiki2" OR PAR.NORMALIZED_METHOD_SIGNATURE = "GET /entity/#/wiki2/#");')
@@ -13,23 +13,23 @@ mytheme <- ggplot2::theme_bw() + ggplot2::theme(axis.text=ggplot2::element_text(
1313
# q.browse <- sprintf(template, projectId, beginTimestamp, endTimestamp)
1414

1515
#' @export
16-
render_report <- function(project_id, team_order, data_file, reportType="report") {
16+
render_report <- function(project_id, team_order, data_file, reportType = "report") {
1717

18-
templates <- c("report"=system.file("templates", "report.Rmd",
19-
package = "synapseusagereports"))
18+
templates <- c("report" = system.file("templates", "report.Rmd",
19+
package = "synapseusagereports"))
2020

21-
myParams <- list(projectId=project_id,
22-
teamOrder=team_order,
23-
queryDataFile=data_file)
21+
myParams <- list(projectId = project_id,
22+
teamOrder = team_order,
23+
queryDataFile = data_file)
2424

2525
htmlFileName <- paste0(myParams[['projectId']], "_", reportType, "_",
2626
lubridate::today(), ".html")
2727

2828
outputFileName <- paste0("/tmp/", htmlFileName)
2929

30-
cat(rmarkdown::render(input=templates[[reportType]],
31-
output_file=outputFileName,
32-
params=myParams))
30+
cat(rmarkdown::render(input = templates[[reportType]],
31+
output_file = outputFileName,
32+
params = myParams))
3333

3434
}
3535

@@ -115,23 +115,23 @@ doQuery <- function(con, template, projectId, start_date, end_date) {
115115
message(sprintf("Query: %s", q))
116116
message(sprintf("Querying %s to %s", start_date, end_date))
117117

118-
res <- DBI::dbGetQuery(conn = con, statement=q)
118+
res <- DBI::dbGetQuery(conn = con, statement = q)
119119
return(res)
120120
}
121121

122122
#' @export
123123
processQuery <- function(data) {
124124
queryData <- data %>%
125-
dplyr::rename(userId=USER_ID, id=ENTITY_ID) %>%
125+
dplyr::rename(userId = USER_ID, id = ENTITY_ID) %>%
126126
dplyr::select(userId, id, DATE, TIMESTAMP, NODE_TYPE, NAME, recordType) %>%
127127
# dplyr::ungroup() %>%
128128
# rename(duplicateCount=n) %>%
129-
dplyr::mutate(date=as.Date(as.character(DATE)),
130-
userId=as.character(userId),
131-
id=as.character(id),
132-
dateGrouping=lubridate::floor_date(date, unit="month"),
133-
monthYear=paste(lubridate::month(dateGrouping, label=TRUE),
134-
lubridate::year(dateGrouping))) %>%
129+
dplyr::mutate(date = as.Date(as.character(DATE)),
130+
userId = as.character(userId),
131+
id = as.character(id),
132+
dateGrouping = lubridate::floor_date(date, unit = "month"),
133+
monthYear = paste(lubridate::month(dateGrouping, label = TRUE),
134+
lubridate::year(dateGrouping))) %>%
135135
dplyr::group_by(id, userId, TIMESTAMP, recordType) %>% # Get unique due to name changes, might not be most recent name!
136136
dplyr::arrange(TIMESTAMP) %>%
137137
dplyr::slice(1) %>%
@@ -152,25 +152,25 @@ processQuery <- function(data) {
152152
#' @export
153153
getData <- function(con, qTemplate, projectId, timestampBreaksDf) {
154154
q.create_temp <- "CREATE TEMPORARY TABLE PROJECT_STATS (`TIMESTAMP` bigint(20) NOT NULL, `ID` bigint(20) NOT NULL, PRIMARY KEY (`ID`,`TIMESTAMP`)); "
155-
create <- DBI::dbSendQuery(conn=con,
156-
statement=q.create_temp)
155+
create <- DBI::dbSendQuery(conn = con,
156+
statement = q.create_temp)
157157
message(sprintf("Created temporary table for entities in project %s", projectId))
158158

159159
q.insert_temp <- "INSERT INTO PROJECT_STATS (ID, TIMESTAMP) SELECT ID, MAX(TIMESTAMP) AS TIMESTAMP FROM NODE_SNAPSHOT WHERE PROJECT_ID = %s GROUP BY ID;"
160160
query_statement <- sprintf(q.insert_temp, projectId)
161-
insert <- DBI::dbSendQuery(conn=con,
162-
statement=query_statement)
161+
insert <- DBI::dbSendQuery(conn = con,
162+
statement = query_statement)
163163
message(sprintf("Inserted rows into temporary table for entities in project %s", projectId))
164164

165165
res <- plyr::ddply(timestampBreaksDf, plyr::.(month, year),
166-
function (x) doQuery(con=con,
167-
template=qTemplate,
168-
projectId=projectId,
169-
start_date=x$start_date,
170-
end_date=x$end_date
166+
function (x) doQuery(con = con,
167+
template = qTemplate,
168+
projectId = projectId,
169+
start_date = x$start_date,
170+
end_date = x$end_date
171171
))
172172

173-
foo <- DBI::dbSendQuery(conn=con, statement='DROP TABLE PROJECT_STATS;')
173+
foo <- DBI::dbSendQuery(conn = con, statement = 'DROP TABLE PROJECT_STATS;')
174174

175175
res
176176
}
@@ -182,8 +182,8 @@ getTeamMemberDF <- function(teamId) {
182182
foo <- foo$asList()
183183

184184
foo %>% {
185-
tibble(teamId=purrr::map_chr(., 'teamId'),
186-
userId=purrr::map_chr(., c("member", "ownerId")))
185+
tibble(teamId = purrr::map_chr(., 'teamId'),
186+
userId = purrr::map_chr(., c("member", "ownerId")))
187187
}
188188

189189
}
@@ -195,8 +195,8 @@ processTeamMemberList <- function(teamIds) {
195195
userList <- purrr::map_df(teamIds, getTeamMemberDF)
196196

197197
userList$teamId <- factor(userList$teamId,
198-
levels=teamIds,
199-
ordered=TRUE)
198+
levels = teamIds,
199+
ordered = TRUE)
200200

201201
userList <- userList %>%
202202
dplyr::group_by(userId) %>%
@@ -216,12 +216,12 @@ getQueryUserProfiles <- function(queryData, useTeamGrouping, userList) {
216216

217217
accessUsers <- plyr::llply(chunk(unique(queryData$userId), 50),
218218
function(x) synapser::synRestGET(sprintf("/userGroupHeaders/batch?ids=%s",
219-
paste(x, collapse=",")))$children)
219+
paste(x, collapse = ",")))$children)
220220

221221
accessUsersChildren <- do.call(c, accessUsers)
222222

223223
allUsersList <- plyr::ldply(accessUsersChildren, as.data.frame) %>%
224-
dplyr::mutate(userId=ownerId) %>%
224+
dplyr::mutate(userId = ownerId) %>%
225225
dplyr::select(userId, userName)
226226

227227
if (useTeamGrouping) {
@@ -244,14 +244,14 @@ getQueryUserProfiles <- function(queryData, useTeamGrouping, userList) {
244244
dplyr::select(teamId) %>% dplyr::distinct(.keep_all=TRUE)
245245

246246
teamInfo <- lapply(as.list(as.character(tmp_all_users$teamId)),
247-
function (x) synapser::synGetTeam(x)) %>%
247+
function(x) synapser::synGetTeam(x)) %>%
248248
{
249-
tibble(teamId=tmp_all_users$teamId,
250-
teamName=purrr::map_chr(., 'name'))
249+
tibble(teamId = tmp_all_users$teamId,
250+
teamName = purrr::map_chr(., 'name'))
251251
}
252252

253253
if (nrow(teamInfo) > 0) {
254-
allUsers <- dplyr::left_join(allUsers, teamInfo, by="teamId")
254+
allUsers <- dplyr::left_join(allUsers, teamInfo, by = "teamId")
255255
} else {
256256
allUsers$teamName <- "Registered Synapse User"
257257
}
@@ -279,7 +279,7 @@ countByMonth <- function(queryData, useTeamGrouping) {
279279
tmp <- queryData
280280

281281
if (!useTeamGrouping) {
282-
tmp <- tmp %>% dplyr::mutate(teamName='All')
282+
tmp <- tmp %>% dplyr::mutate(teamName = 'All')
283283
}
284284

285285
tmp %>%
@@ -293,7 +293,7 @@ countByDay <- function(queryData, useTeamGrouping) {
293293
tmp <- queryData
294294

295295
if (!useTeamGrouping) {
296-
tmp <- tmp %>% dplyr::mutate(teamName="All")
296+
tmp <- tmp %>% dplyr::mutate(teamName = "All")
297297
}
298298

299299
tmp %>%
@@ -305,23 +305,23 @@ countByDay <- function(queryData, useTeamGrouping) {
305305
#' @export
306306
plotByDay <- function(perdayCount, useTeamGrouping) {
307307
plotdata <- perdayCount %>%
308-
reshape2::dcast(date ~ teamName, value.var='n', fill=0) %>%
309-
reshape2::melt(., id.vars=c("date"),
310-
variable.name="teamName", value.name="n") %>%
311-
dplyr::rename(group=teamName)
308+
reshape2::dcast(date ~ teamName, value.var = 'n', fill = 0) %>%
309+
reshape2::melt(., id.vars = c("date"),
310+
variable.name = "teamName", value.name = "n") %>%
311+
dplyr::rename(group = teamName)
312312

313-
p <- ggplot2::ggplot(plotdata, ggplot2::aes(x=date, y=n))
314-
p <- p + ggplot2::geom_line(ggplot2::aes(group=group, color=group), size=1)
313+
p <- ggplot2::ggplot(plotdata, ggplot2::aes(x = date, y = n))
314+
p <- p + ggplot2::geom_line(ggplot2::aes(group = group, color = group), size = 1)
315315

316316
if (useTeamGrouping) {
317317
p <- p + ggplot2::scale_color_brewer(palette = "Set1")
318318
} else {
319-
p <- p + ggplot2::scale_color_manual(values="black")
319+
p <- p + ggplot2::scale_color_manual(values = "black")
320320
}
321321

322-
p <- p + mytheme + ggplot2::theme(axis.title.x=ggplot2::element_blank(),
323-
axis.text.x=ggplot2::element_text(size=16, angle=270),
324-
legend.position="top")
322+
p <- p + mytheme + ggplot2::theme(axis.title.x = ggplot2::element_blank(),
323+
axis.text.x = ggplot2::element_text(size = 16, angle = 270),
324+
legend.position = "top")
325325
p
326326

327327
}
@@ -333,8 +333,8 @@ uniqueUsersPerMonth <- function(queryData) {
333333
dplyr::distinct() %>%
334334
dplyr::filter(userName != "anonymous") %>%
335335
dplyr::group_by(dateGrouping) %>%
336-
dplyr::summarize(Users=n_distinct(userName)) %>%
337-
dplyr::rename(Date=dateGrouping)
336+
dplyr::summarize(Users = n_distinct(userName)) %>%
337+
dplyr::rename(Date = dateGrouping)
338338
}
339339

340340
#' @export
@@ -347,37 +347,37 @@ firstMonthToVisit <- function(queryData) {
347347
dplyr::arrange(dateGrouping) %>%
348348
dplyr::slice(1) %>%
349349
dplyr::ungroup() %>%
350-
dplyr::mutate(visit=1) %>%
350+
dplyr::mutate(visit = 1) %>%
351351
dplyr::count(dateGrouping)
352352

353353
missing <- unique(queryData$dateGrouping[!(queryData$dateGrouping %in% firstMonthVisit$dateGrouping)])
354354

355355
if (length(missing) > 0) {
356356
firstMonthVisit <- rbind(firstMonthVisit,
357-
data.frame(n=0,
358-
dateGrouping=missing))
357+
data.frame(n = 0,
358+
dateGrouping = missing))
359359
}
360360

361361
firstMonthVisit %>%
362362
dplyr::arrange(dateGrouping) %>%
363-
dplyr::rename(Date=dateGrouping, Users=n)
363+
dplyr::rename(Date = dateGrouping, Users = n)
364364
}
365365

366366
#' @export
367367
multiMonthVisits <- function(queryData) {
368368
queryData %>%
369369
dplyr::group_by(userName) %>%
370-
dplyr::summarize(monthsVisited=n_distinct(dateGrouping)) %>%
370+
dplyr::summarize(monthsVisited = n_distinct(dateGrouping)) %>%
371371
dplyr::filter(monthsVisited >= 2, userName != 'anonymous')
372372
}
373373

374374
#' @export
375375
makeDateBreaks <- function(nMonths) {
376-
thisDate <- lubridate::floor_date(lubridate::today(), "month")- lubridate::period(1, "months")
376+
thisDate <- lubridate::floor_date(lubridate::today(), "month") - lubridate::period(1, "months")
377377

378378
beginDates <- thisDate - (lubridate::period(1, "months") * 0:(nMonths - 1))
379379

380-
data.frame(date=beginDates, month=lubridate::month(beginDates), year=lubridate::year(beginDates))
380+
data.frame(date = beginDates, month = lubridate::month(beginDates), year = lubridate::year(beginDates))
381381
# # endDates <- beginDates + (lubridate::days_in_month(beginDates)) - lubridate::seconds(1)
382382
# # beginDates <- beginDates + lubridate::seconds(1)
383383
#
@@ -401,20 +401,20 @@ makeDateBreaksStartEnd <- function(start_date, end_date) {
401401

402402
beginDates <- end_date_floor - (lubridate::period(1, "months") * 0:(n_months))
403403

404-
tibble::tibble(start_date=beginDates) %>%
404+
tibble::tibble(start_date = beginDates) %>%
405405
dplyr::mutate(end_date = dplyr::lag(start_date),
406-
month=lubridate::month(start_date),
407-
year=lubridate::year(start_date)) %>%
406+
month = lubridate::month(start_date),
407+
year = lubridate::year(start_date)) %>%
408408
arrange(start_date)
409409
}
410410

411411
#' @export
412-
topNEntities <- function(queryData, allUsers, topN=20) {
412+
topNEntities <- function(queryData, allUsers, topN = 20) {
413413
plotdata <- queryData %>%
414414
dplyr::count(userName) %>%
415415
dplyr::ungroup() %>%
416416
dplyr::left_join(allUsers) %>%
417-
dplyr::mutate(userName=reorder(userName, n, ordered=TRUE))
417+
dplyr::mutate(userName = reorder(userName, n, ordered = TRUE))
418418

419419
plotdata %>%
420420
dplyr::top_n(topN, n) %>%

0 commit comments

Comments
 (0)