From 8613a6ccbe3b255c9234a3451ae9897dc4c4629c Mon Sep 17 00:00:00 2001
From: Pleydell David <david.pleydell@inra.fr>
Date: Thu, 5 Mar 2020 11:30:09 +0100
Subject: [PATCH 1/7] Moved some code from Rmd to plan.

---
 src/asf_challenge.Rmd | 68 ++++++++++++-------------------------------
 src/plan.R            | 38 ++++++++++++++++++++++--
 2 files changed, 54 insertions(+), 52 deletions(-)

diff --git a/src/asf_challenge.Rmd b/src/asf_challenge.Rmd
index ea3d190..640b17c 100644
--- a/src/asf_challenge.Rmd
+++ b/src/asf_challenge.Rmd
@@ -48,7 +48,10 @@ loadd(
   data_structure,
   pig_sites,
   outbreaks,
-  animal_movements
+  animal_movements,
+  adminPigSiteMap,
+  proportionAgroCoverMap,
+  proportionForestCoverMap
 )
 
 ```
@@ -97,21 +100,9 @@ supposed to be an island.
 
 ```{r map-admin, fig.cap = cap}
 cap <- "Administrative divisions of the dummy territory and pig sites. Observed cases are highlighted in dark red."
-tm_shape(admin) +
-    tm_graticules(col = "lightgrey") +
-    tm_polygons() +
-    tm_compass(type = "8star", position = c("left", "top")) +
-    tm_scale_bar(breaks = c(0, 100, 200), text.size = 1) +
-    tm_layout(bg.color = "lightblue") +
-    tm_shape(pig_sites) +
-    tm_dots() +
-    tm_shape(outbreaks) +
-    tm_dots(size = .1, col = "darkred") 
-
+adminPigSiteMap
 ```
 
-
-
 ### Land cover
 
 Figure \@ref(fig:land-cover) shows a simplified version of the
@@ -139,38 +130,15 @@ Here land cover proportions of forest and agriculture are preressented on such a
 
 ```{r hex-land-cover-agri, message = FALSE, fig.cap = cap}
 cap <- "Proportional cover of agriculture on hexagonal grid."
-
-tm_shape(admin) +
-    tm_polygons() +
-    tm_shape(hex) +
-    tm_polygons(col = "pAgricultural")
-
+proportionAgroCoverMap
 ```
  
-
 ```{r hex-land-cover-forest, message = FALSE, fig.cap = cap}
 cap <- "Proportional cover of forest on hexagonal grid, with detected cases among pig farms and wild boar."
-
-tm_shape(admin) +
-    tm_polygons() +
-    tm_graticules(col = "lightgrey") +
-    tm_layout(bg.color = "lightblue") +
-    tm_shape(hex) +
-    tm_polygons(col = "pForest", lwd=0.2, border.col="grey", palette=get_brewer_pal("Greens", n = 10, plot=FALSE)) +
-    tm_shape(pig_sites) +
-    tm_dots() +
-    tm_shape(outbreaks[outbreaks$HOST=="wild boar",]) +
-    tm_dots(size = .2, col = "orange") +
-    tm_shape(outbreaks[outbreaks$HOST=="pig herd",]) +
-    tm_dots(size = .2, col = "red") +
-    tm_add_legend(type="symbol", labels=c("pig herd","wild boar"), col=c("red", "orange"), border.col=c("red", "orange"), title="Cases") +
-    tm_scale_bar(breaks = c(0, 50, 100, 150), text.size = 1, position = c("left", "top")) 
-
+proportionForestCoverMap
 ```
  
 
-
-
 #### Coordinate reference systems 
 The coordinate system used for shapefile Island_ADMIN.shp is
 ```{r map-admin-crs}
@@ -186,17 +154,17 @@ the official projection in France. Note, the units are in meters.
 
 ### Animal movements
 
