Home > Back-end >  Update Purrr loop to input data row by row in R
Update Purrr loop to input data row by row in R

Time:08-26

This question kinda builds on questions I asked enter image description here

When I do traceback() I get this:

enter image description here

When I step through the loop line by line I realized that in this chunk of code:

call2<-paste(base,endpoint, protocol, sep="")   
httpResponse <- GET(call2, add_headers(authorization = token))
results = fromJSON(content(httpResponse, "text"))


results$protocolId<- "8887"  ## doesn't seem to matter
results$protocolNo<- testprotocols$protocol_no
results$library<- as.character(testprotocols$library)
results$title<- testprotocols$title
results$nctNo<-testprotocols$nct_number
results$objectives<-"To see if the API works, specifically if you can write over a previous number"
results$shortTitle<- "Short joseph Title"
results$nctNo<-testprotocols$nct_number
results$department <- as.character(testprotocols$department)
results$organizationalUnit<- as.charater(testprotocols$organizational_unit)
results$protocolType<- as.character(testprotocols$protocol_type)

Where I had envisioned it downloading ONE sample study and replacing aspects of it with variables from ONE row of my beginning dataframe, its actually trying to paste everything in the column in there. I.e. results$nctNo is "654321 543210" instead of just "654321" from the first row.

TL;DR version:

I need my purrr loop to take one row at a time instead of my entire column, and I think if I do that, it'll all magically work.

CodePudding user response:

Within UpdateAccountNumbers(), you are referring to entire columns of the testprotocols frame when you do things like results$nctNo<-testprotocols$nct_number ....

Instead, perhaps at the top of the UpdateAccountNumbers() function, you can do something like tp = testprotocols[testprotocols$protocol_no == protocol,], and then when you are trying to assign values to results you can refer to tp instead of testprotocols

Note that your purrr::walk() command is passing just one value of protocol at a time to the UpdateAccountNumbers() function

  • Related