I have a data frame with three columns (Category, Sub.category and Acitivty). I need a nested list, with these three levels to put into shinyTree.
I'm trying to match the format of this .Rds file to create a shinyTree.
My full table is 99 lines, I've included 30 below, but may need to do over 100+ in the same way.
So far I've got
activity.list <- dlply(activities, .(Category), dlply, .(Sub.category), dlply, .(as.character(Activity)))
Which almost works, but the 3rd level in the nested list is still a dataframe. How do I keep only the "Activity" column as a character? (matching the structure in the texasCities list)
Dataframe:
activities <- tibble::tribble(
~Category, ~Sub.category, ~Activity,
"Recreational fishing", "Boat", "Cray pot",
"Recreational fishing", "Boat", "Hand/rod & line",
"Recreational fishing", "Boat", "Cray loop",
"Recreational fishing", "Boat", "Drop net",
"Recreational fishing", "Boat", "Spear",
"Recreational fishing", "Boat", "Other",
"Recreational fishing", "Jetty", "Hand/rod & line",
"Recreational fishing", "Jetty", "Cray loop",
"Recreational fishing", "Jetty", "Drop net",
"Recreational fishing", "Jetty", "Other",
"Recreational fishing", "Beach fishing", "Hand/rod & line",
"Recreational fishing", "Beach fishing", "Cray loop",
"Recreational fishing", "Beach fishing", "Drop net",
"Recreational fishing", "Beach fishing", "Bait pump",
"Recreational fishing", "Beach fishing", "Spear",
"Recreational fishing", "Beach fishing", "Other",
"Recreational fishing", "Rock fishing", "Hand/rod & line",
"Recreational fishing", "Rock fishing", "Cray loop",
"Recreational fishing", "Rock fishing", "Drop net",
"Recreational fishing", "Rock fishing", "Spear",
"Recreational fishing", "Rock fishing", "Other",
"Recreational fishing", "Other e.g. Kayak/Jetski", "Hand/rod & line",
"Recreational fishing", "Other e.g. Kayak/Jetski", "Cray loop",
"Recreational fishing", "Other e.g. Kayak/Jetski", "Spear",
"Recreational fishing", "Other e.g. Kayak/Jetski", "Drop net",
"Recreational fishing", "Other e.g. Kayak/Jetski", "Other",
"Recreational - other", "Motorised water sports", "Jet Skiing",
"Recreational - other", "Motorised water sports", "Boating",
"Recreational - other", "Motorised water sports", "Water skiing/Knee boarding/Wake boarding",
"Recreational - other", "Motorised water sports", "Other",
"Recreational - other", "Non-motorised water sports", "Sailing",
"Recreational - other", "Non-motorised water sports", "Surfing",
"Recreational - other", "Non-motorised water sports", "Paddle board",
"Recreational - other", "Non-motorised water sports", "Kayak/canoe/Surf ski/Rowing",
"Recreational - other", "Diving/Snorkelling", "Boat based",
"Recreational - other", "Diving/Snorkelling", "Shore based",
"Recreational - other", "Off road vehicle", "4WD",
"Recreational - other", "Off road vehicle", "Quad bike/ATV",
"Recreational - other", "Off road vehicle", "Dirt bike",
"Recreational - other", "Off road vehicle", "Land yacht",
"Recreational - other", "Off road vehicle", "Other",
"Recreational - other", "Shore based recreation", "Horse riding",
"Recreational - other", "Shore based recreation", "Walking/running",
"Recreational - other", "Shore based recreation", "Beach combing",
"Recreational - other", "Shore based recreation", "Dog Walking",
"Recreational - other", "Shore based recreation", "Photography",
"Recreational - other", "Shore based recreation", "Other",
"Recreational - other", "Other", "Whale watching",
"Recreational - other", "Other", "Bird watching",
"Recreational - other", "Other", "Seal / sea lion watching",
"Recreational - other", "Other", "Other",
"Commercial fishing", "Roes/Greenlip/Brownlip Abalone Fishery", "Hand collection (dive)",
"Commercial fishing", "South Coast Crustacean Managed Fishery (do we need to include the zones?)", "Pot fishery",
"Commercial fishing", "South Coast Estuarine Management Fishery", "Gill Net",
"Commercial fishing", "South Coast Estuarine Management Fishery", "Haul Net",
"Commercial fishing", "South Coast Salmon Managed Fishery", "Beach seine",
"Commercial fishing", "(closed fishery)", NA,
"Commercial fishing", "South Coast Purse Seine Managed Fishery", "Purse seine net",
"Commercial fishing", "Joint Authority Southern Demersal Gillnet and Demersal Longline Fishery", "Demersal gillnets",
"Commercial fishing", "Marine Aquarium Fish Managed Fishery (statewide)", NA,
"Commercial fishing", "Specimen Shell Managed Fishery (statewide)", NA,
"Commercial fishing", "Octopus Interim Managed Fishery", "Trigger trap",
"Commercial fishing", "Octopus Interim Managed Fishery", "Unbaited (shelter) pot",
"Commercial fishing", "Octopus Interim Managed Fishery", "Lobster pot (bycatch)",
"Commercial fishing", "South Coast Trawl Fishery", "Otter trawl",
"Commercial fishing", "South Coast Line and Fish Trap Managed Fishery", "Line",
"Commercial fishing", "South Coast Line and Fish Trap Managed Fishery", "Fish trap",
"Commercial fishing", "South Coast Nearshore Net Managed Fishery", "Net",
"Commercial Tour Operator", "Nature-based/Eco- tourism", "Scenic site",
"Commercial Tour Operator", "Nature-based/Eco- tourism", "Swimming",
"Commercial Tour Operator", "Nature-based/Eco- tourism", "Wildlife viewing",
"Commercial Tour Operator", "Nature-based/Eco- tourism", "Maritime heritage site",
"Commercial Tour Operator", "Nature-based/Eco- tourism", "Other",
"Commercial Tour Operator", "Charter Fishing", "Fishing site",
"Commercial Tour Operator", "Anchorages and landings", "Anchorage",
"Commercial Tour Operator", "Anchorages and landings", "Beach landings",
"Commercial Tour Operator", "Anchorages and landings", "Moorings",
"Commercial Tour Operator", "Anchorages and landings", "Fuel drop-offs",
"Commercial Tour Operator", "Anchorages and landings", "Helicopter landing site",
"Commercial Tour Operator", "Anchorages and landings", "Other",
"Commercial - other", "Film/photography", NA,
"Commercial - other", "Ferry operator", NA,
"Commercial - other", "Harbour/Marina", NA,
"Commercial - other", "Shipping", NA,
"Commercial - other", "Other", NA,
"Environmental", "Education", "School based monitoring",
"Environmental", "Education", "Other",
"Environmental", "Science/Research", "Intertidal monitoring",
"Environmental", "Science/Research", "Subtidal monitoring",
"Environmental", "Science/Research", "Experiment/Tagging",
"Environmental", "Science/Research", "BRUVS site",
"Environmental", "Science/Research", "Marine debris survey",
"Environmental", "Science/Research", "Other",
"Environmental", "Volunteer on ground works", "Community monitoring",
"Environmental", "Volunteer on ground works", "Weed control",
"Environmental", "Volunteer on ground works", "Beach clean up",
"Environmental", "Volunteer on ground works", "Marine debris survey",
"Environmental", "Volunteer on ground works", "Other",
"Environmental", "Other", "Other"
)```
Here is a possible approach using rrapply()
in the rrapply
-package to unmelt
the data.frame to a nested list:
library(rrapply)
lst <- rrapply(activities, how = "unmelt")
str(lst, list.len = 3)
#> List of 6
#> $ Recreational fishing :List of 26
#> ..$ Boat : chr "Cray pot"
#> ..$ Boat : chr "Hand/rod & line"
#> ..$ Boat : chr "Cray loop"
#> .. [list output truncated]
#> $ Recreational - other :List of 25
#> ..$ Motorised water sports : chr "Jet Skiing"
#> ..$ Motorised water sports : chr "Boating"
#> ..$ Motorised water sports : chr "Water skiing/Knee boarding/Wake boarding"
#> .. [list output truncated]
#> $ Commercial fishing :List of 17
#> ..$ Roes/Greenlip/Brownlip Abalone Fishery : chr "Hand collection (dive)"
#> ..$ South Coast Crustacean Managed Fishery (do we need to include the zones?): chr "Pot fishery"
#> ..$ South Coast Estuarine Management Fishery : chr "Gill Net"
#> .. [list output truncated]
#> [list output truncated]
The texasCities
example contains all empty strings (""
) at the leaf nodes, to mimick this we can add an additional (empty-string) column to the initial activities
data.frame:
lst1 <- rrapply(cbind(activities, newcol = ""), how = "unmelt")
str(lst1, list.len = 2)
#> List of 6
#> $ Recreational fishing :List of 5
#> ..$ Boat :List of 6
#> .. ..$ Cray pot : chr ""
#> .. ..$ Hand/rod & line: chr ""
#> .. .. [list output truncated]
#> ..$ Jetty :List of 4
#> .. ..$ Hand/rod & line: chr ""
#> .. ..$ Cray loop : chr ""
#> .. .. [list output truncated]
#> .. [list output truncated]
#> $ Recreational - other :List of 6
#> ..$ Motorised water sports :List of 4
#> .. ..$ Jet Skiing : chr ""
#> .. ..$ Boating : chr ""
#> .. .. [list output truncated]
#> ..$ Non-motorised water sports:List of 4
#> .. ..$ Sailing : chr ""
#> .. ..$ Surfing : chr ""
#> .. .. [list output truncated]
#> .. [list output truncated]
#> [list output truncated]