-```{r eval = FALSE}
-## This is useless
-tm_shape(admin) +
-  tm_graticules(col = "lightgrey") +
-  tm_polygons() +
-  tm_layout(bg.color = "lightblue") +
-  tm_shape(pig_sites) +
-  tm_dots() +
-  tm_shape(animal_movements) +
-  tm_lines(alpha = .3)
-```
+<!-- ```{r eval = FALSE} -->
+<!-- ## This is useless -->
+<!-- tm_shape(admin) + -->
+<!--   tm_graticules(col = "lightgrey") + -->
+<!--   tm_polygons() + -->
+<!--   tm_layout(bg.color = "lightblue") + -->
+<!--   tm_shape(pig_sites) + -->
+<!--   tm_dots() + -->
+<!--   tm_shape(animal_movements) + -->
+<!--   tm_lines(alpha = .3) -->
+<!-- ``` -->
 
 
 \clearpage
diff --git a/src/plan.R b/src/plan.R
index 10cbf68..ebadbae 100644
--- a/src/plan.R
+++ b/src/plan.R
@@ -62,8 +62,42 @@ plan <- drake_plan(
   data_structure = plot_data_structure(fname = here("src/data_structure.yml")),
 
   ## Distance matrix for pigs
-  distMatPigs = st_distance(pig_sites), 
-
+  distMatPigs = st_distance(pig_sites),
+
+  ## Map of administrative areas with pigsites and first cases
+  adminPigSiteMap = tm_shape(admin) +
+    tm_graticules(col = "lightgrey") +
+    tm_polygons() +
+    tm_compass(type = "8star", position = c("left", "top")) +
+    tm_scale_bar(breaks = c(0, 100, 200), text.size = 1) +
+    tm_layout(bg.color = "lightblue") +
+    tm_shape(pig_sites) +
+    tm_dots() +
+    tm_shape(outbreaks) +
+    tm_dots(size = .1, col = "darkred"), 
+
+  ## Map of proportional agriculture cover
+  proportionAgroCoverMap = tm_shape(admin) +
+    tm_polygons() +
+    tm_shape(hex) +
+    tm_polygons(col = "pAgricultural"), 
+  
+  ## Map of proportional forest cover 
+  proportionForestCoverMap = tm_shape(admin) +
+    tm_polygons() +
+    tm_graticules(col = "lightgrey") +
+    tm_layout(bg.color = "lightblue") +
+    tm_shape(hex) +
+    tm_polygons(col = "pForest", lwd=0.2, border.col="grey", palette=get_brewer_pal("Greens", n = 10, plot=FALSE)) +
+    tm_shape(pig_sites) +
+    tm_dots() +
+    tm_shape(outbreaks[outbreaks$HOST=="wild boar",]) +
+    tm_dots(size = .2, col = "orange") +
+    tm_shape(outbreaks[outbreaks$HOST=="pig herd",]) +
+    tm_dots(size = .2, col = "red") +
+    tm_add_legend(type="symbol", labels=c("pig herd","wild boar"), col=c("red", "orange"), border.col=c("red", "orange"), title="Cases") +
+    tm_scale_bar(breaks = c(0, 50, 100, 150), text.size = 1, position = c("left", "top")), 
+  
   #### REPORTS ####
   report = rmarkdown::render(
     knitr_in(here("src/asf_challenge.Rmd")),
-- 
GitLab


From 4f302c3f0cafb381532b8f34b31eb74351767833 Mon Sep 17 00:00:00 2001
From: Pleydell David <david.pleydell@inra.fr>
Date: Thu, 5 Mar 2020 11:39:10 +0100
Subject: [PATCH 2/7] movementAnimation re-added to plan (with 'file_out's
 removed).

---
 src/plan.R | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/src/plan.R b/src/plan.R
index ebadbae..82dc329 100644
--- a/src/plan.R
+++ b/src/plan.R
@@ -53,9 +53,13 @@ plan <- drake_plan(
       ## (NE sites, see doc (?))
       ## 18794 lines removed. Produced invalid lines.
       filter(source != dest) %>%
+      ## Add a column to provide title when using tmap_animation
+      mutate(day4display=paste("Day", stringr::str_pad(date, nchar(max(date)), pad="0"))) %>%
       ## Put source and dest as the first two cols.
       ## Necessary to match with ID in pig_sites
-      select(source, dest, everything()),
+      select(source, dest, everything()) %>%
+ ## FOR RAPID COMPILATION WHILE DEBUGGING - REMOVE WHEN DEBUGGED 
+ filter(date < 10),
     zones = pig_sites),
 
   #### PRE-COMPUTE COMPLICATED FIGURES ####
@@ -64,6 +68,25 @@ plan <- drake_plan(
   ## Distance matrix for pigs
   distMatPigs = st_distance(pig_sites),
 
+  ## Some constants
+  nPigSites = nrow(pig_sites),
+
+  ## Animation
+  movementFileName = (here("animations/animal_moves_by_day.gif")), ## file_out
+
+  movementAnimation = tmap_animation(tm_shape(admin) +
+                                     tm_graticules(col = "lightgrey") +
+                                     tm_polygons() +
+                                     tm_shape(pig_sites) +
+                                     tm_dots(alpha = .3) + # tm_shape(littleAM) + ##
+                                     tm_shape(animal_movements) +
+                                     tm_lines(alpha=0.5) +  
+                                     tm_facets(along = "day4display", free.coords=FALSE) +
+                                     tm_layout(bg.color = "lightblue"), 
+                                     filename=(movementFileName), ## file_out
+                                     width=2000, delay=100, restart.delay = 3000), # loop=FALSE,
+
+
   ## Map of administrative areas with pigsites and first cases
   adminPigSiteMap = tm_shape(admin) +
     tm_graticules(col = "lightgrey") +
-- 
GitLab


From 48b6d56ba77d02a130ec29fd83fa8b94afc10819 Mon Sep 17 00:00:00 2001
From: Pleydell David <david.pleydell@inra.fr>
Date: Thu, 5 Mar 2020 12:01:20 +0100
Subject: [PATCH 3/7] Animation file path hardwired into Rmd file. Compiles.

---
 src/asf_challenge.Rmd | 3 +++
 src/plan.R            | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/asf_challenge.Rmd b/src/asf_challenge.Rmd
index 640b17c..332b63d 100644
--- a/src/asf_challenge.Rmd
+++ b/src/asf_challenge.Rmd
@@ -166,6 +166,9 @@ the official projection in France. Note, the units are in meters.
 <!--   tm_lines(alpha = .3) -->
 <!-- ``` -->
 
+![Alt Text](/home/pleydell/asf-challenge/animations/animal_moves_by_day.gif){scale=30%}
+
+
 
 \clearpage
 
diff --git a/src/plan.R b/src/plan.R
index 82dc329..2440bd5 100644
--- a/src/plan.R
+++ b/src/plan.R
@@ -84,7 +84,7 @@ plan <- drake_plan(
                                      tm_facets(along = "day4display", free.coords=FALSE) +
                                      tm_layout(bg.color = "lightblue"), 
                                      filename=(movementFileName), ## file_out
-                                     width=2000, delay=100, restart.delay = 3000), # loop=FALSE,
+                                     width=2000, delay=100, restart.delay = 300), # loop=FALSE,
 
 
   ## Map of administrative areas with pigsites and first cases
-- 
GitLab


From e4717ba0fec1da9c7e60850f390ce274f5a8b9d2 Mon Sep 17 00:00:00 2001
From: Pleydell David <david.pleydell@inra.fr>
Date: Thu, 5 Mar 2020 12:39:15 +0100
Subject: [PATCH 4/7] Path taken from plan (movementFileName) used to display
 animation. But no figure number.

---
 src/asf_challenge.Rmd | 18 ++++--------------
 src/plan.R            |  2 +-
 2 files changed, 5 insertions(+), 15 deletions(-)

diff --git a/src/asf_challenge.Rmd b/src/asf_challenge.Rmd
index 332b63d..1766948 100644
--- a/src/asf_challenge.Rmd
+++ b/src/asf_challenge.Rmd
@@ -51,7 +51,8 @@ loadd(
   animal_movements,
   adminPigSiteMap,
   proportionAgroCoverMap,
-  proportionForestCoverMap
+  proportionForestCoverMap,
+  movementFileName
 )
 
 ```
@@ -154,19 +155,8 @@ the official projection in France. Note, the units are in meters.
 
 ### Animal movements
 
-<!-- ```{r eval = FALSE} -->
-<!-- ## This is useless -->
-<!-- tm_shape(admin) + -->
-<!--   tm_graticules(col = "lightgrey") + -->
-<!--   tm_polygons() + -->
-<!--   tm_layout(bg.color = "lightblue") + -->
-<!--   tm_shape(pig_sites) + -->
-<!--   tm_dots() + -->
-<!--   tm_shape(animal_movements) + -->
-<!--   tm_lines(alpha = .3) -->
-<!-- ``` -->
-
-![Alt Text](/home/pleydell/asf-challenge/animations/animal_moves_by_day.gif){scale=30%}
+![Figure x: day-by-day animation of animal movements](`r movementFileName`){ scale=10% }
+
 
 
 
diff --git a/src/plan.R b/src/plan.R
index 2440bd5..5ebd053 100644
--- a/src/plan.R
+++ b/src/plan.R
@@ -72,7 +72,7 @@ plan <- drake_plan(
   nPigSites = nrow(pig_sites),
 
   ## Animation
-  movementFileName = (here("animations/animal_moves_by_day.gif")), ## file_out
+  movementFileName = file_out(here("animations/animal_moves_by_day.gif")), 
 
   movementAnimation = tmap_animation(tm_shape(admin) +
                                      tm_graticules(col = "lightgrey") +
-- 
GitLab


From 454835c87e9d2732e4fe5601ed2d3e3b8780572d Mon Sep 17 00:00:00 2001
From: Pleydell David <david.pleydell@inra.fr>
Date: Thu, 5 Mar 2020 12:56:25 +0100
Subject: [PATCH 5/7] Bug fix (scale -> width) for displaying animation.

---
 src/asf_challenge.Rmd | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/asf_challenge.Rmd b/src/asf_challenge.Rmd
index 1766948..0dffe9d 100644
--- a/src/asf_challenge.Rmd
+++ b/src/asf_challenge.Rmd
@@ -52,7 +52,8 @@ loadd(
   adminPigSiteMap,
   proportionAgroCoverMap,
   proportionForestCoverMap,
-  movementFileName
+  movementFileName,
+  movementAnimation
 )
 
 ```
@@ -155,7 +156,7 @@ the official projection in France. Note, the units are in meters.
 
 ### Animal movements
 
-![Figure x: day-by-day animation of animal movements](`r movementFileName`){ scale=10% }
+![Figure x: day-by-day animation of animal movements](`r movementFileName`){ width=60% }
 
 
 
-- 
GitLab


From a034401211f53924c8939e6a0a992293c76c885b Mon Sep 17 00:00:00 2001
From: Pleydell David <david.pleydell@inra.fr>
Date: Thu, 5 Mar 2020 13:02:04 +0100
Subject: [PATCH 6/7] Returned animal_movements to full size (filter on date
 commented).

---
 src/plan.R | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/plan.R b/src/plan.R
index 5ebd053..59d5767 100644
--- a/src/plan.R
+++ b/src/plan.R
@@ -57,9 +57,10 @@ plan <- drake_plan(
       mutate(day4display=paste("Day", stringr::str_pad(date, nchar(max(date)), pad="0"))) %>%
       ## Put source and dest as the first two cols.
       ## Necessary to match with ID in pig_sites
-      select(source, dest, everything()) %>%
+      select(source, dest, everything()), 
+ ## %>%
  ## FOR RAPID COMPILATION WHILE DEBUGGING - REMOVE WHEN DEBUGGED 
- filter(date < 10),
+ ## filter(date < 10),
     zones = pig_sites),
 
   #### PRE-COMPUTE COMPLICATED FIGURES ####
-- 
GitLab


From 037dde5f37bd4f1286aa3864c7afcf3ba5847209 Mon Sep 17 00:00:00 2001
From: Pleydell David <david.pleydell@inra.fr>
Date: Thu, 5 Mar 2020 13:07:22 +0100
Subject: [PATCH 7/7] Update in .gitignore.

---
 .gitignore | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.gitignore b/.gitignore
index ecf3edb..9c15171 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 .drake
 .Rhistory
 dave's_scratchpad.R
+plan_execute.R
-- 
GitLab