Skip to content

Commit

Permalink
Metadata Correction
Browse files Browse the repository at this point in the history
  • Loading branch information
GMBog committed Oct 17, 2024
1 parent 06aa2c9 commit f687686
Show file tree
Hide file tree
Showing 21 changed files with 160 additions and 162 deletions.
4 changes: 2 additions & 2 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 1.0.0
Date: 2024-10-16 14:11:39 UTC
SHA: b738b472e7bfe04747898a65e8d48087fd328caa
Date: 2024-10-16 15:48:00 UTC
SHA: 06aa2c9af8d8b20b0846068ab9528f46a691e462
16 changes: 9 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: greenfeedr
Type: Package
Title: Process and Report GreenFeed Data
Title: Process and Report 'GreenFeed' Data
Version: 1.0.0
Authors@R:
c(
Expand All @@ -16,12 +16,14 @@ Authors@R:
role = c("ctb"))
)
Maintainer: Guillermo Martinez-Boggio <[email protected]>
Description: Functions for downloading, processing, and reporting GreenFeed data. The
GreenFeed system generates large amount of daily data that could be overwhelming for users.
So, the first step is to retrieve data from C-Lock system so use `get_gfdata()` to download via API.
Also, other function such as `report_gfdata()` allows to download and report daily and final data from GreenFeed.
The function `process_gfdata()` process daily or final data for further analysis. Two more functions as
`pellin()` and `viseat()` helps for processing and checking pellet intakes and visits to the GreenFeed.
Description: Downloading, Processing, and Reporting 'GreenFeed' Data.
The 'GreenFeed' system generates substantial amounts of daily data, which can be overwhelming for users.
This package facilitates data retrieval and reporting from the 'GreenFeed' system using a streamlined process.
Users can retrieve data from the 'C-Lock' system via an 'API' using the get_gfdata() function.
Additionally, the report_gfdata() function allows users to generate both daily and final reports from 'GreenFeed' data.
The process_gfdata() function further processes this data for analysis.
For pellet intake processing and visit checks, the package also provides the pellin() and viseat() functions,
ensuring a comprehensive toolset for managing and analyzing 'GreenFeed' data.
License: GPL (>= 3)
Encoding: UTF-8
LazyData: true
Expand Down
23 changes: 11 additions & 12 deletions R/get_gfdata.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
#' @name get_gfdata
#' @title Download Daily GreenFeed Data via API
#' @title Download Daily 'GreenFeed' Data via 'API'
#'
#' @description Downloads daily GreenFeed data from the C-Lock Inc. server
#' via an application programming interface (API). Retrieves data
#' based on specified parameters such as login information, date range and units,
#' and provides an Excel file with the GreenFeed data.
#' @description Downloads daily 'GreenFeed' data from the 'C-Lock Inc.' server via an 'API'.
#' Retrieves data based on specified parameters (login, date range and units), and
#' provides a CSV file with the 'GreenFeed' daily data.
#'
#' @param user a character string representing the user name to logging into GreenFeed system
#' @param pass a character string representing password to logging into GreenFeed system
#' @param user a character string representing the user name to logging into 'GreenFeed' system
#' @param pass a character string representing password to logging into 'GreenFeed' system
#' @param exp a character string representing study name or other study identifier. It is used as file name to save the data
#' @param unit numeric or character vector, or a list representing one or more GreenFeed unit numbers
#' @param unit numeric or character vector, or a list representing one or more 'GreenFeed' unit numbers
#' @param start_date a character string representing the start date of the study (format: "mm/dd/yyyy")
#' @param end_date a character string representing the end date of the study (format: "mm/dd/yyyy")
#' @param save_dir a character string representing the directory to save the output file
#'
#' @return A CSV file with daily GreenFeed data in the specified directory
#' @return A CSV file with daily 'GreenFeed' data in the specified directory
#'
#' @examplesIf has_credentials()
#' # Please replace "your_username" and "your_password" with your actual GreenFeed credentials.
#' # Please replace "your_username" and "your_password" with your actual 'GreenFeed' credentials.
#' user <- Sys.getenv("API_USER")
#' pass <- Sys.getenv("API_PASS")
#' exp <- "StudyName"
Expand All @@ -36,7 +35,7 @@
#' @import stringr

