Computational_Econ/Class4/Slides/Best_Practice.tex
2025-09-03 12:45:27 -06:00

172 lines
5.3 KiB
TeX
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

\documentclass{beamer}
\usepackage{graphicx}
\usepackage{multicol}
\usepackage{hyperref}
\usepackage{lipsum} % for placeholder text
\graphicspath{{pdf_images/}}
\title{ECON 4530/5530 \\ Computational Economics}
\subtitle{Best Practice of Coding}
\author{Alex Gebben}
\begin{document}
% Title Slide
\begin{frame}
\titlepage
\end{frame}
%%%%%%%%%%%%%%%%%%
\begin{frame}{What problems have you run into?}
\begin{centering}
\begin{itemize}
\onslide<2->{\item Working directory location}
\onslide<3->{\item Backslash syntax \texttt{\textbackslash} vs \texttt{/}}
\onslide<4->{\item What data comes loaded on start?}
\onslide<5->{\item Forgetting what the code was for}
\onslide<6->{\item Need to reload from scratch}
\onslide<7->{\item What to do when the code fails}
\end{itemize}
\end{centering}
\end{frame}
\begin{frame}{Work flow}
\only<1-6>{
At some point you will:
\begin{itemize}
\onslide<2->{\item Reload an old project}
\onslide<3->{\item Share code with others}
\onslide<4->{\item Run multiple projects at once}
\onslide<5->{\item Need to load and export data}
\onslide<6->{\item Need to reproduce a result}
\end{itemize}
}
\only<7>{\huge Let's make life easier (and harder)}
\only<8>{
% Top half: two columns
\vspace{-0.5em}
\begin{columns}[T,onlytextwidth]
\column{0.5\textwidth}
\includegraphics[width=\textwidth]{clean-slate}
\column{0.5\textwidth}
\textbf{Never save a R session}
\begin{itemize}
\item{You wont get tangled by lines out of order}
\item{Makes code reproducible}
\item{You can update results more easily}
\end{itemize}
\end{columns}
}
\only<9->{
Alternatively you can run code to clear all data at the start of each session \texttt{\\usethis::use\_blank\_slate()} but you will need to install the library \texttt{"usethis"}:
\begin{enumerate}
\item \texttt{install.packages("usethis")}
\item \texttt{library("usethis")}
\end{enumerate}
}
\end{frame}
%%%%%%%%%%%%%%
\begin{frame}{R studio Projects}
\only<1-5>{
If you are using R studio it is best practice to create a project file.
\begin{itemize}
\onslide<2->{\item Sets the working directory.}
\onslide<3->{\item Allows all code to use relative paths.}
\onslide<4->{\item \emph{Advanced:} Can store all packages with the library renv.}
\onslide<5->{\item \emph{Advanced:} Can use code control with git (more on that later).}
\end{itemize}
}
\only<6>{ \includegraphics[width=0.9\textwidth]{new-project}}
\only<7->{
\begin{columns}[T,onlytextwidth]
\column{0.6\textwidth}
\begin{center}
\includegraphics[width=\textwidth]{Project_file.png}
\includegraphics[width=0.5\textwidth]{Project_file_Contents.png}
\end{center}
\column{0.4\textwidth}
\begin{enumerate}
\item Creates a .Rproj file.
\item Open this file from R.
\item Saved paths
\end{enumerate}
\end{columns}
}
\end{frame}
%%%%%%%%%%%%%%%%%%
\begin{frame}{Class Exercise}
\huge
Download relevant data
\normalsize
It is good practice to store the data is a separate folder in the location of the Rscript. Such as a "Data" folder.
\end{frame}
\begin{frame}{File Management}
\begin{itemize}
\onslide<1->{\item Create directories for data}
\onslide<2->{\item Create Directories for scripts}
\onslide<3->{\item Make code do most of the cleaning}
\onslide<4->{\item Clear naming of files}
\onslide<5->{\item Document all files with a Readme file}
\end{itemize}
\end{frame}
\begin{frame}{Create Directories}
\begin{itemize}
\onslide<1->{\item Create directories for data}
\onslide<2->{\item Create Directories for scripts}
\onslide<3->{\item Clear naming of files}
\onslide<4->{\item Document all files with a Readme file}
\end{itemize}
\end{frame}
\begin{frame}{Manage Directories}
\only<1-14>{
\begin{columns}[T,onlytextwidth]
\column{0.5\textwidth}
\textbf{Usefull Functions}
\begin{itemize}
\onslide<2->{\item \texttt{create.dir()}}
\onslide<3->{\item\texttt{dir.exists()}}
\onslide<4->{\item\texttt{file.exists()}}
\onslide<5->{\item\texttt{list.files()}}
\onslide<6->{\item\texttt{dir.create()}}
\end{itemize}
\column{0.5\textwidth}
\onslide<7->{\textbf{Saving Data}
\begin{itemize}
\onslide<8->{\item \texttt{write\_csv()}}
\onslide<9->{\item \texttt{write\_excel\_csv()}}
\onslide<10->{\item\texttt{write\_delim()}}
\onslide<11->{\item\texttt{saveRDS()}}
\onslide<12->{\item\texttt{ggsave()}}
\onslide<13->{\item\texttt{write.csv()}}
\end{itemize}
}
\end{columns}
\vspace{1.5em}
\onslide<14>{Many other options in R review \texttt{help()} and the internet.}
}
\only<15>{\includegraphics[width=\textwidth]{DIR.png}}
\only<16>{\includegraphics[width=\textwidth]{DIR2.png}}
\end{frame}
\begin{frame}{Naming Files}
% \href{https://r4ds.hadley.nz/workflow-scripts.html#fn2'}{For an example DONT LEAVE AS A LINK FOR MY REFRENCE}
It might be tempting to name your files code.R or myscript.R, but you should think a bit harder before choosing a name for your file. Three important principles for file naming are as follows\:
File names should be machine readable\: avoid spaces, symbols, and special characters. Dont rely on case sensitivity to distinguish files.
File names should be human readable\: use file names to describe whats in the file.
File names should play well with default ordering\: start file names with numbers so that alphabetical sorting puts them in the order they get used.
\end{frame}
\end{document}