API returns with null values

R help needed, please. To be clear: I'm almost positive I'm having R problems, not API problems.

I'm trying to make a data frame from the results of API calls to the RStudio Connect server to get usage information using R calls. The return is a list of usage records, with each record being a list of key-value pairs.

{'content_guid':'a889911ab...', 'user_guid':'9802abc213',...}

with seven entries per inner list.

The problem is that some of the values are NULL, so when I convert to a data frame with

data.frame(matrix(unlist(payload$results), nrow=length(payload$results), byrow=T), stringsAsFactors=FALSE)

or anything similar, the key-value entries with NULLs are dropped so that the record has, say, five pairs rather than seven. Records from that point down go into the data frame misaligned and therefore garbage.

I've tried to convert the NULLs to NA or to empty strings by applying functions to the lists:

a[sapply(a, is.null)] <- NA
a[sapply(a, function(x) is.null(x))] <- NA
a[sapply(a, function(x) length(x)==0L)] <- NA

and many other variations. None make any difference.

If anyone has working code to make a properly-aligned data frame from the API's return list I'd appreciate it. Or a link to a StackOverflow page, or even a list of search terms which will give me a solution rather than something kinda but not quite like what I'm looking for.

Thanks.

Have you tried the connectapi package?

I have not. Thanks for the tip. I'll work with it tomorrow and then put in an update or reply in the thread.

I ended up using the technique in parse_connectapi() from the connectapi package and that accomplished everything I needed in terms of de-NULLing some values.

The full package wanted me to update a whole lot of R packages which I need to keep as-is for existing reports, and also complained that our three-month-old RSC version was too old.

Thank you for the assistance. I'm fully satisfied with the response.

1 Like

If you're worried about installing new package breaking existing workflows, you should check out {renv}.