get_gfdata <- function(user, pass, exp = NA, unit,
start_date, end_date = Sys.Date(), save_dir = getwd()) {
start_date, end_date = Sys.Date(), save_dir = tempdir()) {
# Ensure unit is a comma-separated string
unit <- convert_unit(unit)

Expand All @@ -54,7 +53,7 @@ get_gfdata <- function(user, pass, exp = NA, unit,
"https://portal.c-lockinc.com/api/getemissions?d=visits&fids=", unit,
"&st=", start_date, "&et=", end_date, "%2012:00:00"
)
print(URL)
message(URL)

req <- httr::POST(URL, body = list(token = TOK))
httr::stop_for_status(req)
Expand Down
18 changes: 9 additions & 9 deletions R/pellin.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#' @name pellin
#' @title Process GreenFeed Pellet Intakes
#' @title Process 'GreenFeed' Pellet Intakes
#'
#' @description Processes the feedtimes file from GreenFeed system,
#' @description Processes the "feedtimes" file from 'GreenFeed' system,
#' including visits and food drops across a specific period, and it is used
#' to calculate pellet intakes per animal from all units.
#' Aggregates data to provide insights into the feeding behavior
#' and pellet consumption of the animals during a study.
#'
#' @param file_path a character string or list representing files(s) with feedtimes from C-Lock
#' @param unit numeric or character vector or list representing one or more GreenFeed unit numbers. The order should match with feedtimes files
#' @param file_path a character string or list representing files(s) with "feedtimes" from 'C-Lock Inc.'
#' @param unit numeric or character vector or list representing one or more 'GreenFeed' unit numbers. The order should match with "feedtimes" files
#' @param gcup a numeric value representing the grams of pellets per cup.
#' @param start_date a character string representing the start date of the study (format: "mm/dd/yyyy")
#' @param end_date a character string representing the end date of the study (format: "mm/dd/yyyy")
#' @param save_dir a character string representing the directory to save the output file
#' @param rfid_file a character string representing the file with individual IDs. The order should be AnimalName (col1) and RFID (col2)
#' @param rfid_file a character string representing the file with individual IDs. The order should be Visual ID (col1) and RFID (col2)
#'
#' @return An Excel file with pellet intakes for all animals and days within the specified period is saved to `save_dir`.
#' @return An Excel file with pellet intakes for all animals and days within the specified period is saved to save_dir.
#' The file is named "Pellet_Intakes_YYYY-MM-DD_YYYY-MM-DD.csv".
#'
#' @examples
Expand All @@ -25,7 +25,7 @@
#'
#' # You must include the grams of pellets per cup based on the result obtained from the 10-drops test
#'
#' # If the user include an rfid file, the structure should be in col1 AnimalName or VisualID, and
#' # If the user include an rfid file, the structure should be in col1 AnimalName or Visual ID, and
#' # col2 the RFID or TAG_ID. The file could be save in different formats (.xlsx, .csv, or .txt).
#' RFIDs <- system.file("extdata", "RFID_file.csv", package = "greenfeedr")
#'
Expand Down Expand Up @@ -56,7 +56,7 @@ utils::globalVariables(c(
))

pellin <- function(file_path, unit, gcup, start_date, end_date,
save_dir = getwd(), rfid_file = NULL) {
save_dir = tempdir(), rfid_file = NULL) {
message("Please set the 'gcup' parameter based on the 10-drops test.
If units have different gram values, define 'gcup' as a vector with an element for each unit.")

Expand Down Expand Up @@ -88,7 +88,7 @@ pellin <- function(file_path, unit, gcup, start_date, end_date,



# If rfid_file provided, filter and get animal ID not visiting the GreenFeed units
# If rfid_file provided, filter and get animal ID not visiting the 'GreenFeed' units
if (!is.null(rfid_file) && is.data.frame(rfid_file) && nrow(rfid_file) > 0) {
df <- df[df$CowTag %in% rfid_file$RFID, ]
noGFvisits <- rfid_file$FarmName[!(rfid_file$RFID %in% df$CowTag)]
Expand Down
32 changes: 16 additions & 16 deletions R/process_gfdata.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#' @name process_gfdata
#' @title Process Daily and Final GreenFeed Data
#' @title Process Daily and Final 'GreenFeed' Data
#'
#' @description Processes and calculates daily and weekly averages of GreenFeed data.
#' Handles data filtering, aggregation, and summarization to facilitate
#' further analysis and reporting.
#' @description Processes and calculates daily and weekly averages of 'GreenFeed' data.
#' Handles data filtering, aggregation, and summarization to facilitate further analysis.
#'
#' @param data a data frame with daily or finalized GreenFeed data
#' @param data a data frame with daily or finalized 'GreenFeed' data
#' @param start_date a character string representing the start date of the study (format: "mm/dd/yyyy")
#' @param end_date a character string representing the end date of the study (format: "mm/dd/yyyy")
#' @param param1 an integer representing the number of records per day to be consider for analysis
#' @param param2 an integer representing the number of days with records per week to be consider for analysis
#' @param min_time an integer representing the minimum number of minutes for a records to be consider for analysis. By default min_time is 2
#'
#' @return A list of two data frames:
#' \item{daily_data }{data frame with daily processed GreenFeed data}
#' \item{weekly_data }{data frame with weekly processed GreenFeed data}
#' \item{daily_data }{data frame with daily processed 'GreenFeed' data}
#' \item{weekly_data }{data frame with weekly processed 'GreenFeed' data}
#'
#' @examples
#' file <- system.file("extdata", "StudyName_GFdata.csv", package = "greenfeedr")
Expand All @@ -25,7 +24,8 @@
#' start_date = "2024-05-13",
#' end_date = "2024-05-25",
#' param1 = 2,
#' param2 = 3
#' param2 = 3,
#' min_time = 2
#' )
#' head(gf_data)
#'
Expand Down Expand Up @@ -229,17 +229,17 @@ process_gfdata <- function(data, start_date, end_date,


# Description of mean, sd, and CV for weekly gases
print(paste0("CH4: ", round(mean(weekly_df$CH4GramsPerDay, na.rm = TRUE), 2), " +- ", round(stats::sd(weekly_df$CH4GramsPerDay, na.rm = TRUE), 2)))
print(paste0("CH4 CV = ", round(stats::sd(weekly_df$CH4GramsPerDay, na.rm = TRUE) / mean(weekly_df$CH4GramsPerDay, na.rm = TRUE) * 100, 1), "%"))
message(paste0("CH4: ", round(mean(weekly_df$CH4GramsPerDay, na.rm = TRUE), 2), " +- ", round(stats::sd(weekly_df$CH4GramsPerDay, na.rm = TRUE), 2)))
message(paste0("CH4 CV = ", round(stats::sd(weekly_df$CH4GramsPerDay, na.rm = TRUE) / mean(weekly_df$CH4GramsPerDay, na.rm = TRUE) * 100, 1), "%"))

print(paste0("CO2: ", round(mean(weekly_df$CO2GramsPerDay, na.rm = TRUE), 2), " +- ", round(stats::sd(weekly_df$CO2GramsPerDay, na.rm = TRUE), 2)))
print(paste0("CO2 CV = ", round(stats::sd(weekly_df$CO2GramsPerDay, na.rm = TRUE) / mean(weekly_df$CO2GramsPerDay, na.rm = TRUE) * 100, 1), "%"))
message(paste0("CO2: ", round(mean(weekly_df$CO2GramsPerDay, na.rm = TRUE), 2), " +- ", round(stats::sd(weekly_df$CO2GramsPerDay, na.rm = TRUE), 2)))
message(paste0("CO2 CV = ", round(stats::sd(weekly_df$CO2GramsPerDay, na.rm = TRUE) / mean(weekly_df$CO2GramsPerDay, na.rm = TRUE) * 100, 1), "%"))

print(paste0("O2: ", round(mean(weekly_df$O2GramsPerDay, na.rm = TRUE), 2), " +- ", round(stats::sd(weekly_df$O2GramsPerDay, na.rm = TRUE), 2)))
print(paste0("O2 CV = ", round(stats::sd(weekly_df$O2GramsPerDay, na.rm = TRUE) / mean(weekly_df$O2GramsPerDay, na.rm = TRUE) * 100, 1), "%"))
message(paste0("O2: ", round(mean(weekly_df$O2GramsPerDay, na.rm = TRUE), 2), " +- ", round(stats::sd(weekly_df$O2GramsPerDay, na.rm = TRUE), 2)))
message(paste0("O2 CV = ", round(stats::sd(weekly_df$O2GramsPerDay, na.rm = TRUE) / mean(weekly_df$O2GramsPerDay, na.rm = TRUE) * 100, 1), "%"))

print(paste0("H2: ", round(mean(weekly_df$H2GramsPerDay, na.rm = TRUE), 2), " +- ", round(stats::sd(weekly_df$H2GramsPerDay, na.rm = TRUE), 2)))
print(paste0("H2 CV = ", round(stats::sd(weekly_df$H2GramsPerDay, na.rm = TRUE) / mean(weekly_df$H2GramsPerDay, na.rm = TRUE) * 100, 1), "%"))
message(paste0("H2: ", round(mean(weekly_df$H2GramsPerDay, na.rm = TRUE), 2), " +- ", round(stats::sd(weekly_df$H2GramsPerDay, na.rm = TRUE), 2)))
message(paste0("H2 CV = ", round(stats::sd(weekly_df$H2GramsPerDay, na.rm = TRUE) / mean(weekly_df$H2GramsPerDay, na.rm = TRUE) * 100, 1), "%"))


# Return a list of data frames
Expand Down
42 changes: 21 additions & 21 deletions R/report_gfdata.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#' @name report_gfdata
#' @title Download and Report GreenFeed Data
#' @title Download and Report 'GreenFeed' Data
#'
#' @description Generates PDF reports of daily and final GreenFeed data.
#' If option "daily" is used, data is retrieved from C-Lock server via an application programming interface (API)
#' and generates a PDF report to with number of animals, records, and gas production of the ongoing study.
#' But, if option "final" is used, final data should be provided to generates a PDF report
#' to evaluate all GreenFeed data obtained from the finalized study.
#' @description Generates PDF reports of daily and final 'GreenFeed' data.
#' If the option daily is used, data is retrieved from 'C-Lock Inc.' server via an 'API' and
#' generates a PDF report to with number of animals, records, and gases production.
#' However, if the option final is used, the finalized data should be provided to generates a PDF report
#' to evaluate all 'GreenFeed' data obtained from the finalized study.
#'
#' @param input_type a character string representing type of data (options: "daily" or "final")
#' @param input_type a character string representing type of data (options: "daily" and "final")
#' @param exp a character string representing study name or other study identifier. It is used as file name to save the data
#' @param unit numeric or character vector, or a list representing one or more GreenFeed unit numbers
#' @param unit numeric or character vector, or a list representing one or more 'GreenFeed' unit numbers
#' @param start_date a character string representing the start date of the study (format: "mm/dd/yyyy")
#' @param end_date a character string representing the end date of the study (format: "mm/dd/yyyy")
#' @param save_dir a character string representing the directory to save the output file
#' @param plot_opt a character string representing the gas(es) to plot (options: 'All', 'CH4', 'CO2', 'O2', 'H2')
#' @param rfid_file a character string representing the file with individual IDs. The order should be AnimalName (col1) and RFID (col2)
#' @param user a character string representing the user name to logging into GreenFeed system. If `input_type` is "final", this parameter is ignored
#' @param pass a character string representing password to logging into GreenFeed system. If `input_type` is "final", this parameter is ignored
#' @param file_path A list of file paths containing the final report(s) from the GreenFeed system. If `input_type` is "final", this parameter is ignored
#' @param plot_opt a character string representing the gas(es) to plot (options: "All", "CH4", "CO2", "O2", "H2")
#' @param rfid_file a character string representing the file with individual IDs. The order should be Visual ID (col1) and RFID (col2)
#' @param user a character string representing the user name to logging into 'GreenFeed' system. If input_type is "final", this parameter is ignored
#' @param pass a character string representing password to logging into 'GreenFeed' system. If input_type is "final", this parameter is ignored
#' @param file_path A list of file paths containing the final report(s) from the 'GreenFeed' system. If input_type is "final", this parameter is ignored
#'
#' @return A CSV file with daily GreenFeed data and a PDF report with a description of the daily or final records
#' @return A CSV file with daily 'GreenFeed' data and a PDF report with a description of the daily or final records
#'
#' @examplesIf has_credentials()
#' # Please replace "your_username" and "your_password" with your actual GreenFeed credentials.
#' # Please replace "your_username" and "your_password" with your actual 'GreenFeed' credentials.
#' user <- Sys.getenv("API_USER")
#' pass <- Sys.getenv("API_PASS")
#'
Expand Down Expand Up @@ -54,7 +54,7 @@
utils::globalVariables(c("GoodDataDuration", "StartTime", "AirflowLitersPerSec", "Gas_Data"))

report_gfdata <- function(input_type, exp = NA, unit, start_date, end_date = Sys.Date(),
save_dir = getwd(), plot_opt = "CH4", rfid_file = NULL,
save_dir = tempdir(), plot_opt = "CH4", rfid_file = NULL,
user = NA, pass = NA, file_path) {
# Ensure unit is a comma-separated string
unit <- convert_unit(unit)
Expand Down Expand Up @@ -84,12 +84,12 @@ report_gfdata <- function(input_type, exp = NA, unit, start_date, end_date = Sys
"https://portal.c-lockinc.com/api/getemissions?d=visits&fids=", unit,
"&st=", start_date, "&et=", end_date, "%2012:00:00"
)
print(URL)
message(URL)

req <- httr::POST(URL, body = list(token = TOK))
httr::stop_for_status(req)
a <- httr::content(req, as = "text")
print(a)
message(a)

# Split the lines
perline <- stringr::str_split(a, "\\n")[[1]]
Expand Down Expand Up @@ -126,7 +126,7 @@ report_gfdata <- function(input_type, exp = NA, unit, start_date, end_date = Sys
dir.create(save_dir, recursive = TRUE)
}

# Save GreenFeed data as a csv file in the specified directory
# Save 'GreenFeed' data as a csv file in the specified directory
readr::write_excel_csv(df, file = paste0(save_dir, "/", exp, "_GFdata.csv"))


Expand All @@ -142,7 +142,7 @@ report_gfdata <- function(input_type, exp = NA, unit, start_date, end_date = Sys
}
}

# Dataframe (df) contains daily GreenFeed data
# Dataframe (df) contains daily 'GreenFeed' data
df <- df %>%
## Remove "unknown IDs" and leading zeros from RFID col
dplyr::filter(RFID != "unknown") %>%
Expand Down Expand Up @@ -205,7 +205,7 @@ report_gfdata <- function(input_type, exp = NA, unit, start_date, end_date = Sys
"AirflowCf"
)

# df contains finalized GreenFeed data
# df contains finalized 'GreenFeed' data
df <- df %>%
## Remove "unknown IDs" and leading zeros from RFID col
dplyr::filter(RFID != "unknown") %>%
Expand Down
Loading

0 comments on commit f687686

Please sign in to comment.