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

Delete method #372

Closed
wibeasley opened this issue Nov 21, 2021 · 4 comments · Fixed by #373 or #376
Closed

Delete method #372

wibeasley opened this issue Nov 21, 2021 · 4 comments · Fixed by #373 or #376
Assignees

Comments

@wibeasley
Copy link
Member

A recent post on an old community thread made we realize REDCapR doesn't implement the delete function. I'm kinda surprised no one has requested it.

@wibeasley wibeasley self-assigned this Nov 21, 2021
@joundso
Copy link
Contributor

joundso commented Nov 22, 2021

Very raw suggestion (funnily I just needed it a few days ago):

install.packages("httr")
 
remove_ids <- c(9, 10, 11)
 
url <- Sys.getenv("REDCAP_API_URL")
formData <-
  list(
    token = Sys.getenv("REDCAP_API_KEY"),
    action = "delete",
    content = "record"
    # ,'records[0]' = '2HA9FF0A',
    # 'records[1]' = '2J9YPFTE',
    # 'records[2]' = '2J49KFRP'
  )
i <- 0
for (id in remove_ids) {
  formData[[paste0("records[", i, "]")]] <- id
  i <- i + 1
}
 
response <- httr::POST(url, body = formData, encode = "form")
if (httr::http_error(response)) {
  stop("Error in HTTP request to REDCap.")
} else {
  print(httr::content(x = response, as = "text"))
}

@wibeasley
Copy link
Member Author

@joundso, thanks for thinking & working on this, I just noticed #373 in GitHub Actions that you're working on this too. I should have refreshed this report before I pushed what I was working on last night.

It looks like we're about in the same place too: the single-arm deletion is working, the parameter for the multiple-arm project needs to be added. Do you want to work on that? If so, I'll step away and let you do your thing. I'll use your code, and feel free to take anything from my dev branch that you find helpful.

Two things to note:

  1. you may prefer the way I add records to the post_body without declaring a loop.
  2. I'm thinking that a error should be thrown (with stop()) if it's unsuccessful. But I'm open to ideas. I think overall REDCapR needs to do a better job throwing & communicating errors. I thought I 'd try errors for this function, and see how if works well with other people.

@joundso
Copy link
Contributor

joundso commented Nov 24, 2021

Hi @wibeasley - wow - great work! Sorry for interrupting/overlapping.
I really like the way you wrote the functions!

  1. Absolutely right. My loop is bad performing:
## Cleanup the backend in RStudio:
cat("\014") # Clears the console (imitates CTR + L)
rm(list = ls()) # Clears the Global Environment/variables/data
invisible(gc()) # Garbage collector/Clear unused RAM

records_to_delete <- sample(x = 1:1e10,
                            size = 1e3,
                            replace = FALSE)

loop_assignment <- function(vec) {
  i <- 0
  res <- list()
  for (id in vec) {
    res[[paste0("records[", i, "]")]] <- id
    i <- i + 1
  }
  return(res)
}

name_assignment <- function(vec) {
  return(stats::setNames(vec,
                         sprintf("records[%i]", seq_along(vec) - 1)))
}


microbenchmark::microbenchmark(loop_assignment(records_to_delete),
                               name_assignment(records_to_delete))
#> Unit: microseconds
#>                                expr     min       lq     mean   median      uq
#>  loop_assignment(records_to_delete) 10574.7 10855.15 11702.27 11157.45 11929.3
#>  name_assignment(records_to_delete)   247.3   269.55   318.45   292.50   313.7
#>      max neval
#>  18396.4   100
#>   2613.0   100

Created on 2021-11-24 by the reprex package (v2.0.1)

  1. I would like to see more errors being thrown. So I can only endorse the suggestion to include stop().

Unfortunately I won't be able to contribute much meaningful code in the coming days - but I will continue to follow your developments with interest! 👀

@wibeasley
Copy link
Member Author

Cool. Thanks for the feedback, I'll take it from here. I saw things in your code that I like, and I'll try to do it so you show up in the commits.

And I wouldn't worry too much about the loop/vector performance. Even though you say my way is 1sec faster, I'm guessing this delay will be dwarfed by the network latency & delete operation on MySQL.

wibeasley added a commit that referenced this issue Nov 25, 2021
I'll add guard rails in a sec.

ref #372
@wibeasley wibeasley reopened this Nov 25, 2021
wibeasley added a commit that referenced this issue Nov 25, 2021
wibeasley added a commit that referenced this issue Nov 25, 2021
@wibeasley wibeasley mentioned this issue Nov 25, 2021
wibeasley added a commit that referenced this issue Nov 25, 2021
wibeasley added a commit that referenced this issue Nov 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants