This function takes a nested JSON object representing plausibility scores (where top-level keys are IDP names and values are lists of Outcome-Score pairs) and converts it into a wide data frame format. In the output, IDPs will be row names, and Outcomes will be column names, with the scores as values.
json_to_wide_df(json_string)
A character string containing the JSON data. The expected structure is a named list (or object) where:
Each top-level key is an Imaging Derived Phenotype (IDP) name (character).
The value for each IDP is another named list (or object) where:
Each key is an Outcome name (character).
The value for each Outcome is the plausibility score (numeric).
A wide data frame where rows represent IDPs and columns represent Outcomes, with plausibility scores as the cell values. The IDP names will be used as row names. Returns an empty data frame if the input JSON is invalid or malformed.
The process involves:
Parsing the JSON string using jsonlite::fromJSON
.
Reshaping the nested list into a tidy (long) data frame with columns 'IDP', 'Outcome', and 'PlausibilityScore'.
Pivoting this tidy data frame into a wide format using
tidyr::pivot_wider
.
Setting the 'IDP' column as row names for the final data frame.
if (FALSE) { # \dontrun{
# Example usage with a sample JSON string
json_data_string <- '{
"CA1": {
"verbal.learning.recall": 0.85,
"recall.delayed": 0.9,
"recall.total": 0.88
},
"Dentate Gyrus/CA3": {
"verbal.learning.recall": 0.8,
"recall.delayed": 0.85,
"recall.total": 0.83
},
"Anterolateral Entorhinal Cortex": {
"verbal.learning.recall": 0.75,
"recall.delayed": 0.8,
"recall.total": 0.78
}
}'
plausibility_df <- json_to_wide_df(json_data_string)
print(plausibility_df)
} # }