mfdb_import_* to accept tibbles
Closed this issue · 3 comments
I've stumble upon an error when trying to import tibbles (tidyverse's spin on data.frames) to mfdb.
Error: Must use a vector in `[`, not an object of class matrix.
At the moment the only work around seems to cast the tibble to a data.frame prior to import. I assume this has something to do with the R postgres package but I would find it cleaner that the mfdb_import_* functions would allow for tibbles in the import.
Not necessarily, tibbles wrap data.frames then break backwards-compatibility, so there's likely to be sharp edges when using them with code not expecting them.
I had a quick go and did the commit above, in branch tibble-support
, which fixes an obvious problem when you feed tibbles into mfdb_import_survey()
. I'm not convinced it's related to the error above, but most of what mfdb_import_data.R does is on individual columns, not the table object as a whole.
If it isn't, what function are you feeding a tibble into? Could be I'm looking in the wrong place.
Here is an example of what I was doing (sorry for the lengthy example), the last bit doesn't work unless the data is stored as a data.frame
library(icesDatras)
library(mfdb)
library(tidyices)
library(tidyverse)
mdb <- mfdb('datras')
yrs <- 2017 # years
qs <- c(1) # quarters
hh <-
icesDatras::getDATRAS(record = "HH",
survey = "NS-IBTS",
years = yrs,
quarters = qs) %>%
dtrs_tidy_hh()
hl <-
icesDatras::getDATRAS(record = "HL",
survey = "NS-IBTS",
years = yrs,
quarters = qs) %>%
dtrs_tidy_hl(hh)
ca <-
icesDatras::getDATRAS(record = "CA",
survey = "NS-IBTS",
years = yrs,
quarters = qs) %>%
dtrs_tidy_ca()
## Setup the spatial taxonomy
hh <- hh %>%
mutate(areacell = geo::d2sr(shootlat,shootlong))
hh %>%
select(areacell) %>%
distinct() %>%
mutate(id = 1:n(),
lat = geo::sr2d(areacell)$lat,
lon = geo::sr2d(areacell)$lon,
area = geo::srA(areacell)) %>%
select(id, name=areacell,size = area) %>%
mfdb_import_area(mdb,.)
## Setup sampling types
mfdb_import_sampling_type(mdb, data.frame(
id = 1:3,
name = c('NS-IBTS','SEA','LND'),
description = c('North Sea IBTS','Catch samples','Landings')))
## upload tow taxonomy (optional)
# select(synis_id,ar,man,lat=kastad_n_breidd,lon=kastad_v_lengd,lat1=hift_n_breidd,lon1=hift_v_lengd,
# gear,sampling_type,depth=dypi_kastad,vessel,reitur,smareitur)
hh %>%
select(sampling_type = survey,
name = id,
year,
month = quarter,
latitude = shootlat,
longitude = shootlong,
towlength = hauldur,
depth,
vessel = ship) %>%
mfdb_import_tow_taxonomy(mdb,.)
## setup vessel taxonomy
hh %>%
select(name = ship) %>%
distinct() %>%
mutate(length=NA,tonnage=NA,power=NA, full_name = name) %>%
mfdb_import_vessel_taxonomy(mdb,.)
## import length data
hh %>%
left_join(hl) %>%
select(sampling_type = survey,
areacell,
tow = id,
year,
month = quarter,
latitude = shootlat,
longitude = shootlong,
towlength = hauldur,
depth,
vessel = ship,
latin,
length,
sex,
count = n)%>%
mutate(latin = tolower(latin)) %>%
left_join(mfdb::species %>%
mutate(latin = gsub('.+\\((.+)\\)','\\1',description),
latin = tolower(latin)) %>%
rename(species = name)) %>%
mutate(species = as.character(species)) %>%
select(-c(id,latin,description)) %>%
as.data.frame() %>% ## here I need to cast the tibble to data.frame
mfdb_import_survey(mdb,.,data_source = 'DATRAS-lengths')
No worries. I'm not quite sure where the matrix comes into it, but the above commit does fix this example, I'll merge into 6.x