-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTutorial.html
More file actions
1559 lines (1415 loc) · 151 KB
/
Tutorial.html
File metadata and controls
1559 lines (1415 loc) · 151 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="author" content="Cory Whitney" />
<meta name="date" content="2025-03-20" />
<meta name="progressive" content="false" />
<meta name="allow-skip" content="false" />
<meta name="learnr-version-prerender" content="0.11.5" />
<title>Decision Modeling for Agroecology</title>
<!-- header-includes START -->
<!-- HEAD_CONTENT -->
<!-- header-includes END -->
<!-- HEAD_CONTENT -->
<!-- highlightjs -->
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<!-- taken from https://github.com/rstudio/rmarkdown/blob/de8a9c38618903627ca509f5401d50a0876079f7/inst/rmd/h/default.html#L293-L343 -->
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- end tabsets -->
</head>
<body>
<a class='sr-only sr-only-focusable visually-hidden-focusable' href='#learnr-tutorial-content'>Skip to Tutorial Content</a>
<div class="pageContent band">
<main class="bandContent page">
<article class="topics" id="learnr-tutorial-content">
<div id="section-introduction" class="section level2">
<h2><strong>Introduction</strong></h2>
<p>Welcome to the interactive tutorial on <strong>Decision Modeling for
Agroecology & Conservation</strong>. This tutorial will guide you
through:</p>
<ul>
<li><strong>Theoretical foundations</strong> of decision modeling</li>
<li><strong>Identifying decision options and outcomes</strong></li>
<li><strong>Structuring causal models</strong> using Directed Acyclic
Graphs (DAGs)</li>
<li><strong>Monte Carlo simulation</strong> for decision-making</li>
<li><strong>Expert-elicited probabilities</strong> and model
validation</li>
<li><strong>Evaluating models</strong> with Pareto-optimality</li>
</ul>
<div class="form-group shiny-input-container">
<label class="control-label" id="intro_question-label" for="intro_question">What do you already know about decision modeling?</label>
<input id="intro_question" type="text" class="shiny-input-text form-control" value="" placeholder="Write a short response..."/>
</div>
<hr />
</div>
<div id="section-decision-modeling" class="section level2">
<h2><strong>1. Decision Modeling</strong></h2>
<p>Decision-making in agroecology often requires navigating trade-offs,
for example between productivity and biodiversity. Decisions are further
complicated by system complexity and uncertainty, i.e. the impacts of
climate variability or market fluctuations. In this section, we will
explore why decision modeling matters for agroecology, how it can guide
sustainable land management, and what tools can help us make informed
decisions.</p>
<div class="panel-heading tutorial-quiz-title"><span data-i18n="text.quiz">Quiz</span></div>
<div class="panel panel-default tutorial-question-container">
<div data-label="question1-1" class="tutorial-question panel-body">
<div id="question1-1-answer_container" class="shiny-html-output"></div>
<div id="question1-1-message_container" class="shiny-html-output"></div>
<div id="question1-1-action_button_container" class="shiny-html-output"></div>
<script>if (Tutorial.triggerMathJax) Tutorial.triggerMathJax()</script>
</div>
</div>
<hr />
</div>
<div id="section-identifying-decision-options-and-outcomes"
class="section level2">
<h2><strong>2. Identifying Decision Options and Outcomes</strong></h2>
<p><strong>Activity:</strong> Identify and list <strong>decision
options</strong> and <strong>outcomes of interest</strong> from the
perspective of decision-makers.</p>
<div class="form-group shiny-input-container">
<label class="control-label" id="decision_options-label" for="decision_options">List the decision options relevant to an agroecological problem:</label>
<input id="decision_options" type="text" class="shiny-input-text form-control" value="" placeholder="Example: Adopting agroforestry, expanding irrigation, switching to organic inputs..."/>
</div>
<div class="form-group shiny-input-container">
<label class="control-label" id="decision_outcomes-label" for="decision_outcomes">List the key outcomes of interest for decision-makers:</label>
<input id="decision_outcomes" type="text" class="shiny-input-text form-control" value="" placeholder="Example: Yield improvement, biodiversity conservation, financial returns..."/>
</div>
<hr />
</div>
<div id="section-structuring-a-causal-decision-model-dags"
class="section level2">
<h2><strong>3. Structuring a Causal Decision Model (DAGs)</strong></h2>
<p><strong>Activity:</strong> Sketch a decision model using the
interactive diagram tool or upload a hand-drawn image.</p>
<div id="section-option-1-interactive-dag-drawing"
class="section level3">
<h3><strong>Option 1: Interactive DAG Drawing</strong></h3>
<p>Share a link of a graph you build in <a
href="https://ncase.me/loopy/">Loopy</a> by <a
href="http://explorableexplanations.com/">Explorable
Explanations</a></p>
</div>
<div id="section-option-2-upload-a-hand-drawn-model"
class="section level3">
<h3><strong>Option 2: Upload a Hand-Drawn Model</strong></h3>
<p>Sketch out a decision model on a piece of paper and share with
us.</p>
<div class="form-group shiny-input-container">
<label class="control-label" id="model_upload-label" for="model_upload">Upload your decision model (optional)</label>
<div class="input-group">
<label class="input-group-btn input-group-prepend">
<span class="btn btn-default btn-file">
Browse...
<input id="model_upload" class="shiny-input-file" name="model_upload" type="file" style="position: absolute !important; top: -99999px !important; left: -99999px !important;" accept="image/png,image/jpeg"/>
</span>
</label>
<input type="text" class="form-control" placeholder="No file selected" readonly="readonly"/>
</div>
<div id="model_upload_progress" class="progress active shiny-file-input-progress">
<div class="progress-bar"></div>
</div>
</div>
<p>Or you can adjust this one if you are familiar with DOT language.</p>
<div class="tutorial-exercise" data-label="decision_model"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code>DiagrammeR::grViz("
digraph decision_model {
# Define node styles
node [shape=box, style=filled, fillcolor=lightblue] Decision;
node [shape=ellipse, style=filled, fillcolor=lightgrey] Apple_Income Sheep_Income Costs;
node [shape=ellipse, style=filled, fillcolor=green] Outcome_Profit;
# Define relationships
Decision -> Apple_Income;
Decision -> Sheep_Income;
Decision -> Costs;
Costs -> Apple_Income;
Costs -> Sheep_Income;
Apple_Income -> Outcome_Profit;
Sheep_Income -> Outcome_Profit;
}
")</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<hr />
</div>
</div>
<div id="section-implementing-a-monte-carlo-model"
class="section level2">
<h2><strong>4. Implementing a Monte Carlo Model</strong></h2>
<p>Now for a very simple <strong>Monte Carlo Simulation</strong> in R.
We will analyze a <strong>decision</strong>. The choice of a farmer to
start agroforestry (apple trees + sheep) or maintain a treeless sheep
system. Use <code>set.seed</code> and change the number of simulations
to 1000.</p>
<div class="tutorial-exercise" data-label="apples_model"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># set.seed(42)
num_simulations <- 100
# Define ranges per ha per year Euro
# lower and upper
apple_income <- runif(n = num_simulations,
min = 3000,
max = 60000)
apple_costs <- runif(n = num_simulations,
min = 15000,
max = 30000)
apple_profits <- apple_income - apple_costs</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="apples_model-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>set.seed(42)
num_simulations <- 1000
# Define ranges per ha per year Euro
# lower and upper
apple_income <- runif(n = num_simulations,
min = 3000,
max = 60000)
apple_costs <- runif(n = num_simulations,
min = 15000,
max = 30000)
apple_profits <- apple_income - apple_costs</code></pre>
</div>
<p>Add sheep to the horticulture system. So we can judge the relative
benefits of transition to a silvopastoral agroforestry system. Add the
sheep and apple profits as a last step in the function.</p>
<div class="tutorial-exercise" data-label="sheep_model"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Euro per ha per year
sheep_income <- runif(n = num_simulations,
min = 2000,
max = 5000)
sheep_costs <- runif(n = num_simulations,
min = 1000,
max = 2500)
sheep_profits <- sheep_income - sheep_costs</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support" data-label="sheep_model-solution"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Euro per ha per year
sheep_income <- runif(n = num_simulations,
min = 2000,
max = 5000)
sheep_costs <- runif(n = num_simulations,
min = 1000,
max = 2500)
sheep_profits <- sheep_income - sheep_costs
total_profits <- apple_profits + sheep_profits</code></pre>
</div>
<p>Plot a histogram of the profits. Change the color of the
apple_profits to grey.</p>
<div class="tutorial-exercise" data-label="plot_profits_overlay"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code>hist(total_profits, col = "white")
hist(apple_profits, add = TRUE, col = "pink")
hist(sheep_profits, add = TRUE, col = "black")</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="plot_profits_overlay-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>hist(total_profits, col = "white")
hist(apple_profits, add = TRUE, col = "grey")
hist(sheep_profits, add = TRUE, col = "black")</code></pre>
</div>
<div class="panel-heading tutorial-quiz-title"><span data-i18n="text.quiz">Quiz</span></div>
<div class="panel panel-default tutorial-question-container">
<div data-label="question2-1" class="tutorial-question panel-body">
<div id="question2-1-answer_container" class="shiny-html-output"></div>
<div id="question2-1-message_container" class="shiny-html-output"></div>
<div id="question2-1-action_button_container" class="shiny-html-output"></div>
<script>if (Tutorial.triggerMathJax) Tutorial.triggerMathJax()</script>
</div>
</div>
<hr />
</div>
<div id="section-pareto-optimality" class="section level2">
<h2><strong>5. Pareto-Optimality</strong></h2>
<p>Now we will calcualte Pareto-Optimality. We will use the concept of
the Pareto front to find the best trade-offs between two competing
objectives. We can learn which solutions are <em>optimal</em> (better in
at least one way, without being worse in another) and which solutions
are <em>dominated</em> (we could improve at least one objective without
making the other worse).</p>
<p>First we need to increase the model complexity a little by adding
management options. We add stocking density (sheep per hectare), tree
density (trees per hectare), proportion of tree area, and proportion of
sheep area (1 - proportion_tree_area). <em>Adjust these numbers to
reflect your best estimate.</em></p>
<div class="tutorial-exercise" data-label="management_variables"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Define management variables per hectare
max_stocking_density <- 20 # Max sheep per ha before overgrazing effects occur
stocking_density <- runif(num_simulations, min = 5, max = max_stocking_density)
max_tree_density <- 500 # Maximum trees per hectare
tree_density <- runif(num_simulations, min = 50, max = max_tree_density)
# Define proportion of tree area vs. sheep area
proportion_tree_area <- runif(num_simulations, min = 0.2, max = 0.8)
proportion_sheep_area <- 1 - proportion_tree_area # Remaining area for sheep</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="management_variables-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code># Define management variables per hectare
max_stocking_density <- 40 # Max sheep per ha before overgrazing effects occur
stocking_density <- runif(num_simulations, min = 5, max = max_stocking_density)
max_tree_density <- 200 # Maximum trees per hectare
tree_density <- runif(num_simulations, min = 50, max = max_tree_density)
# Define proportion of tree area vs. sheep area
proportion_tree_area <- runif(num_simulations, min = 0.2, max = 0.8)
proportion_sheep_area <- 1 - proportion_tree_area # Remaining area for sheep
total_profits_agroforestry <- apple_profits + (sheep_profits * proportion_sheep_area)
total_profits_treeless <- sheep_profits # No tree component in treeless system</code></pre>
</div>
<p>Adjust these biodiversity numbers to reflect your best estimate. For
example, depending on the ecosystem, each tree likely adds far fewer
species and each sheep likely has far less of a negative impact.</p>
<div class="tutorial-exercise" data-label="biodiversity_variables"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Define species richness and grazing impact factors per hectare
species_richness_per_tree <- 0.05 # Each tree adds 0.05 species per ha
species_loss_per_sheep <- 0.02 # Each sheep reduces 0.02 species per ha
# Calculate biodiversity per ha
tree_species_richness <- tree_density * species_richness_per_tree
grazing_species_loss <- stocking_density * species_loss_per_sheep
# Compute biodiversity outcomes per ha
biodiversity_agroforestry <- tree_species_richness - grazing_species_loss
biodiversity_treeless <- -grazing_species_loss # No trees, only grazing effect
# Calculate difference (decision impact of trees)
# measures the financial difference when switching to trees
total_profits <- total_profits_agroforestry - total_profits_treeless
# Calculate difference (decision impact of trees on biodiversity)
# measures the net gain/loss in biodiversity from switching to trees
biodiversity_impact <- biodiversity_agroforestry - biodiversity_treeless
# Define a weighting factor for biodiversity impact (convert biodiversity into € equivalent)
biodiversity_value_per_species <- 500 # Assume each additional species is worth €500</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="biodiversity_variables-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code># Define species richness and grazing impact factors per hectare
species_richness_per_tree <- 0.01 # Each tree adds 0.001 species per ha
species_loss_per_sheep <- 0.001 # Each sheep reduces 0.001 species per ha
# Calculate biodiversity per ha
tree_species_richness <- tree_density * species_richness_per_tree
grazing_species_loss <- stocking_density * species_loss_per_sheep
# Compute biodiversity outcomes per ha
biodiversity_agroforestry <- tree_species_richness - grazing_species_loss
biodiversity_treeless <- -grazing_species_loss # No trees, only grazing effect
# Calculate difference (decision impact of trees)
# measures the financial difference when switching to trees
total_profits <- total_profits_agroforestry - total_profits_treeless
# Calculate difference (decision impact of trees on biodiversity)
# measures the net gain/loss in biodiversity from switching to trees
biodiversity_impact <- biodiversity_agroforestry - biodiversity_treeless
# Define a weighting factor for biodiversity impact (convert biodiversity into € equivalent)
biodiversity_value_per_species <- 500 # Assume each additional species is worth €500</code></pre>
</div>
<p>For the Pareto front plot we will first add the data together, store
results in dataframe and plot a scatter plot. Change the color of the
points in the scatter plot to green.</p>
<div class="tutorial-exercise" data-label="results_data"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code>results <- data.frame(
total_profits,
biodiversity_impact
)
# Scatter plot
plot(results$biodiversity_impact, results$total_profits,
col = "blue", pch = 19, xlab = "Biodiversity Impact (Species Change per ha)",
ylab = "Total Profits (€ per ha)", main = "Pareto Front: Agroforestry vs. Treeless System")</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="results_data-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code>results <- data.frame(
total_profits,
biodiversity_impact
)
# Scatter plot
plot(results$biodiversity_impact, results$total_profits,
col = "green", pch = 19, xlab = "Biodiversity Impact (Species Change per ha)",
ylab = "Total Profits (€ per ha)", main = "Pareto Front: Agroforestry vs. Treeless System")</code></pre>
</div>
<p>Now that we see what the scatter plot looks like what is your
impression?</p>
<div class="form-group shiny-input-container">
<label class="control-label" id="pareto_question-label" for="pareto_question">What do you expect the trade-offs to be between profit and biodiversity?</label>
<input id="pareto_question" type="text" class="shiny-input-text form-control" value="" placeholder="Write your prediction before running the analysis..."/>
</div>
<p>Now we can look for the non-dominated points where no other solution
is better in both biodiversity and total profits. A point (biodiversity,
profit) is Pareto-optimal if no other point has higher biodiversity and
higher profits. Change the color of the points to blue.</p>
<div class="tutorial-exercise" data-label="pareto_points"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Sort results by biodiversity impact
results <- results[order(results$biodiversity_impact, decreasing = TRUE),]
# Initialize Pareto front
pareto_front <- results[1, ] # Start with the first point
# Loop through sorted results and keep only non-dominated points
for (i in 2:nrow(results)) {
if (results$total_profits[i] > tail(pareto_front$total_profits, 1)) {
pareto_front <- rbind(pareto_front, results[i, ])
}
}
# Plot all points
plot(results$biodiversity_impact, results$total_profits,
col = "green", pch = 19, xlab = "Biodiversity Impact (Species Change per ha)",
ylab = "Total Profits (€ per ha)", main = "Pareto Front: Agroforestry vs. Treeless System")
# Highlight Pareto front points
points(pareto_front$biodiversity_impact, pareto_front$total_profits,
col = "red", pch = 19, type = "o", lwd = 2)</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="pareto_points-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code># Sort results by biodiversity impact
results <- results[order(results$biodiversity_impact, decreasing = TRUE),]
# Initialize Pareto front
pareto_front <- results[1, ] # Start with the first point
# Loop through sorted results and keep only non-dominated points
for (i in 2:nrow(results)) {
if (results$total_profits[i] > tail(pareto_front$total_profits, 1)) {
pareto_front <- rbind(pareto_front, results[i, ])
}
}
# Plot all points
plot(results$biodiversity_impact, results$total_profits,
col = "green", pch = 19, xlab = "Biodiversity Impact (Species Change per ha)",
ylab = "Total Profits (€ per ha)", main = "Pareto Front: Agroforestry vs. Treeless System")
# Highlight Pareto front points
points(pareto_front$biodiversity_impact, pareto_front$total_profits,
col = "blue", pch = 19, type = "o", lwd = 2)</code></pre>
</div>
<p>Now we can zoom in on the Pareto-optimal points and examine the
conditions for key operational variables we calculated earlier.</p>
<div class="tutorial-exercise" data-label="pareto_conditions"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Extract operational conditions for Pareto-optimal points
pareto_conditions <- results[results$biodiversity_impact %in% pareto_front$biodiversity_impact &
results$total_profits %in% pareto_front$total_profits,]
# Merge with original dataset to include operational variables
pareto_conditions <- merge(pareto_conditions, data.frame(
biodiversity_impact, total_profits, stocking_density, tree_density, proportion_tree_area),
by = c("biodiversity_impact", "total_profits"))
# Show Pareto-optimal conditions
print(pareto_conditions)</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support"
data-label="pareto_conditions-solution" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code># Extract operational conditions for Pareto-optimal points
pareto_conditions <- results[results$biodiversity_impact %in% pareto_front$biodiversity_impact &
results$total_profits %in% pareto_front$total_profits,]
# Merge with original dataset to include operational variables
pareto_conditions <- merge(pareto_conditions, data.frame(
biodiversity_impact, total_profits, stocking_density, tree_density, proportion_tree_area),
by = c("biodiversity_impact", "total_profits"))
# Show Pareto-optimal conditions
print(pareto_conditions)</code></pre>
</div>
<p>Finally we can plot the Pareto. Make the color of the points
blue.</p>
<div class="tutorial-exercise" data-label="plot_pareto"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Determine dynamic position based on plot coordinates
text_positions <- ifelse(
pareto_front$biodiversity_impact > median(pareto_front$biodiversity_impact), 2, 4
)
# Adjust for very high or low points
text_positions <- ifelse(
pareto_front$total_profits > quantile(pareto_front$total_profits, 0.75), 1, text_positions
)
text_positions <- ifelse(
pareto_front$total_profits < quantile(pareto_front$total_profits, 0.25), 3, text_positions
)
# Plot Pareto front
plot(pareto_front$biodiversity_impact, pareto_front$total_profits,
col = "red", pch = 19, type = "o", lwd = 2,
xlab = "Biodiversity Impact (Species Change per ha)",
ylab = "Total Profits (€ per ha)", main = "Pareto Front: Agroforestry vs. Treeless System")
# Add footnote at the bottom
mtext("S = Stocking Density (sheep/ha), T = Tree Density (trees/ha), P = Proportion of Tree Area",
side = 1, line = 4, cex = 0.8)
# Create multi-line labels with line breaks
labels <- paste0("S:", round(pareto_conditions$stocking_density, 1), "\n",
"T:", round(pareto_conditions$tree_density, 1), "\n",
"P:", round(pareto_conditions$proportion_tree_area, 2))
# Annotate with dynamically positioned text
text(pareto_front$biodiversity_impact, pareto_front$total_profits,
labels = labels, pos = text_positions, cex = 0.8)</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<div class="tutorial-exercise-support" data-label="plot_pareto-solution"
data-completion="1" data-diagnostics="1" data-startover="1"
data-lines="0" data-pipe="|>">
<pre class="text"><code># Determine dynamic position based on plot coordinates
text_positions <- ifelse(
pareto_front$biodiversity_impact > median(pareto_front$biodiversity_impact), 2, 4
)
# Adjust for very high or low points
text_positions <- ifelse(
pareto_front$total_profits > quantile(pareto_front$total_profits, 0.75), 1, text_positions
)
text_positions <- ifelse(
pareto_front$total_profits < quantile(pareto_front$total_profits, 0.25), 3, text_positions
)
# Plot Pareto front
plot(pareto_front$biodiversity_impact, pareto_front$total_profits,
col = "blue", pch = 19, type = "o", lwd = 2,
xlab = "Biodiversity Impact (Species Change per ha)",
ylab = "Total Profits (€ per ha)", main = "Pareto Front: Agroforestry vs. Treeless System")
# Add footnote at the bottom
mtext("S = Stocking Density (sheep/ha), T = Tree Density (trees/ha), P = Proportion of Tree Area",
side = 1, line = 4, cex = 0.8)
# Create multi-line labels with line breaks
labels <- paste0("S:", round(pareto_conditions$stocking_density, 1), "\n",
"T:", round(pareto_conditions$tree_density, 1), "\n",
"P:", round(pareto_conditions$proportion_tree_area, 2))
# Annotate with dynamically positioned text
text(pareto_front$biodiversity_impact, pareto_front$total_profits,
labels = labels, pos = text_positions, cex = 0.8)</code></pre>
</div>
<div class="form-group shiny-input-container">
<label class="control-label" id="pareto_reflection-label" for="pareto_reflection">What did you observe in the Pareto front?</label>
<input id="pareto_reflection" type="text" class="shiny-input-text form-control" value=""/>
</div>
<hr />
</div>
<div id="section-next-steps" class="section level2">
<h2><strong>6. Next Steps</strong></h2>
<div class="tutorial-exercise" data-label="own_work" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<pre class="text"><code># Define a decision problem related to agroecology.
# What are the key variables?
# Example: Define key income and cost variables
your_income <- runif(100, min = ???, max = ???) # Fill in realistic values
your_costs <- runif(100, min = ???, max = ???)
# Compute profits
your_profits <- your_income - your_costs
# --- Next Steps ---
# Add an uncertainty factor (e.g., climate risk)
# Include biodiversity or sustainability impacts
# run a Monte Carlo simulation</code></pre>
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<hr />
</div>
<div id="section-final-reflection" class="section level2">
<h2><strong>7. Final Reflection</strong></h2>
<ul>
<li>What insights did you gain from this tutorial?</li>
<li>How would you improve your decision model?</li>
<li>How does this relate to <strong>real-world agroecology and
conservation?</strong></li>
</ul>
<div class="form-group shiny-input-container">
<label class="control-label" id="engagement-label" for="engagement">How engaging was this tutorial?</label>
<input class="js-range-slider" id="engagement" data-skin="shiny" data-min="1" data-max="5" data-from="3" data-step="1" data-grid="true" data-grid-num="4" data-grid-snap="false" data-prettify-separator="," data-prettify-enabled="true" data-keyboard="true" data-data-type="number"/>
</div>
<div class="form-group shiny-input-container">
<label class="control-label" id="difficulty-label" for="difficulty">What was the hardest part to understand?</label>
<input id="difficulty" type="text" class="shiny-input-text form-control" value=""/>
</div>
<div class="form-group shiny-input-container">
<label class="control-label" id="feedback-label" for="feedback">Share your key takeaways:</label>
<input id="feedback" type="text" class="shiny-input-text form-control" value="" placeholder="Write your reflections here..."/>
</div>
<hr />
</div>
<div id="section-next-steps-1" class="section level2">
<h2><strong>Next Steps</strong></h2>
<ul>
<li>Apply Decision Analysis to your own research</li>
<li>Continue refining expert-elicited models</li>
</ul>
<p>Thanks for following this tutorial. As thanks, here is an open space
for running your scripts.</p>
<div class="tutorial-exercise" data-label="blank" data-completion="1"
data-diagnostics="1" data-startover="1" data-lines="0"
data-pipe="|>">
<script type="application/json" data-ui-opts="1">{"engine":"r","has_checker":false,"caption":"<span data-i18n=\"text.enginecap\" data-i18n-opts=\"{"engine":"R"}\">R Code<\/span>"}</script>
</div>
<hr />
</div>
<div id="section-references" class="section level2">
<h2>References</h2>
<p>Whitney, C, K Shepherd, and E Luedeling. “Decision Analysis Methods
Guide; Agricultural Policy for Nutrition.” World Agroforestry (ICRAF)
Working Paper series, no. 275 (2018): 40. <a
href="http://dx.doi.org/10.5716/WP18001.PDF"
class="uri">http://dx.doi.org/10.5716/WP18001.PDF</a>.</p>
<p>Whitney, Cory, Denis Lanzanova, Caroline Muchiri, Keith D. Shepherd,
Todd S. Rosenstock, Michael Krawinkel, John R. S. Tabuti, and Eike
Luedeling. “Probabilistic Decision Tools for Determining Impacts of
Agricultural Development Policy on Household Nutrition.” Earth’s Future
6, no. 3 (2018): 359–72. <a href="https://doi.org/10.1002/2017EF000765"
class="uri">https://doi.org/10.1002/2017EF000765</a>.</p>
<p>Whitney, Cory, Gordon O’Brien, Vuyisile Dlamini, Ikhothatseng Jacob
Greffiths, Chris Dickens, and Eike Luedeling. “Balancing Ecosystem
Sustainability and Irrigated Smallholder Agriculture: A Modeling
Approach for Water Resource Management.” Journal of Hydrology 651 (April
2025): 132560. <a href="https://doi.org/10.1016/j.jhydrol.2024.132560"
class="uri">https://doi.org/10.1016/j.jhydrol.2024.132560</a>.</p>
<p>Whitney, Cory, Lisa Biber-Freudenberger, and Eike Luedeling.
“Decision Analytical Methods for Assessing the Efficacy of Agroecology
Interventions.” CABI Agriculture and Bioscience 4, no. 1 (2023): 9. <a
href="https://doi.org/10.1186/s43170-023-00151-9"
class="uri">https://doi.org/10.1186/s43170-023-00151-9</a>.</p>
<p>
<script type="application/shiny-prerendered" data-context="server-start">
knitr::opts_chunk$set(echo = TRUE)
library(decisionSupport)
library(ggplot2)
library(learnr)
library(shiny)
library(DiagrammeR)
source("functions/monte_carlo.R")
source("functions/apples_sheep_in_class.R")
source("functions/apple_sheep_pareto.R")
</script>
<script type="application/shiny-prerendered" data-context="server">
learnr:::register_http_handlers(session, metadata = NULL)
</script>
<script type="application/shiny-prerendered" data-context="server">
learnr:::prepare_tutorial_state(session)
</script>
<script type="application/shiny-prerendered" data-context="server">
learnr:::i18n_observe_tutorial_language(input, session)
</script>
<script type="application/shiny-prerendered" data-context="server">
session$onSessionEnded(function() {
learnr:::event_trigger(session, "session_stop")
})
</script>
<script type="application/shiny-prerendered" data-context="server">
learnr:::question_prerendered_chunk(structure(list(type = "learnr_radio", label = "question1-1",
question = structure("What is a good reason to use a probabilistic approach to support decision-making in agroecology?", html = TRUE, class = c("html",
"character")), answers = list(structure(list(id = "lnr_ans_b55c462",
option = "It captures uncertainty in outcomes and trade-offs.",
value = "It captures uncertainty in outcomes and trade-offs.",
label = structure("It captures uncertainty in outcomes and trade-offs.", html = TRUE, class = c("html",
"character")), correct = TRUE, message = NULL, type = "literal"), class = c("tutorial_question_answer",
"tutorial_quiz_answer")), structure(list(id = "lnr_ans_b0e1f88",
option = "It provides fixed deterministic solutions.",
value = "It provides fixed deterministic solutions.",
label = structure("It provides fixed deterministic solutions.", html = TRUE, class = c("html",
"character")), correct = FALSE, message = NULL, type = "literal"), class = c("tutorial_question_answer",
"tutorial_quiz_answer")), structure(list(id = "lnr_ans_d537632",
option = "It replaces the need for expert knowledge.",
value = "It replaces the need for expert knowledge.",
label = structure("It replaces the need for expert knowledge.", html = TRUE, class = c("html",
"character")), correct = FALSE, message = NULL, type = "literal"), class = c("tutorial_question_answer",
"tutorial_quiz_answer")), structure(list(id = "lnr_ans_faffd3e",
option = "It avoids the need for validation.", value = "It avoids the need for validation.",
label = structure("It avoids the need for validation.", html = TRUE, class = c("html",
"character")), correct = FALSE, message = NULL, type = "literal"), class = c("tutorial_question_answer",
"tutorial_quiz_answer"))), button_labels = list(submit = structure("<span data-i18n=\"button.questionsubmit\">Submit Answer<\u002fspan>", html = TRUE, class = c("html",
"character")), try_again = structure("<span data-i18n=\"button.questiontryagain\">Try Again<\u002fspan>", html = TRUE, class = c("html",
"character"))), messages = list(correct = structure("Correct!", html = TRUE, class = c("html",
"character")), try_again = structure("Incorrect", html = TRUE, class = c("html",
"character")), incorrect = structure("Incorrect", html = TRUE, class = c("html",
"character")), message = NULL, post_message = NULL), ids = list(
answer = "question1-1-answer", question = "question1-1"),
loading = NULL, random_answer_order = FALSE, allow_retry = FALSE,
seed = 153002688.928753, options = list()), class = c("learnr_radio",
"tutorial_question")), session = session)
</script>
<script type="application/shiny-prerendered" data-context="server">
`tutorial-exercise-decision_model-result` <- learnr:::setup_exercise_handler(reactive(req(input$`tutorial-exercise-decision_model-code-editor`)), session)
output$`tutorial-exercise-decision_model-output` <- renderUI({
`tutorial-exercise-decision_model-result`()
})
</script>
<script type="application/shiny-prerendered" data-context="server">
learnr:::store_exercise_cache(structure(list(label = "decision_model", global_setup = structure(c("knitr::opts_chunk$set(echo = TRUE)",
"", "library(decisionSupport)", "library(ggplot2)", "library(learnr)",
"library(shiny)", "library(DiagrammeR)", "", "source(\"functions/monte_carlo.R\")",
"source(\"functions/apples_sheep_in_class.R\")", "source(\"functions/apple_sheep_pareto.R\")",
""), chunk_opts = list(label = "setup", include = FALSE)), setup = NULL,
chunks = list(list(label = "decision_model", code = "DiagrammeR::grViz(\"\ndigraph decision_model {\n # Define node styles\n node [shape=box, style=filled, fillcolor=lightblue] Decision;\n node [shape=ellipse, style=filled, fillcolor=lightgrey] Apple_Income Sheep_Income Costs;\n node [shape=ellipse, style=filled, fillcolor=green] Outcome_Profit;\n\n # Define relationships\n Decision -> Apple_Income;\n Decision -> Sheep_Income;\n Decision -> Costs;\n Costs -> Apple_Income;\n Costs -> Sheep_Income;\n Apple_Income -> Outcome_Profit;\n Sheep_Income -> Outcome_Profit;\n}\n\")",
opts = list(label = "\"decision_model\"", exercise = "TRUE"),
engine = "r")), code_check = NULL, error_check = NULL,
check = NULL, solution = NULL, tests = NULL, options = list(
eval = FALSE, echo = TRUE, results = "markup", tidy = FALSE,
tidy.opts = NULL, collapse = FALSE, prompt = FALSE, comment = NA,
highlight = FALSE, size = "normalsize", background = "#F7F7F7",
strip.white = TRUE, cache = 0, cache.path = "Tutorial_cache/html/",
cache.vars = NULL, cache.lazy = TRUE, dependson = NULL,
autodep = FALSE, cache.rebuild = FALSE, fig.keep = "high",
fig.show = "asis", fig.align = "default", fig.path = "Tutorial_files/figure-html/",
dev = "png", dev.args = NULL, dpi = 192, fig.ext = "png",
fig.width = 6.5, fig.height = 4, fig.env = "figure",
fig.cap = NULL, fig.scap = NULL, fig.lp = "fig:", fig.subcap = NULL,
fig.pos = "", out.width = 624, out.height = NULL, out.extra = NULL,
fig.retina = 2, external = TRUE, sanitize = FALSE, interval = 1,
aniopts = "controls,loop", warning = TRUE, error = FALSE,
message = TRUE, render = NULL, ref.label = NULL, child = NULL,
engine = "r", split = FALSE, include = TRUE, purl = TRUE,
max.print = 1000, label = "decision_model", exercise = TRUE,
code = c("DiagrammeR::grViz(\"", "digraph decision_model {",
" # Define node styles", " node [shape=box, style=filled, fillcolor=lightblue] Decision;",
" node [shape=ellipse, style=filled, fillcolor=lightgrey] Apple_Income Sheep_Income Costs;",
" node [shape=ellipse, style=filled, fillcolor=green] Outcome_Profit;",
"", " # Define relationships", " Decision -> Apple_Income;",
" Decision -> Sheep_Income;", " Decision -> Costs;",
" Costs -> Apple_Income;", " Costs -> Sheep_Income;",
" Apple_Income -> Outcome_Profit;", " Sheep_Income -> Outcome_Profit;",
"}", "\")"), out.width.px = 624, out.height.px = 384,
params.src = "decision_model, exercise=TRUE", fig.num = 0,
exercise.df_print = "paged", exercise.checker = "NULL"),
engine = "r", version = "4"), class = c("r", "tutorial_exercise"
)))
</script>
<script type="application/shiny-prerendered" data-context="server">
`tutorial-exercise-apples_model-result` <- learnr:::setup_exercise_handler(reactive(req(input$`tutorial-exercise-apples_model-code-editor`)), session)
output$`tutorial-exercise-apples_model-output` <- renderUI({
`tutorial-exercise-apples_model-result`()
})
</script>
<script type="application/shiny-prerendered" data-context="server">
learnr:::store_exercise_cache(structure(list(label = "apples_model", global_setup = structure(c("knitr::opts_chunk$set(echo = TRUE)",
"", "library(decisionSupport)", "library(ggplot2)", "library(learnr)",
"library(shiny)", "library(DiagrammeR)", "", "source(\"functions/monte_carlo.R\")",
"source(\"functions/apples_sheep_in_class.R\")", "source(\"functions/apple_sheep_pareto.R\")",
""), chunk_opts = list(label = "setup", include = FALSE)), setup = NULL,
chunks = list(list(label = "apples_model", code = "\n# set.seed(42)\nnum_simulations <- 100\n\n# Define ranges per ha per year Euro\n# lower and upper\napple_income <- runif(n = num_simulations, \n min = 3000, \n max = 60000)\n\napple_costs <- runif(n = num_simulations, \n min = 15000, \n max = 30000)\n\napple_profits <- apple_income - apple_costs",
opts = list(label = "\"apples_model\"", exercise = "TRUE"),
engine = "r")), code_check = NULL, error_check = NULL,
check = NULL, solution = structure(c("", "set.seed(42)",
"num_simulations <- 1000", "", "# Define ranges per ha per year Euro",
"# lower and upper", "apple_income <- runif(n = num_simulations, ",
" min = 3000, ", " max = 60000)",
"", "apple_costs <- runif(n = num_simulations, ", " min = 15000, ",
" max = 30000)", "", "apple_profits <- apple_income - apple_costs"
), chunk_opts = list(label = "apples_model-solution")), tests = NULL,
options = list(eval = FALSE, echo = TRUE, results = "markup",
tidy = FALSE, tidy.opts = NULL, collapse = FALSE, prompt = FALSE,
comment = NA, highlight = FALSE, size = "normalsize",
background = "#F7F7F7", strip.white = TRUE, cache = 0,
cache.path = "Tutorial_cache/html/", cache.vars = NULL,
cache.lazy = TRUE, dependson = NULL, autodep = FALSE,
cache.rebuild = FALSE, fig.keep = "high", fig.show = "asis",
fig.align = "default", fig.path = "Tutorial_files/figure-html/",
dev = "png", dev.args = NULL, dpi = 192, fig.ext = "png",
fig.width = 6.5, fig.height = 4, fig.env = "figure",
fig.cap = NULL, fig.scap = NULL, fig.lp = "fig:", fig.subcap = NULL,
fig.pos = "", out.width = 624, out.height = NULL, out.extra = NULL,
fig.retina = 2, external = TRUE, sanitize = FALSE, interval = 1,
aniopts = "controls,loop", warning = TRUE, error = FALSE,
message = TRUE, render = NULL, ref.label = NULL, child = NULL,
engine = "r", split = FALSE, include = TRUE, purl = TRUE,
max.print = 1000, label = "apples_model", exercise = TRUE,
code = c("", "# set.seed(42)", "num_simulations <- 100",
"", "# Define ranges per ha per year Euro", "# lower and upper",
"apple_income <- runif(n = num_simulations, ", " min = 3000, ",
" max = 60000)", "", "apple_costs <- runif(n = num_simulations, ",
" min = 15000, ", " max = 30000)",
"", "apple_profits <- apple_income - apple_costs"), out.width.px = 624,
out.height.px = 384, params.src = "apples_model, exercise=TRUE",
fig.num = 0, exercise.df_print = "paged", exercise.checker = "NULL"),
engine = "r", version = "4"), class = c("r", "tutorial_exercise"
)))
</script>
<script type="application/shiny-prerendered" data-context="server">
`tutorial-exercise-sheep_model-result` <- learnr:::setup_exercise_handler(reactive(req(input$`tutorial-exercise-sheep_model-code-editor`)), session)
output$`tutorial-exercise-sheep_model-output` <- renderUI({
`tutorial-exercise-sheep_model-result`()
})
</script>
<script type="application/shiny-prerendered" data-context="server">
learnr:::store_exercise_cache(structure(list(label = "sheep_model", global_setup = structure(c("knitr::opts_chunk$set(echo = TRUE)",
"", "library(decisionSupport)", "library(ggplot2)", "library(learnr)",
"library(shiny)", "library(DiagrammeR)", "", "source(\"functions/monte_carlo.R\")",
"source(\"functions/apples_sheep_in_class.R\")", "source(\"functions/apple_sheep_pareto.R\")",
""), chunk_opts = list(label = "setup", include = FALSE)), setup = NULL,
chunks = list(list(label = "sheep_model", code = "\n# Euro per ha per year\nsheep_income <- runif(n = num_simulations, \n min = 2000, \n max = 5000)\n\nsheep_costs <- runif(n = num_simulations, \n min = 1000, \n max = 2500)\n\nsheep_profits <- sheep_income - sheep_costs",
opts = list(label = "\"sheep_model\"", exercise = "TRUE"),
engine = "r")), code_check = NULL, error_check = NULL,
check = NULL, solution = structure(c("", "# Euro per ha per year",
"sheep_income <- runif(n = num_simulations, ", " min = 2000, ",
" max = 5000)", "", "sheep_costs <- runif(n = num_simulations, ",
" min = 1000, ", " max = 2500)",
"", "sheep_profits <- sheep_income - sheep_costs", "", "total_profits <- apple_profits + sheep_profits"
), chunk_opts = list(label = "sheep_model-solution")), tests = NULL,
options = list(eval = FALSE, echo = TRUE, results = "markup",
tidy = FALSE, tidy.opts = NULL, collapse = FALSE, prompt = FALSE,
comment = NA, highlight = FALSE, size = "normalsize",
background = "#F7F7F7", strip.white = TRUE, cache = 0,
cache.path = "Tutorial_cache/html/", cache.vars = NULL,
cache.lazy = TRUE, dependson = NULL, autodep = FALSE,
cache.rebuild = FALSE, fig.keep = "high", fig.show = "asis",
fig.align = "default", fig.path = "Tutorial_files/figure-html/",
dev = "png", dev.args = NULL, dpi = 192, fig.ext = "png",
fig.width = 6.5, fig.height = 4, fig.env = "figure",
fig.cap = NULL, fig.scap = NULL, fig.lp = "fig:", fig.subcap = NULL,
fig.pos = "", out.width = 624, out.height = NULL, out.extra = NULL,
fig.retina = 2, external = TRUE, sanitize = FALSE, interval = 1,
aniopts = "controls,loop", warning = TRUE, error = FALSE,
message = TRUE, render = NULL, ref.label = NULL, child = NULL,
engine = "r", split = FALSE, include = TRUE, purl = TRUE,
max.print = 1000, label = "sheep_model", exercise = TRUE,
code = c("", "# Euro per ha per year", "sheep_income <- runif(n = num_simulations, ",
" min = 2000, ", " max = 5000)",
"", "sheep_costs <- runif(n = num_simulations, ", " min = 1000, ",
" max = 2500)", "", "sheep_profits <- sheep_income - sheep_costs"
), out.width.px = 624, out.height.px = 384, params.src = "sheep_model, exercise=TRUE",
fig.num = 0, exercise.df_print = "paged", exercise.checker = "NULL"),
engine = "r", version = "4"), class = c("r", "tutorial_exercise"
)))
</script>
<script type="application/shiny-prerendered" data-context="server">
`tutorial-exercise-plot_profits_overlay-result` <- learnr:::setup_exercise_handler(reactive(req(input$`tutorial-exercise-plot_profits_overlay-code-editor`)), session)
output$`tutorial-exercise-plot_profits_overlay-output` <- renderUI({
`tutorial-exercise-plot_profits_overlay-result`()
})
</script>
<script type="application/shiny-prerendered" data-context="server">
learnr:::store_exercise_cache(structure(list(label = "plot_profits_overlay", global_setup = structure(c("knitr::opts_chunk$set(echo = TRUE)",
"", "library(decisionSupport)", "library(ggplot2)", "library(learnr)",
"library(shiny)", "library(DiagrammeR)", "", "source(\"functions/monte_carlo.R\")",
"source(\"functions/apples_sheep_in_class.R\")", "source(\"functions/apple_sheep_pareto.R\")",
""), chunk_opts = list(label = "setup", include = FALSE)), setup = NULL,
chunks = list(list(label = "plot_profits_overlay", code = "hist(total_profits, col = \"white\")\nhist(apple_profits, add = TRUE, col = \"pink\")\nhist(sheep_profits, add = TRUE, col = \"black\")",
opts = list(label = "\"plot_profits_overlay\"", exercise = "TRUE"),
engine = "r")), code_check = NULL, error_check = NULL,
check = NULL, solution = structure(c("hist(total_profits, col = \"white\")",
"hist(apple_profits, add = TRUE, col = \"grey\")", "hist(sheep_profits, add = TRUE, col = \"black\")"
), chunk_opts = list(label = "plot_profits_overlay-solution")),
tests = NULL, options = list(eval = FALSE, echo = TRUE, results = "markup",
tidy = FALSE, tidy.opts = NULL, collapse = FALSE, prompt = FALSE,
comment = NA, highlight = FALSE, size = "normalsize",
background = "#F7F7F7", strip.white = TRUE, cache = 0,
cache.path = "Tutorial_cache/html/", cache.vars = NULL,
cache.lazy = TRUE, dependson = NULL, autodep = FALSE,
cache.rebuild = FALSE, fig.keep = "high", fig.show = "asis",
fig.align = "default", fig.path = "Tutorial_files/figure-html/",
dev = "png", dev.args = NULL, dpi = 192, fig.ext = "png",
fig.width = 6.5, fig.height = 4, fig.env = "figure",
fig.cap = NULL, fig.scap = NULL, fig.lp = "fig:", fig.subcap = NULL,
fig.pos = "", out.width = 624, out.height = NULL, out.extra = NULL,
fig.retina = 2, external = TRUE, sanitize = FALSE, interval = 1,
aniopts = "controls,loop", warning = TRUE, error = FALSE,
message = TRUE, render = NULL, ref.label = NULL, child = NULL,
engine = "r", split = FALSE, include = TRUE, purl = TRUE,
max.print = 1000, label = "plot_profits_overlay", exercise = TRUE,
code = c("hist(total_profits, col = \"white\")", "hist(apple_profits, add = TRUE, col = \"pink\")",
"hist(sheep_profits, add = TRUE, col = \"black\")"),
out.width.px = 624, out.height.px = 384, params.src = "plot_profits_overlay, exercise=TRUE",
fig.num = 0, exercise.df_print = "paged", exercise.checker = "NULL"),
engine = "r", version = "4"), class = c("r", "tutorial_exercise"
)))
</script>
<script type="application/shiny-prerendered" data-context="server">