Updating resources with Ckanr

Hello!

I’m using the ckanr package for the first time and I’m having troubles when updating resources with resource_update(). The resource is updated but the existing metadata is deleted. I tried calling resource_show() (before updating) and see that the attributesDescription parameter is being deleted in the update, which I need to keep.

Any ideas on how to update just the resource file? Is there a way to do that without losing the metadata?

Hope I’ve been clear, english isn’t my first language.

Thanks in advance!

1 Like

@sharla do you have any advice for @eliansoutu ?

@eliansoutu, Sharla will come back to respond. They’re dealing with a big work deadline right now.

1 Like

Thanks a lot for the response @stefanie, no hurry!

1 Like

Hi @eliansoutu, my apologies for the long delay! Thank you for your patience :slight_smile: (and thank you @stefanie for keeping an eye on this for me!)

I can confirm that this behaviour is happening: when you update a resource, the metadata (e.g. description) is deleted:

(note key below is just an API key for the beta instance)

library(ckanr)
#> Loading required package: DBI

ckanr_setup("https://beta.ckan.org/")

# create package and resource
package <- package_create("ckanr-metadata-update-test", key = key)

resource <- resource_create(package[["id"]], description = "this is a resource with a description", key = key)

# check that resource has a description
resource_id <- resource[["id"]]

resource_show(resource_id)
#> <CKAN Resource> e810ca9e-880f-4213-9c52-d0538531c32d 
#>   Name: 
#>   Description: this is a resource with a description
#>   Creator/Modified: 2021-10-20T18:05:45.741174 / 
#>   Size: 
#>   Format:

# prep file to upload to resource
file <- "mtcars.csv"
write.csv(mtcars, file)

# update with file
resource_update(resource_id, path = file, key = key)
#> <CKAN Resource> e810ca9e-880f-4213-9c52-d0538531c32d 
#>   Name: 
#>   Description: 
#>   Creator/Modified: 2021-10-20T18:05:45.741174 / 2021-10-20T18:05:46.574034
#>   Size: 1783
#>   Format: CSV

# Look at it again - no longer has a description (and you can see from update call above, too)
resource_show(resource_id)$description
#> NULL

# What about if you pass the description when updating? 
resource_update(resource_id, path = file, key = key, extras = list(description = "this is a resource with a description"))$description
#> [1] "this is a resource with a description"

# Description sticks

It sounds like, from a previous issue (relating to updating packages, not resources, but same idea) that this behaviour is intended - the list of things to update should be “complete”, so not including metadata means the existing metadata will be removed.

So for now, I would suggest calling resource_show(), getting any fields you want from there, and also passing those when you do resource_update(), like this:

# Would suggest getting the resource, pulling its description (or whatever fields are needed), then passing that along with the resource_update()
resource <- resource_show(resource_id)
description <- resource$description

resource_update(resource_id, path = file, key = key, extras = list(description = description))
#> <CKAN Resource> e810ca9e-880f-4213-9c52-d0538531c32d 
#>   Name: 
#>   Description: this is a resource with a description
#>   Creator/Modified: 2021-10-20T18:05:45.741174 / 2021-10-20T18:05:48.196164
#>   Size: 1783
#>   Format: CSV

I understand that this is not clear in the documentation, as it says “Other metadata, such as name or description, are not updated.” - but it seems quite the opposite is true!

I will follow up in the issue I linked, since it looks like the intention was to update the documentation for package_update(), so maybe we can update for resource_update() at the same time to clarify what the actual behaviour is.

I hope this is helpful, please let me know if you have any more questions!

3 Likes

Hi @sharla ! Thanks a lot for your response and for giving such a detailed answer. It’s been really helpful (I’ve been able to do the update :grinning:).

I agree that it could be a good idea to clarify this behaviour in the documentation, for future users. Great proposal!

Thanks again,
Greetings from Argentina!

2 Likes