Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds new end-points and improves to existing ones #5

Merged
merged 17 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
on:
push:
branches:
- master
pull_request:
branches:
- master

name: R-CMD-check
on: [push, pull_request]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check all the things sounds good to me!


jobs:
R-CMD-check:
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(create_cohort)
export(extract_genotypic_data)
export(extract_participants)
export(list_cohorts)
47 changes: 47 additions & 0 deletions R/create_cohort.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#' @title Create Cohort
#'
#' @description Creates a new Cohort
#'
#' @param baseurl Base URL of the CloudOS server. (Required)
#' @param auth An authentication method. (Required)
#' Example - Bearer token or API key.
#' @param teamid Team ID in CloudOS account. (Required)
#' @param cohort_name New cohort name to be created. (Required)
#' @param cohort_desc New cohort description to be created. (Required)
#' @param filters WIP - details will be added.
#'
#' @return A dataframe.
#'
#' @examples
#' \dontrun{
#' create_cohort(baseurl= "https://cloudos.lifebit.ai",
#' auth = "Bearer ***token***",
#' teamid = "***teamid***",
#' cohort_name = "my cohort",
#' cohort_desc = "my cohort description")
#' }
#' @export
create_cohort <- function(baseurl, auth, teamid,
cohort_name, cohort_desc, filters = "") {
url <- paste(baseurl, "api/v1/cohort/", sep = "/")
r <- httr::POST(url,
httr::add_headers(.headers = c("Authorization" = auth,
"accept" = "application/json, text/plain, */*",
"content-type" = "application/json;charset=UTF-8")),
query = list("teamId" = teamid),
body = list("name" = cohort_name,
"description" = cohort_desc,
"moreFilters" = filters),
encode = "json"
)
if (!r$status_code == 200) {
message("Something went wrong. Not able to create a cohort")
}else{
Copy link
Contributor

@cgpu cgpu Aug 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (r$status_code == 200)

on this, I generally think if is more explicit and helps with matching assumptions with tests.

This is also Jenny Bryan approved, see here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a legit point 👍 This will make the code even more readable as well. Let me do that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sk-sahu thanks for being on the Jenny Bryan side of life 😄 !

One note, after checking the code:

)
if (!r$status_code == 200) {
stop("Something went wrong. Not able to create a cohort")
}
# parse the content
message("Cohort named ", cohort_name, " created successfully. Bellow are the details")
res <- httr::content(r)
# into a dataframe

Once you remove else, the idea is to replace it with the explicit if statement, not eliminate it altogether.
This is fine for now, I will open an issue and link it to your currently open PR to implement this in all functions.

So in principle as an example the following snippet, when if-ified would lokk like this:

if (!r$status_code == 200) {
stop("Something went wrong. Not able to create a cohort")
}else{
message("Cohort named ", cohort_name, " created successfully. Bellow are the details")
res <- httr::content(r)
# into a dataframe
res_df <- do.call(rbind, res)
colnames(res_df) <- "details"
return(res_df)
}

if (!r$status_code == 200) { 
    stop("Something went wrong. Not able to create a cohort") 
 }
if (r$status_code == 200){
    message("Cohort named ", cohort_name, " created successfully. Bellow are the details") 
    res <- httr::content(r) 
    # into a dataframe 
    res_df <- do.call(rbind, res) 
    colnames(res_df) <- "details" 
    return(res_df) 
 } 

message("Cohort named ", cohort_name, " created successfully. Bellow are the details")
res <- httr::content(r)
# into a dataframe
res_df <- do.call(rbind, res)
colnames(res_df) <- "details"
return(res_df)
}
}
43 changes: 43 additions & 0 deletions R/extract_genotypic_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#' @title Extract Genotypic Data
#'
#' @description Extract Genotypic Data with filter.
#'
#' @param baseurl Base URL of the CloudOS server. (Required)
#' @param auth An authentication method. (Required)
#' Example - Bearer token or API key.
#' @param teamid Team ID in CloudOS account. (Required)
#' @param page_number Number of page. (Optional) Default - 0
#' @param page_size Number of entries in a page. (Optional) Default - 10
#' @param filters WIP - details will be added.
#'
#' @return A dataframe.
#'
#' @examples
#' \dontrun{
#' extract_genotypic_data(baseurl= "https://cloudos.lifebit.ai",
#' auth = "Bearer ***token***",
#' teamid = "***teamid***")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make everythang snakecase eg
baseurl -> base_url
teamid -> team_id

