library(httr)
library(readr)
library(assertthat)
library(jsonlite)
library(purrr)

baseurl<-"https://play.dhis2.org/2.38nightly/"
username<-"admin"
password<-"district"

loginDHIS2<-function(baseurl,username,password) {
  url<-paste0(baseurl,"api/me")
  r<-GET(url,authenticate(username,password))
  assert_that(r$status_code == 200L) }

loginDHIS2(baseurl,username,password)

# get dataset
dataSet<-"BfMAe6Itzgt"
period<-"202101"
ou<-"DiszpKrYNg8"

url<-paste0(baseurl,
            "api/dataValueSets.json?",
            "dataSet=", dataSet,
            "&period=",period,
            "&orgUnit=",ou)

output<-fromJSON(content(GET(url),type="text", encoding="UTF-8"))

# output
head(output$dataValues) #note that "dataValues" object is in tabular format with data

# now select and update a data value within output
purrr::pluck(output,"dataValues","value",1)<-103

# head(output$dataValues)
# double check its updated

#POST back to server

resp<-httr::POST(url,body=toJSON(output), content_type_json())

resp$status_code #check 200 response

Detail: https://community.dhis2.org/t/send-data-to-dhis2-instance-from-r/52665