-
Notifications
You must be signed in to change notification settings - Fork 0
/
workshop_git_dashboard.tex
1324 lines (1070 loc) · 50.9 KB
/
workshop_git_dashboard.tex
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
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
\PassOptionsToPackage{dvipsnames,svgnames,x11names}{xcolor}
%
\documentclass[
12pt,
]{article}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{iftex}
\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
\KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\usepackage[margin=1in]{geometry}
\usepackage{longtable,booktabs,array}
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\usepackage{longtable}
\usepackage{graphics}
\usepackage{xparse}
\usepackage{moresize}
\usepackage{setspace}
\usepackage{tcolorbox}
\usepackage{wrapfig}
\usepackage{helvet}
\usepackage{sectsty}
\usepackage{fancyhdr}
\usepackage{xpatch}
\usepackage{booktabs}
\onehalfspacing
\pagestyle{fancy}
\definecolor{gssmidblue}{RGB}{32, 115, 188}
\definecolor{dfeheadingblue}{RGB}{16, 79, 117}
\renewcommand{\familydefault}{\sfdefault}
\allsectionsfont{\color{dfeheadingblue}}
\sectionfont{\color{dfeheadingblue}\fontsize{16}{18}\selectfont}
\fancyhead[C]{}
\fancyhead[RL]{}
\fancyfoot[LR]{}
\fancyfoot[C]{\sffamily \thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\futurelet\TMPfootrule\def\footrule{{\color{gssmidblue}\TMPfootrule}}
\usepackage{floatrow}
\floatsetup[figure]{capposition=top}
\usepackage[tableposition=top]{caption}
\usepackage[titles]{tocloft}
\renewcommand{\cftdot}{}
\AtBeginDocument{\let\maketitle\relax}
\usepackage{cellspace}
\usepackage{etoolbox}
\colorlet{headercolour}{DarkSeaGreen}
\ifLuaTeX
\usepackage{selnolig} % disable illegal ligatures
\fi
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same} % disable monospaced font for URLs
\hypersetup{
pdftitle={DfE Statistics Development Team Workshops},
pdfauthor={Department for Education},
colorlinks=true,
linkcolor={Maroon},
filecolor={Maroon},
citecolor={Blue},
urlcolor={blue},
pdfcreator={LaTeX via pandoc}}
\title{DfE Statistics Development Team Workshops}
\author{Department for Education}
\date{}
\begin{document}
\maketitle
\resizebox{48mm}{!}{\includegraphics{images/Department_for_Education.png}}
\vspace*{0.24\textheight}
\raggedright\HUGE{\color{dfeheadingblue}\textbf{DfE Statistics Development Team Workshops}}
\huge{\color{dfeheadingblue}\textbf{Using git and GitHub (building a R-Shiny dashboard)}}
\vspace*{2\baselineskip}
\normalsize
\newpage
{
\hypersetup{linkcolor=}
\setcounter{tocdepth}{2}
\tableofcontents
}
\newpage
\hypertarget{introduction}{%
\section{Introduction}\label{introduction}}
We've prepared this walkthrough guide for statistics publication teams
as an introduction on how to work collaboratively with git, using the
creation of a data dashboard as a relevant context. The guide is
intended to be step-by-step, building up from the very basics. The plan
is to work through this in groups of 3 or so and with access to
experienced git users to call on for support. If it starts too basic for
your level, then just go through at your own/your group's pace as you
see fit. By no means can we cover everything in this walkthrough, so
please see it as a prompt to ask follow-up questions as you're working
through on anything related to git, GitHub and Dev Ops.
For simplicity of access and accounts, we're focussing on GitHub rather
than Dev Ops here, but much of the material is transferable.
\hypertarget{github-versus-dev-ops}{%
\subsection{GitHub versus Dev Ops}\label{github-versus-dev-ops}}
GitHub and Dev Ops effectively provide the same service in terms of
creating software via a git repository: they both act as the host for
the remote repository, whilst offering important tools to manage bugs
and issues, tasks, merging branches, deploying applications and so on.
Dev Ops is part of the Microsoft Azure platform and uses pricate DfE
servers. This can allow you to connect or deploy your repository into
wider Azure services. This includes SQL databases that you might already
be storing data on as well as the DfE's implementation of rsconnect on
DfE internal servers, which allows deployment of shiny apps for internal
DfE use.
GitHub is hosted on external servers and therefore is more appropriate
for making your code or application available for public access and use.
For example, from a GitHub repository, you can deploy an R Shiny
dashboard to shinyapps.io where members of the public may view and
interact with your published data.
\hypertarget{pre-workshop-requirements}{%
\section{Pre-workshop requirements}\label{pre-workshop-requirements}}
First of all, make sure to bring your laptop. This is going to be
interactive and require you to do some coding.
Preferably before coming along, you'll need to go through the following
list of things you'll need to make sure are set up on your DfE laptop:
\begin{itemize}
\item
Create a GitHub account: \url{https://github.com/join}; Either:
\item
Install git on your laptop: \url{https://git-scm.com/downloads};
\item
Install R-Studio on your machine: Download \textbf{R for Windows
(x64)} and \textbf{RStudio} from the Software Centre on your DfE
laptop.
\end{itemize}
Or:
\begin{itemize}
\tightlist
\item
If you're on EDAP and used to using R/R-Studio and/or git on there,
feel free to just use that.
\end{itemize}
You'll also need to make sure that git is set up in the git/SVN pane of
global options in R-Studio (found in the Tools drop down menu). Make
sure the path to your git executable is entered in the git path box and
git should automatically be integrated with R-Studio.
\begin{figure}
\includegraphics[width=0.64\linewidth]{images/gitdemo/gitdemo-gitRstudio-settings} \caption{Enter the path to your git executable in the git path option box}\label{fig:unnamed-chunk-1}
\end{figure}
Once you open a repository, you'll get an extra panel, named `git', in
the top right pane of R-Studio and you'll also be able to use git in the
`Terminal' tab at the bottom left (in the same place as the R console).
\begin{figure}
\includegraphics[width=0.56\linewidth]{images/gitdemo/gitdemo-gitRstudio-NewTerminal} \caption{The `git BASH` terminal in R-Studio}\label{fig:unnamed-chunk-2}
\end{figure}
A useful thing here if you want to use git commands in the terminal is
to switch the terminal from the default Windows Command Prompt to
\texttt{git\ BASH}. You can do this in the Terminal tab of R-Studio's
global options - just select \texttt{git\ BASH} from the `New terminal
opens with' pull down menu. Click apply and then select the Terminal tab
(next to the Console tab), click `Terminal 1' and then select `New
terminal' from the drop down menu. You should see something similar to
the terminal screenshot.
\newpage
\hypertarget{setting-up-the-repository}{%
\section{Setting up the repository}\label{setting-up-the-repository}}
\hypertarget{creating-a-new-repository-on-github}{%
\subsection{Creating a new repository on
GitHub}\label{creating-a-new-repository-on-github}}
At this point, we're ready to create a new repository. The context of
this exercise is to create a dashboard, so let's get a head start on
that by using the DfE R-Shiny template.
\begin{figure}
\centering
\includegraphics{images/gitdemo/gitdemo-shinydash-template.png}
\caption{Click the use this template button to create the new
repository}
\end{figure}
You can access the template here:
\url{https://github.com/dfe-analytical-services/shiny-template}
On that page, you'll see the main repository page. This contains a menu
bar to navigate the range of GitHub features (e.g.~Code, Issues, Pull
Requests, Discussions, Actions and others); shortcuts to access
different branches within the repository; some top-level summary
information on the repository; a listing of the files and folders in the
repository's root directory; and a markdown render of the repository's
Readme file if one exists.
In the case of our template, you'll also see a button saying use this
repo as a template. At this point, \textbf{one (and only one!)} of your
group should click that button, which will take you to the create
repository page. Here you have the option to create a copy of the
template in your own GitHub area as shown below. Give the new repository
a name and a description and then click ``Create repository from
template''.
\begin{figure}
\includegraphics[width=0.72\linewidth]{images/gitdemo/gitdemo-shinydash-newrepofromtemplate} \caption{Put in a name for the new repo and a quick description and then click on Create repository from template}\label{fig:unnamed-chunk-3}
\end{figure}
One of you at least should now have their own repository produced from
this starting template project. We need everyone in your team to be able
to work on this project however, so now you'll need to give access to
your other team members. To do so, navigate to \textbf{Settings} (at the
far right of the menu bar on your repository page) and select
\textbf{Collaborators} on the left hand side. Under \textbf{Manage
access}, add your teammates via either their email addresses or their
GitHub user names.
\hypertarget{cloning-the-repository-to-your-local-machine}{%
\subsection{Cloning the repository to your local
machine}\label{cloning-the-repository-to-your-local-machine}}
Cloning the repository refers to creating a copy of the remote
repository (i.e.~the copy on GitHub or Dev Ops) on the disk on your
local machine (i.e.~your DfE laptop). For an R project, there are two
basic options to choose from for doing this:
\begin{itemize}
\tightlist
\item
using the R-Studio new project wizard, or
\item
using \texttt{git\ BASH}.
\end{itemize}
We'd recommend trying the different options across your working group.
\hypertarget{cloning-in-git-bash}{%
\subsubsection{\texorpdfstring{Cloning in
\texttt{git\ BASH}}{Cloning in git BASH}}\label{cloning-in-git-bash}}
You can open up a \texttt{git\ BASH} terminal, by typing
\texttt{git\ BASH} in the Windows search bar and select
\texttt{git\ BASH} when it comes up. With a terminal, you can interact
with it just by typing, similar to working in the R console in RStudio.
First let's make a directory in which to store our repositories:
\begin{verbatim}
mkdir repos
\end{verbatim}
We can then move into the directory we just created using:
\begin{verbatim}
cd repos
\end{verbatim}
Now grab the repo url and replace
\texttt{\textless{}repo\_url\textgreater{}} in the next command with the
actual url:
\begin{verbatim}
git clone <repo_url>
\end{verbatim}
You should get some messages letting you know git is connecting to the
server and cloning the repository and it should look something like the
figure below.
\begin{figure}
\includegraphics[width=0.6\linewidth]{images/gitdemo/gitdemo-terminal_clone} \caption{Cloning a repository in git BASH}\label{fig:unnamed-chunk-4}
\end{figure}
If all went well, you'll now have a complete copy of the repository on
your laptop. To open the repository in RStudio, start up RStudio and
select Open project. In the file explorer window that opens up, type
\texttt{C:\textbackslash{}Users\textbackslash{}} and hit enter (see the
screenshot below) and then open up your home folder.
\begin{figure}
\includegraphics[width=0.6\linewidth]{images/gitdemo/gitdemo-RStudio_OpenProj} \caption{Open a cloned project in RStudio}\label{fig:unnamed-chunk-5}
\end{figure}
Then navigate into \texttt{repos} and the repository folder. The full
path should be something along the lines of:
\begin{quote}
\texttt{This\ PC\ \textgreater{}\ Windows\ (C:)\ \textgreater{}\ Users\ \textgreater{}\ \textless{}USERNAME\textgreater{}\ \textgreater{}\ repos\ \textgreater{}\ \textless{}REPONAME\textgreater{}}
\end{quote}
Select the R project file and select open.
\begin{figure}
\includegraphics[width=0.6\linewidth]{images/gitdemo/gitdemo-RStudio_OpenProj_fullpath} \caption{Open a cloned project in RStudio}\label{fig:unnamed-chunk-6}
\end{figure}
\hypertarget{cloning-using-the-rstudio-wizard}{%
\subsubsection{Cloning using the RStudio
wizard}\label{cloning-using-the-rstudio-wizard}}
If that looks like a bit too much text based effort, RStudio offers a
way to clone a repository with it's New project wizard. To do this
navigate the menu bar to \textbf{File \textgreater{} New
Project\ldots{}}, select \textbf{Version Control} and then Git. This
opens up a dialogue box to enter the repository url and select where to
save it. As with the git BASH version, copy and paste your remote repo
URL here and set a directory where you want it saved on your laptop.
\begin{figure}
\includegraphics[width=0.6\linewidth]{images/gitdemo/gitdemo-RStudio_OpenProj_wizard} \caption{Clone a project using the RStudio git wizard}\label{fig:unnamed-chunk-7}
\end{figure}
\hypertarget{a-note-on-local-repository-clones-and-onedrive}{%
\paragraph{A note on local repository clones and
OneDrive}\label{a-note-on-local-repository-clones-and-onedrive}}
\begin{quote}
Note that saving a repository within your OneDrive folder structure can
cause some awkward issues. If you use git to perform version control on
a repository saved within a OneDrive folder, you may start receiving
warning messages that large numbers of files have been removed from
OneDrive. In addtion, it can put a heavy burden on your internet
connection as OneDrive tries to keep up with changes to the files
managed by git. Best practice therefore is to store your repositories
somewhere outside your OneDrive file structure. We recommend creating a
\texttt{repos} directory within your base User directory
(i.e.~\texttt{C:\textbackslash{}Users\textbackslash{}\textless{}USERNAME\textgreater{}\textbackslash{}repos\textbackslash{}}.
Windows sometimes tries to make it awkward for you to navigate to places
on your laptop outside of the OneDrive folders, so a useful tip is to
add your \texttt{repos} folder to your Quick access list in File
Explorer.
\end{quote}
\hypertarget{controlling-packages-with-renv}{%
\subsection{Controlling packages with
renv}\label{controlling-packages-with-renv}}
Now that you've each got a clone of the repository, it's useful to
understand a little bit about environment control. Using R as the
specific example, any app or pipeline that you develop will have
packages that it depends on. If someone wants to use your code, they
need to know what packages are involved, whilst it can also be helpful
for them to be able to use the same versions of those packages in order
to recreate exactly what you intended the code to be able to do.
To manage this, we can use an R package called \texttt{renv}. This
manages the R environment for you, helping you keep track of your
repositories required R packages and their versions. In a brand new
empty project, you'd initiate \texttt{renv} with the command
\texttt{renv::init()}. In this case, the template already has
\texttt{renv} initiated. So instead we can tell renv to check the
necessary packages and install them for us. You can do this by entering
the following command in the R console:
\begin{verbatim}
renv::restore()
\end{verbatim}
This should go through \texttt{renv}'s list of the required packages and
install them all in one go (note that you'll need to be connected to the
internet for it to retrieve any packages that aren't already installed).
Once you've done this, the contents of the repository should run as
intended. If at some point you install an extra package for the
repository, you can add this to \texttt{renv}'s package list by entering
the command \texttt{renv::snapshot()} into the R console.
\hypertarget{running-the-dashboard-template-locally}{%
\subsection{Running the dashboard template
locally}\label{running-the-dashboard-template-locally}}
Once you've cloned the package and run \texttt{renv::restore}, you
should then be able to run the Shiny dashboard template. To do this,
either a) enter \texttt{shiny::runApp()} in the R console or b) open up
the file \textbf{ui.R} and then click \textbf{run App} to the top right
of the editor pane. The dashboard should then load either in an RStudio
window or in your default web browser (depending on your chosen settings
in RStudio).
\begin{figure}
\centering
\includegraphics{images/gitdemo/gitdemo-ShinyAppTemplate.PNG}
\caption{The DfE R-Shiny template loaded locally from RStudio}
\end{figure}
Although this workshop is primariy a chance to practice using git,
you'll need a little understanding of the components of an R-Shiny app
as well. A single app generally consists of 3 key scripts:
\emph{global.R}, \emph{ui.R} and \emph{server.R}. Each of these fulfills
a specific purpose as follows:
\begin{itemize}
\tightlist
\item
\emph{global.R}: contains variables and functions required by the rest
of the app (e.g.~reading in data, connecting to a database);
\item
\emph{ui.R}: contains code to create the app's user interface
(e.g.~arranging the layout, placement of charts, tables and text);
\item
\emph{server.R}: contains the functions that make the app responsive
(e.g.~rendering tables and charts and updating dynamic text and input
fields).
\end{itemize}
We'll explain more as needed, but for now that should give you the basic
overview.
\hypertarget{summary}{%
\subsection{Summary}\label{summary}}
In this section, we've covered creating a repository via a template on
GitHub, cloning it to your local drive (using both the BASH terminal and
the RStudio wizard) and getting the necessary packages installed in one
go with \texttt{renv::restore()} via the R console.
In the next section, we'll cover some of the basics of using git to log
changes to your code and sync them between the remote repo and local
copies.
\newpage
\hypertarget{basics-of-git}{%
\section{Basics of git}\label{basics-of-git}}
We'll now take a look at updating repositories with your working. To do
so, we'll follow some example first steps you might take with developing
the template dashboard app to use your own data. The steps we'll follow
will be to create a new branch, add the data in, commit it to the github
log and then push it to the remote (GitHub) repository.
\hypertarget{the-git-log}{%
\subsection{The git log}\label{the-git-log}}
In order to move quickly between different versions of files and code,
git is built around indexing and a log file that track the changes in a
repository. To view the log of any repository, we can simply go into
that repository and run the command \texttt{git\ log} from the BASH
terminal. For example, if you run it in the repository you've created
from the template, you'll get something along the lines of the
following:
\begin{center}\includegraphics[width=0.8\linewidth]{images/gitdemo/gitdemo-gitlog-1} \end{center}
The log shows all ``commits'' that have been made to the repository.
We'll go into making commits in the next section.
\hypertarget{adding-commiting-and-pushing}{%
\subsection{Adding, commiting and
pushing}\label{adding-commiting-and-pushing}}
To adapt the existing template, we want to add some new data. There
should be a data folder in the repository already, so all we need to do
is grab some data and save it there. For this workshop, we'll use a file
from a publication on Explore Education Statistics.
\emph{Again, for this section, just one of your group should run through
the following steps:}
Go to
\href{https://explore-education-statistics.service.gov.uk/data-catalogue/progression-to-higher-education-or-training/2019-20}{the
progression to higher education or training data catalogue} and download
the \textbf{Progression to higher education and training - Local
authority level (csv, 4 Mb)} file, extract the data csv file
(\emph{l4\_tidy\_2020\_all\_la.csv}) and save it into the repo's data
folder.
Now to add this to the git tracking: run the following commands:
\begin{verbatim}
git add .
\end{verbatim}
This searches the repo for any files that have been modified since the
last commit and creates a log of the changes.
\begin{verbatim}
git commit -m "Added data file into repository."
\end{verbatim}
This adds an entry on to the log, updating it with the file changes that
you've just made. Note that the text after the \texttt{-m} is a comment
used to describe the changes to make it easier for someone looking back
from the log to see what changes have happened. Those are the two key
steps for tracking changes to the files and folders in your repository.
Say we decide we're not happy with the data filename, maybe we think the
filename should be more informative for users. We'd now change the
filename as normal (e.g.~to \emph{progress\_he\_la\_2020.csv} using File
Explorer), and then run the commands
\begin{verbatim}
git add data/progress_he_la_2020.csv
\end{verbatim}
and
\begin{verbatim}
git commit -m "Made data filename more informative."
\end{verbatim}
Note that in this instance, we're adding just a single specific file (by
naming the file in the \texttt{git\ add} comand), rather than asking git
to check the whole repo for any updated files (i.e.~by using
\texttt{git\ add\ .}). We've also updated the commit message to be
informative about the changes that we've made. If we now run
\texttt{git\ log} again, we get something along the lines of:
\begin{center}\includegraphics[width=0.8\linewidth]{images/gitdemo/gitdemo-gitlog-2} \end{center}
Here we can see, in reverse order, the commits that have been made, who
made them, when they made them, and the messages that have been recorded
with them.
Finally, it's important to note that what we've done so far is only
being applied to the local copy of the repository (i.e.~the copy on your
laptop). To apply your changes to the remote repository (i.e.~on GitHub
or Dev Ops), you need to ``push'' the changes. This can be done a couple
of different ways: a) in the terminal type \texttt{git\ push} or b) on
the toolbar in the git tab in RStudio press the green up button! Once
you've done this, open up a browser and go to your remote repository on
GitHub and you should now see the data file stored there.
\hypertarget{pulling-from-the-remote-repository}{%
\subsection{Pulling from the remote
repository}\label{pulling-from-the-remote-repository}}
Now that you've made changes, the rest of your team can update their own
local copies of the repository with your updates by pulling from the
remote. Similarly to pushing, they can do this by either a) typing
\texttt{git\ push} in the BASH terminal or b) pressing the down arrow in
the toolbar of the git tab in RStudio.
\hypertarget{summary-of-git-basics}{%
\subsection{Summary of git basics}\label{summary-of-git-basics}}
We've quickly tried out a quick cycle of adding and committing, which is
used to log changes into the local repository and then we've pushed and
pulled to and from the remote repository and local copies on different
laptops. The table below gives a summary of the relevant commands in the
BASH terminal and the corresponding buttons in the RStudio git panel.
\begin{longtable}[]{@{}
>{\raggedright\arraybackslash}p{(\columnwidth - 4\tabcolsep) * \real{0.0968}}
>{\raggedright\arraybackslash}p{(\columnwidth - 4\tabcolsep) * \real{0.3763}}
>{\raggedright\arraybackslash}p{(\columnwidth - 4\tabcolsep) * \real{0.5269}}@{}}
\toprule()
\begin{minipage}[b]{\linewidth}\raggedright
Process
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
git BASH
\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
RStudio git panel
\end{minipage} \\
\midrule()
\endhead
Add & \texttt{git\ add\ .} & Stage using tickbox next to each modified
file. \\
Commit & \texttt{git\ commit\ -m\ "Commit\ message."} & ``Commit''
button in toolbar. \\
Push & \texttt{git\ push} & Green up arrown in toolbar. \\
Pull & \texttt{git\ pull} & Blue down arrow in toolbar. \\
View the log & \texttt{git\ log} & Clock icon in toolbar. \\
\bottomrule()
\end{longtable}
\newpage
\hypertarget{working-collaboratively-with-git}{%
\section{Working collaboratively with
git}\label{working-collaboratively-with-git}}
Git only really makes proper sense once multiple people start working on
a project collaboratively. Solo working, git is useful for version
control and syncing your work to a remote repository site like Dev Ops
and GitHub, but may not feel like it offers all that much more beyond
that. Once we start working collaboratively however, the benefits of
using git (alongside GitHub or Dev Ops) become more apparent. We'll now
look further into this with some worked examples.
\hypertarget{branches-and-splitting-tasks}{%
\subsection{Branches and splitting
tasks}\label{branches-and-splitting-tasks}}
\hypertarget{task-management}{%
\subsubsection{Task management}\label{task-management}}
One useful management tool that we can use from GitHub is the Issues
tab. Here we can create individual tasks, assign them to team member and
then create new \textbf{branches} from those tasks. You can think of
\textbf{branches} as self contained copies of the repository that can
contain complementary or conflicting differences with all other
\textbf{branches} in the repository. These allow you to work on
different multiple tasks on your code independently of any other changes
you might be making. Bringing these different \textbf{branches} or tasks
together is then managed using \textbf{merges} or \textbf{pull requests}
(PRs).
We'll demonstrate this by performing 3 related tasks on 3 different
branches. Each task should be done by only one of your group, so split
the following 3 tasks between your group and each follow the relevant
instructions in the subsections below:
\begin{itemize}
\tightlist
\item
1a) Reading in the data
\item
1b) Updating references in the server script and the user interface
\item
1c) Creating a chart
\end{itemize}
Once you've decided who's doing what, each of you should jump to the
relevant section below. And remember that you're not working in
independent silos here, what you do can impact what other people are
doing so communication needs to happen along the way.
\hypertarget{task-1a---reading-in-the-data}{%
\paragraph{Task 1a - Reading in the
data}\label{task-1a---reading-in-the-data}}
This task underpins the next two. You need to read in the data file that
we've already downloaded. Start by creating a new branch: go to the BASH
terminal, and type:
\begin{verbatim}
git checkout -b featReadData
\end{verbatim}
This creates and moves you into a new branch called featReadData.
Anything you do here has no affect on the contents of the main branch
until you explicitly tell git to merge your changes. Note that this only
creates the branch in your local copy of the repository, for it to be
added to the remote, you'll need to push:
\begin{verbatim}
git push --set-upstream origin featReadData
\end{verbatim}
Note that if you only enter \texttt{git\ push} without the
\texttt{set-upstream} flag, git will suggest the full command for you.
Now to write the code in this branch that's going to read in the data.
We'll read it in to a data frame and the first task is to choose a name
for the data frame and let the rest of the team know, as they'll need
that info (in the example below, we're calling it
\texttt{dfProgressHE}). If you've got the R expertise to do this
already, then feel free to go for it, otherwise just follow the
following steps below.
Open up the script \texttt{R/read\_data.R} and change the code from:
\begin{verbatim}
read_revenue_data <- function(file){
dfRevenue <- read.csv(file)
colnames(dfRevenue)[1] <- "time_period"
dfRevenue <- dfRevenue %>%
mutate(year = as.numeric(paste0("20",substr(format(time_period),5,6))),
area_name=case_when(geographic_level=='National' ~ country_name,
geographic_level=='Regional' ~ region_name,
TRUE ~ la_name))
return(dfRevenue)
}
\end{verbatim}
to:
\begin{verbatim}
funcReadData <- function(file){
dfData <- read.csv(file)
dfData <- dfData %>%
mutate(year = as.numeric(paste0("20",substr(format(time_period),5,6)))) %>%
filter(geographic_level != "Parliamentary constituency",
characteristic_group=='Total',
institution_type=='Total') %>%
mutate(area_name=case_when(geographic_level=='National' ~ country_name,
geographic_level=='Regional' ~ region_name,
TRUE ~ la_name))
return(dfData)
}
\end{verbatim}
You've made a substantive change, so let's run a quick add, commit and
push in the BASH terminal:
\begin{verbatim}
git add .
git commit -m "Added function to read data in data."
git push
\end{verbatim}
We've made a function to read in the data, but we're not running that
function anywhere. So now open up \emph{global.R} and change the line:
\begin{verbatim}
dfRevBal <- read_revenue_data()
\end{verbatim}
to the following:
\begin{verbatim}
dfProgressHE <- funcReadData('data/progress_he_la_2020.csv')
\end{verbatim}
Then change the following lines from:
\begin{verbatim}
choicesAreas <- dfAreas %>%
filter(geographic_level == "National") %>%
select(geographic_level, area_name = country_name) %>%
rbind(dfAreas %>% filter(geographic_level == "Regional") %>%
select(geographic_level, area_name = region_name)) %>%
rbind(choicesLAs)
choicesYears <- unique(dfRevBal$time_period)
choicesPhase <- unique(dfRevBal$school_phase)
\end{verbatim}
to:
\begin{verbatim}
choicesAreas <- dfAreas %>%
filter(geographic_level == "Regional") %>%
select(geographic_level, area_name = region_name) %>%
rbind(choicesLAs)
choicesYears <- unique(dfProgressHE$time_period)
choicesPhase <- unique(dfProgressHE$data_type)
\end{verbatim}
Give it a test, by sourcing \emph{global.R} in the R console:
\begin{verbatim}
source("global.R")
\end{verbatim}
And then have a check of the contents of \texttt{dfProgressHE} (e.g.~by
typing \texttt{dfProgressHE} in the R console). It's also worth having a
look through the contents of the \texttt{choicesAreas},
\texttt{choicesYear} and \texttt{choicesPhase} variables to get an idea
of what they contain.
Once you're happy, then run another
\texttt{add}/\texttt{commit}/\texttt{push} cycle and flag to your team
that you've finished the code to read in the data.
\hypertarget{task-1b---updating-the-user-interface-inputs}{%
\paragraph{Task 1b - Updating the user-interface
inputs}\label{task-1b---updating-the-user-interface-inputs}}
Whilst task 1a is updating the data that's been read in, this task aims
to tweak some of the code to make sure the dashboard still works. As
we're no longer working with the same data, we've changed the name of
the data frame holding it and as well some of the column names will be
different.
In this task, we'll create a new branch using GitHub. Open up the
repository on GitHub in your web browser and select the Issues tab. You
should see something similar to shown below.
\begin{figure}
{\centering \includegraphics[width=0.92\linewidth]{images/gitdemo/gitdemo-GitHub-Issues}
}
\caption{The Issues panel in GitHub. This can be used to keep track of jobs that need doing on a repository and create new branches linked to individual jobs.}\label{fig:unnamed-chunk-10}
\end{figure}
On the issues tab, select \emph{New issue} and then click \textbf{Get
started} alongside \textbf{Feature request}. This will open a new issue
panel, with boxes to put in some details about the feature being
requested. Let's put in a title of \emph{``Update inputs for new
data''}. You'll see some handy guidance on what information to enter for
an issue. Have a quick read of this, but then for brevity here, let's
ignore it and put in a short description along the lines of ``The data
file being read in has been updated and the inputs needed updating to
match the column headers etc.''. You've got options to add assign this
task and add labels, link to projects and milestones. We don't need much
of that, but maybe add yourself as the assignee. When you're ready,
click \textbf{Submit new issue}.
After you've done so, you'll see you now have the option in the right
hand side panel to create a new branch linked to this issue. Select that
and choose to check out the branch locally as the next option. Once
you've confirmed you'll get some code to use to checkout the branch
locally, which you can copy and use in the terminal in RStudio. It
should look something like this:
\begin{verbatim}
git fetch origin
git checkout 1-update-inputs-for-new-data
\end{verbatim}
Run that in the \texttt{BASH} terminal in RStudio and you'll have your
new branch ready to go.
\begin{figure}
{\centering \includegraphics[width=0.64\linewidth]{images/gitdemo/gitdemo-GitHub-Issues-NewBranch}
}
\caption{Once you've created an Issue on GitHub, you can create a new branch from the GitHub webpage and then fetch it to work on locally on your laptop.}\label{fig:unnamed-chunk-11}
\end{figure}
So we've got a few small changes just to update here given the changes
to the input data. Whoever's performing Task 1a, should be updating the
data frame containing the new input data to be called
\texttt{dfProgressHE} to reflect the different data that's being read
in. The main changes that are needed are in the \emph{server.R} and
\emph{R/dashboard\_panels.R}.
Open up \emph{server.R} and replace any instances of \texttt{dfRevBal}
with \texttt{dfProgressHE} and any instances of \texttt{school\_phase}
with \texttt{data\_type} (which references a specific column within the
data frame).
Then in \emph{R/dashboard\_panels.R}, update the titles of the input
boxes from:
\begin{verbatim}
selectizeInput("selectPhase",
"Select a school phase",
choices = choicesPhase
\end{verbatim}
to:
\begin{verbatim}
selectizeInput("selectPhase",
"Select an indicator",
choices = choicesPhase
\end{verbatim}
And that should be it for this task. All that's left is to commit and
push your changes. If you've got a preferred way already to perform
commits, then go for it. If not then let's use the RStudio git panel.
\begin{figure}
{\centering \includegraphics[width=0.64\linewidth]{images/gitdemo/gitdemo-RStudio-gitpanel}
}
\caption{Staging files in the RStudio git panel.}\label{fig:unnamed-chunk-12}
\end{figure}
Firstly click on the git tab in the top right of RStudio to show the git
panel (see the screenshot below). Next click the tickboxes next to the
files with changes (i.e.~these should be server.R and
R/dashboards\_panels.R) to \textbf{stage} (aka \textbf{add}) the files.
Now click \textbf{commit}, add a commit message in the relavent text box
and then hit \textbf{commit} in the bottom right corner of the window.
Assuming that all went through without any issues, you can now press the
green up arrow in the git panel to \textbf{push} your changes to the
remote repository on GitHub.
Let the others know that you're done!
\hypertarget{task-1c---creating-a-chart}{%
\paragraph{Task 1c - Creating a
chart}\label{task-1c---creating-a-chart}}
There's already an example chart in the template, so let's re-wire that
as the basis for our demo chart here. Let's start by creating a new
branch in RStudio. With your repo open (and pulled with the latest
changes) in RStudio, click the new branch button in the git panel
toolbar. This will open a dialogue box as shown here in which you can
enter a new branch name. Enter a name (e.g.~\emph{featTimeSeriesChart}),
make sure syncing to the remote is selected and press \emph{Create}.
\begin{figure}
{\centering \includegraphics[width=0.64\linewidth]{images/gitdemo/gitdemo-RStudio_NewBranch}
}
\caption{The new branch dialogue box in RStudio is opened using the purple new branch icon in the git toolbar.}\label{fig:unnamed-chunk-13}
\end{figure}
Now in this new branch, you can make updates that won't effect any other
branches until you choose to merge this branch with another one.
To keep the code for the dashboard organised, we've split it into
separate scripts and functions in different files. The main code for
defining the line chart in the template is contained in
\texttt{R/plotting.R}. Open that script now and change the lines:
\begin{verbatim}
createAvgRevTimeSeries <- function(dfRevenueBalance,inputArea){
ggplot(dfRevenueBalance, aes(x=year,y=average_revenue_balance,color=area_name)) +
\end{verbatim}
to:
\begin{verbatim}
createTimeSeries <- function(df,inputArea){
ggplot(df, aes(x=year,y=cohort,color=institution_group)) +
\end{verbatim}
Then we need to update the code in \emph{server.R} to reflect these
changes to the plot, so update:
\begin{verbatim}
ggplotly(createAvgRevTimeSeries(reactiveRevBal(), input$selectArea)) %>%
\end{verbatim}
to
\begin{verbatim}
ggplotly(createTimeSeries(reactiveRevBal(), input$selectArea)) %>%
\end{verbatim}
This tells the plot to now take the variable \texttt{cohort} as the
value to plot on the y-axis and \texttt{institution\_group} as a filter
on which to colour the output.
And that should be it for this task. All that's left is to commit and
push your changes. If you've got a preferred way already to perform
commits, then go for it. If not then let's use the RStudio git panel.
\begin{figure}
{\centering \includegraphics[width=0.64\linewidth]{images/gitdemo/gitdemo-RStudio-gitpanel}
}
\caption{Staging files in the RStudio git panel.}\label{fig:unnamed-chunk-14}
\end{figure}