in all occurrences?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

#' }
#' @export
extract_genotypic_data <- function(baseurl, auth, teamid,
page_number = 0,
page_size = 10,
filters = "") {
url <- paste(baseurl, "api/v1/cohort/genotypic-data", sep = "/")
r <- httr::POST(url,
httr::add_headers(.headers = c("Authorization" = auth,
"accept" = "application/json, text/plain, */*",
"content-type" = "application/json;charset=UTF-8")),
query = list("teamId" = teamid),
body = list("pageNumber" = page_number,
"pageSize" = page_size,
"filters" = filters),
encode = "json"
)
if (!r$status_code == 200) {
message("Something went wrong.")
}else{
res <- httr::content(r)
return(res$participants)
}
}
10 changes: 7 additions & 3 deletions R/extract_participants.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ extract_participants <- function(baseurl, auth, raw_data) {
body = raw_data,
encode = "json"
)
res <- httr::content(r, as = "text")
df <- utils::read.csv(textConnection(res))
return(df)
if (!r$status_code == 200) {
message("Something went wrong.")
}else{
res <- httr::content(r, as = "text")
df <- utils::read.csv(textConnection(res))
cgpu marked this conversation as resolved.
Show resolved Hide resolved
return(df)
}
}
10 changes: 5 additions & 5 deletions R/list_cohorts.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' @title Limited Cohort Data Extractor
#' @title List cohorts
#'
#' @description Extracts the data frame with limited cohort data columns.
#'
Expand Down Expand Up @@ -29,15 +29,15 @@ list_cohorts <- function(baseurl,
query = list("teamId" = teamid,
"pageNumber" = page_number,
"pageSize" = page_size))
res <- httr::content(r)
if(length(res) == 0){
if (!r$status_code == 200) {
message("No cohorts found. Or not able to connect with server.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here why message and not error to exit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion. There is stop() for error message and exit. I'll include that.

} else {
res <- httr::content(r)
message("Total number of cohorts found - ", res$total)
cohorts <- res$cohort
# make in to a list
cohorts_list <- list()
for(n in 1:res$total){
for (n in 1:res$total) {
dta <- data.frame(id = cohorts[[n]]$`_id`,
name = cohorts[[n]]$`name`,
description = cohorts[[n]]$`description`,
Expand All @@ -50,7 +50,7 @@ list_cohorts <- function(baseurl,
# cohorts[[1]]$`filters`
}
# make in to a dataframe
cohorts_df = do.call(rbind, cohorts_list)
cohorts_df <- do.call(rbind, cohorts_list)
return(cohorts_df)
}
}
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ if (!require(remotes)) { install.packages("remotes") }
remotes::install_github("lifebit-ai/cloudos")
```

## Example
## Documentation

This is a basic example.
Online pkgdown generated doc - https://lifebit-ai.github.io/cloudos/

``` r
library(cloudos)
All the avaiable functions - https://lifebit-ai.github.io/cloudos/reference/index.html

extract_participants(baseurl= "https://cloudos.lifebit.ai",
auth = "Bearer ***token***",
raw_data = "a JSON string for selected participants")
A vignette with examples on - [Access Lifebit’s cohort broswer in R](https://lifebit-ai.github.io/cloudos/articles/cohort_browser.html)

```

6 changes: 6 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
url: https://lifebit-ai.github.io/cloudos/
authors:
Lifebit:
href: https://lifebit.ai
development:
mode: auto
14 changes: 7 additions & 7 deletions docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/LICENSE-text.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/LICENSE.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading