For 循环从 SQLite 数据库中读取多个表

For loop to read in multiple tables from SQLite database

我想创建一个 for 循环,从 SQLite 数据库中读取多个 tables。我希望它读取前 300 tables,但理想情况下我想让它从我的数据库中读取 300 个随机 tables 到 R.

对于每个 table 读入,我希望它通过编写的代码,在最后保存图表然后重新开始一个新的 table。如果可能的话,我希望所有 table 都在同一张图表上。我已经为单个 table 编写了代码,但我不确定如何从这里继续。

for (i in 1:300){
# Reads the selected table in database
ind1 <- dbReadTable(mydb, i)

# Formats the SQL data to appropriate R data structure
cols <- c("Mortality", "AnimalID", "Species", "Sex", "CurrentCohort", 
          "BirthYear", "CaptureUnit","CaptureSubunit",
          "CaptureArea", "ProjectName")
ind[cols] <- lapply(ind[cols], factor)  ## as.factor() could also be used
ind$DateAndTime <- as.POSIXct(ind$DateAndTime, tz = "UTC",
                               origin = '1970-01-01')

# Converts the Longitude and Latitude to UTMs
ind <- convert_utm(ind1)

ind_steps <- ind %>% 
  # It's always a good idea to *double check* that your data are sorted
  # properly before using lag() or lead() to get the previous/next value.
  arrange(AnimalID, DateAndTime) %>% 
  # If we group_by() AnimalID, lead() will insert NAs in the proper
  # places when we get to the end of one individual's data and the beginning
  # of the next
  group_by(AnimalID) %>% 
  # Now rename our base columns to reflect that they are the step's start point
  rename(x1 = utm_x, 
         y1 = utm_y, 
         t1 = DateAndTime) %>% 
  # Attach the step's end point
  mutate(x2 = lead(x1),
         y2 = lead(y1),
         t2 = lead(t1)) %>% 
  # Calculate differences in space and time
  mutate(dx = x2 - x1,
         dy = y2 - y1,
         DateAndTime = as.numeric(difftime(t2, t1, units = "hours"))) %>% 
  # Calculate step length
  mutate(sl = sqrt(dx^2 + dy^2)) %>% 
  # Calculate absolute angle
  mutate(abs_angle = (pi/2 - atan2(dy, dx)) %% (2*pi)) %>% 
  # Calculate relative angle
  mutate(rel_diff = (abs_angle - lag(abs_angle)) %% (2*pi),
         rel_angle = ifelse(rel_diff > pi, rel_diff - 2*pi, rel_diff)) %>% 
  # Drop this uneccesary column
  select(-rel_diff) %>% 
  # Drop incomplete final step
  filter(!is.na(x2))

ind_steps <- ind_steps %>% 
  mutate(NSD = (x2 - x1[1])^2 + (y2 - y1[1])^2)

# Plot NSD
ind_steps %>% 
  ggplot(aes(x = t2, y = NSD)) +
  geom_line() +
  theme_bw()
}

如有任何帮助,我们将不胜感激!

如果有 1000 个表,您可以使用 sample 从中随机获取 300 个,创建一个长度为 300 的列表来存储图表,如果您想将它们一起绘制,您可以使用 cowplot::plot_grid.

random_tables <- sample(1000, 300, replace = TRUE)
plot_list <- vector('list', 300)

for (i in seq_along(random_tables)){
  # Reads the selected table in database
  ind1 <- dbReadTable(mydb, random_tables[i])
  
  #...Rest of the code
  #....
  #....
  # Plot NSD
  plot_list[[i]] <- ggplot(ind_steps, aes(x = t2, y = NSD)) + 
                    geom_line() + theme_bw()
}

cowplot::plot_grid(plotlist = plot_list, nrow = 30, ncol = 10)