Go back to fan’s Tex4Econ and Miscellaneous Repository.

1 Objective

Install various compilers and IDE:

  1. Python (Anaconda, Pycharm + VSCode + Atom, Poetry)
  2. R (VSCode + Atom)
  3. Matlab
  4. Latex and PDF (texlive, Okular, VSCode + Atom + Texstudio)

Test at the end:

  1. Do sample py files work?
    • work from command line
    • work from atom (hydrogen), Pycharm, VSCode
  2. Do sample rmd files work?
    • work from r-studio
    • work from atom (hydrogen), VSCode
  3. Do tex templates compile?
    • tikz files, template files with bib
    • work from texstudio, VSCode live compile, Atom

Installation Plan outline:

  1. Conda install (python)
  2. Install Atom along with Hydrogen and other packages.

2 Python Testing and Installation with Poetry and Docs

When to use Python:

  1. Main use Python for Dynamic programing type tasks
  2. All immediate data tasks should be done in R, including things like generating data structures. Also graph generation in R.
  3. Certain basic tabulation summary statistics etc things done in Stata

Two types of python usage cases:

  1. Small chunks of python code in RMD for tutorial/class files, combine files in book format as bookdown.
  2. Python installable packages and modules created with poetry, push to github as pypi: Locally test specially functions in poetry environment. Push to github and Pypi, and then install via pip for system wide use.

Installations:

  1. Python package development in poetry virtual environment, install with needed dependencies.
    • This means, during python package development, do not install the packages needed directly in conda, but add as dependency in Poetry
  2. General environment with pip installed packages and conda installed path discoverable pathon packages for rmd chunks.

2.1 Poetry

Install Python via Anaconda first following other instructions. Possibly cleanly uninstall and reinstall. Use Poetry to build python packages and modules.

For development machines, install Poetry and nstall sphinx.

# poetry
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
# sphinx
pip install sphinx

Theme for sphinx

pip install sphinx-rtd-theme
pip install sphinx_bootstrap_theme

Initialize a Poetry-style Python Package and add Sphinx style docs. This creates the folder ~/pyfan and initialize it with various:

# creates ~/pyfan as poetry packaging
cd ~
poetry new pyfan

# sphinix quickstart
cd ~/pyfan
mkdir docs
sphinx-quickstart
make html

# build empty package
rm -rf dist/ build/
poetry install
poetry build
poetry publish

# What is the virtual Env id
poetry env info

2.1.1 Installating Poetry Created Packages

Normally install/test/use inside conda virtual env or poetry virtual env. Poetry would install with poetry add and poetry install. After seeing some package of interest on github etc, see if the package is on pypi. pip install from pypi if possible.

Local Install

pip install --no-index --find-links="C:/Users/fan/Py4Econ/dist/pyfan-0.0.0.900.tar.gz" pyfan --ignore-installed

Pypi Install

Open up conda prompt, pip install from base. Will install dependencies of package that are in the poetry env but not otherwise in python installation main path.

pip install git+https://github.com/fanwangecon/pyfan.git#egg=pyfan
pip install pyfan --ignore-installed
pip show pyfan

Sometimes, someone ports a package and adds certain things that I need, so need to install from git a package whose modified version is not on pypi, then use git+https

2.1.1.1 What are egg, tar.gz, whl?

poetry creates tar.gz and whl in the dist folder, what are they?

  • tarballs: tar.gz

2.1.2 Developing and testing new tools Pycharm + Poetry

When tesing and developing tools, do so in the poetry virtual environment for the package in which testing is taking place.

After a package has been created and poetry built and poetry publish:

  1. Type in poetry env info, and copy for example path: C:/Users/fan/AppData/Local/pypoetry/Cache/virtualenvs/py4econ-6xnVtByB-py3.7, to pycharm, so that can test module in pycharm using the poetry generated isolated packaging testing env.
  2. In Pycharm: Settings (ctrl+alt+s), Project, Python interpreter.
  3. Click on the cog button, “Add” > Right menu, “Virtual Environement” > Existing environement > Three dots > paste the path and point it to the python.exe in Scripts
poetry env info

When a particular tool is needed to build this new module inside package, add the tool via poetry add as a dependency. If a module requires another dependency: poetry add numpy, if adding numpy. This will install in the virtual env numpy. When packages are added, they are automatically installed. When the the pyfan package repo is downloaded somewhere, the dependencies might not be installed yet, and poetry install would install them.

poetry add numpy

2.1.2.1 Development for pyfan

pyfan is the general python tools package for various projects, not economics related directly.

Poetry added dependencies are in /pyfan/pyproject.toml, Poetry add:

cd "C:/Users/fan/pyfan"
cd ~/pyfan
poetry env info

All installations:

poetry remove python-frontmatter pyyaml
poetry add python-frontmatter pyyaml
poetry add numpy
poetry add scipy

Poetry modules added over time:

# 2020-04
poetry add pyyaml
poetry add python-frontmatter

After modules are added and new programs developed

sphinx readthedocx

cd ~/pyfan
cd "C:/Users/fan/pyfan"
cd "C:/Users/fan/pyecon"
poetry install

rm -rf dist/ build/ docs/
mkdir docs
cd docs/
sphinx-build -b html ../ _build
<!-- make html -->
sphinx-build -b latexpdf ../ _build
make latexpdf

poetry build
poetry publish

On some local computer install pyfan:

pip show pyfan
# conda activate pyfan
pip install pyfan
pip install git+https://github.com/fanwangecon/pyfan.git#egg=pyfan
ipython
import pyfan.util.rmd.bookdownparse as bookdownparse
?bookdownparse.fs_yml2readme
2.1.2.1.1 Uninstall

uninstall packages in the overall environment

# list all installed packages
pip list
pip show pyyaml
pip show python-frontmatter
pip show pyfan
# unstall packages
pip uninstall pyyaml
pip uninstall python-frontmatter
2.1.2.1.2 pyfan workflow and files added

New python functions are added, or new Rmd files are added with python exampls.

List of to dos and checks.

New rmd files:

  1. Edit yaml to include functions used
  • Go over each part, add to short desc
  • In each section, add code usage
  1. Link to py functions if implemented there
  2. Run file internally
  3. Add new files to *_bookdown.yml*
  4. Run pyfan rmd compile
  5. Overall bookdown check pdf and html

New py functions:

  1. Document with function name, parameters and returns
  2. update version number
  3. Test run the function file’s main
  4. Local sphnix text etc
  5. Poetry publish
  6. Git push
  7. Check Docs on readthedocs
  8. Uninstall and install locally
  9. Uninstall and install in wk_pyfan envir
cd "C:/Users/fan/pyfan"
poetry install

rm -rf dist/ build/ docs/
mkdir docs
cd docs/
sphinx-build -b html ../ _build

poetry build
poetry publish
pip show pyfan
<!-- pip install pyfan -->
pip uninstall pyfan -y
pip install pyfan
pip install git+https://github.com/fanwangecon/pyfan.git#egg=pyfan

conda activate wk_pyfan
pip uninstall pyfan -y
pip install pyfan
pip install git+https://github.com/fanwangecon/pyfan.git#egg=pyfan
conda deactivate

2.2 sphinx and readthedocs

readthedocs is logged in via github account. Automatically recognizes github repositories, and can import repositories to generate documentation that it can.

  1. install sphinx
pip install sphinx
cd ~/Py4Econ
mkdir docs
sphinx-quickstart
make html
  1. install sphinx packages
pip install sphinx-gallery

2.2.1 autodoc

Documenting Python Packages with Sphinx and ReadTheDocs

The Napoleon extension for Sphinx allows for NumPy/Google style docstrings instead of using the hard-to-read reStructuredText in your docstrings

The autodoc extension for sphinx can automatically generate API reference doc pages from the docstrings in your python code. Python docstrings are string literals which occur immediately after function or class definitions. They’re treated as comments, and it’s customary to document a function or class in its docstring. The autodoc extension allows you to include reStructuredText in your docstrings, and will build an API reference automatically for your module from the docstrings, while allowing for further customization.

To enable the autodoc extension, in your conf.py file, add the string ‘sphinx.ext.autodoc’ to the extensions list (this should have already been done if you answered yes to enabling the autodoc extension during sphinx-quickstart).

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))

After enabling autodoc, in the documentation’s .rst files, you can use the automodule directive:

.. automodule:: package_name.module :members:

.. autosummary:: :toctree: _autosummary

fantools.utility fantools.utility.inout fantools.utility.inout.bookdownparse fantools.utility.inout.rmdparse fantools.utility.timer

2.2.2 rst

= for page titles, - for sections, ^ for subsections, and " for subsubsections.

Bulleted lists use asterisks, numbered lists use numbers, and nesting is done with indentation, like Markdown (though nested lists need to be surrounded by blank lines):

  • Bulleted

  • list

    • nested
    • list
  • con’t

  1. Numbered
  2. list

Code sample

For hyperlinks, use:

Link text <http://www.link-address.com>_

Commented lines begin with two periods and a space (like a directive w/o the double colons):

.. I’m a comment! And multiline comments are also like directives w/o the colons:

.. I am a multiline

comment

2.3 Python, Poetry, Docs, Git

Start from without a package. Create two packages: pyecon, pyfan

  1. generate poetry repos first, before adding in .git with pull_repos
cd ~
poetry new pyfan

cd ~
poetry new pyecon

base ~/base/bin/pull_repos

cd ~/pyfan
cd "C:/Users/fan/pyfan"
mkdir docs
<!-- change release number conf.py and .toml -->
<!-- sphinx-quickstart -->
make html
poetry build
poetry publish

cd ~/pyecon
cd "C:/Users/fan/pyecon"
mkdir docs
poetry build
poetry publish
sphinx-quickstart
make html

poetry env info
<!-- get virtual env info  -->

install git locally so can use outside of env

pip install git+https://github.com/fanwangecon/pyfan.git#egg=pyfan pip show pyfan

Name: pyfan Version: 0.1.2 Summary: Home-page: None Author: Fan Wang Author-email: License: None Location: c:-packages Requires: Required-by:

import pyfan.util.rmd.rmdparse as ft_rmdparse
ft_rmdparse.fs_rmd_yml_parse()

import pyfan.util.rmd.bookdownparse as ft_bookdownparse
sfc_prj = 'R4Econ'
sph_prj = 'C:/Users/fan/R4Econ/'
spn_prj_bookdown_yml = '_bookdown.yml'
spn_prj_readme_toc = 'README_toc_three.md'

ft_bookdownparse.fs_yml2readme(sfc_prj=sfc_prj, sph_prj=sph_prj,
                          spn_prj_bookdown_yml=spn_prj_bookdown_yml,
                          spn_prj_readme_toc=spn_prj_readme_toc)

3 Python and Anaconda Installation

Use conda across platforms, so that locally on windows and ubuntu and remotely on aws, can have the same software setup environment.

conda list anaconda
python -V

3.1 Anaconda Install

3.1.1 First Time Conda Install

Download Anaconda Python 3 and install for all users. Afterwards, Anaconda does not automatically get added to Windows Path. Need to use Anaconda Prompt to access programs. To access Anaconda packages from windows prompt, from git bash, from R, etc, need to Add Anaconda to Windows Path.

To install conda in Linux/Debian, follow these instructions. Overall, installations are very similar.

In command/anaconda prompt:

# To remove conda Fully
rm -rf ~/anaconda3

where anaconda
# C:/ProgramData/Anaconda3/Scripts/anaconda.exe
where python
# C:/ProgramData/Anaconda3/python.exe
where jupyter
# C:/ProgramData/Anaconda3/Scripts/jupyter.exe
where jupyter-kernelspec
# if R installed already
where r

###############################
# Add these to Windows PATH:
###############################
# C:\ProgramData\Anaconda3
# C:\ProgramData\Anaconda3\Scripts
# C:\ProgramData\Anaconda3\Scripts\bin

To Add Anaconda to Path, In Windows 1. Search for: Environment Variables 2. Edit Environment Variables 3. Add new to Path (lower half): - C:/ProgramData/Anaconda3/ - C:/ProgramData/Anaconda3/Scripts/ - C:/ProgramData/Anaconda3/Scripts/bin
4. Now open up regular windows command Prompt, Type in: - conda –version - also Close and Open up Git Bash: conda –version

3.1.2 Conda Update, Install, Channels

Open up Anaconda Navigator, it will update navigator automatically. If there are errors, might have to clean first.

There are different channels/repositories from which packages could be installed: conda, conda-forge, for example. Can change where to look for packages first. Many packages are in multiple channels. Conda-forge often have mroe recent packages.

# if there are bugs
# conda clean --packages
# use conda-forge as main channel, more updated packages
conda config --get channels
conda config --add channels conda-forge
conda config --get channels
# remove conda-forge from channels (do so for main env install)
conda config --remove channels conda-forge
# normal update
conda update --all

3.1.3 Conda Environments

For testing installed packages. Note that for development, using poetry for testing package dependencies for package development, and associate pycharm for projects with poetry virtual environments. The idea is that there might be dependency problems etc for packages depending on where/when they were made, might not work in the overall conda environment where pyfan and pyecon, my main tools are installed. Some specific task might only need one particular package. And when working on these, use a conda environment. When this works, could incorporate whatever I am working on into a pyfan, pyecon package along with other packages.

conda list env
conda env remove -n test_package_name
conda create -n test_package_name -y
conda activate test_package_name
conda list
conda deactivate

conda install pip

# see if package exists on pip and install from pip
pip install smop

3.1.3.1 Conda Perl Environment

To run some perl tools in an isolated environment:

  1. latexpand
# Set up
conda deactivate
conda list env
conda env remove -n wk_perl
conda create -n wk_perl -y
conda activate wk_perl
conda install -c anaconda perl -y
conda list
conda deactivate

# intall packages in virtual env
cd "C:\Users\fan\.conda\envs\wk_perl\"
cd "C:\ProgramData\Anaconda3\envs\wk_perl\"
git clone https://gitlab.com/latexpand/latexpand.git

# use latexpand
conda activate wk_perl
cd "C:\Users\fan\Documents\Dropbox (UH-ECON)\Project Emily Minority Survey\Drafts_RR\OverleafChinaSchoolClosure"
perl "C:\Users\fan\.conda\envs\wk_perl\latexpand\latexpand" draft_edcc_submit_rr.tex > draft_edcc_submit_rr_flat.tex

3.1.3.2 Conda Jinja Environment

For testing and packaging latex files. In particular, single folder latex files for final paper submissions.

# Set up
conda deactivate
conda list env
conda env remove -n wk_jinja
conda create -n wk_jinja -y
conda activate wk_jinja
conda install -c anaconda jinja2
conda install -c anaconda pyyaml

# use yaml2tex
cd "C:\Users\fan\Documents\Dropbox (UH-ECON)\repos\fanwangecon.github.io\_cv\templating\"
python yaml2tex.py

3.1.3.3 Conda Jekyll Environment (Ruby)

For testing github pages sites. On windows, install ruby using windows-installer. Windows choose install option 3, install toolchain.

# Set up
conda deactivate
conda list env
conda env remove -n wk_ruby
conda create -n wk_ruby -y
conda activate wk_ruby

ruby -v
gem -v 
gcc -v
g++ -v 
make -v 

gem install jekyll bundler

# use yaml2tex
cd "C:\Users\fan\Documents\Dropbox (UH-ECON)\repos\fanwangecon.github.io\_cv\templating\"
python yaml2tex.py

3.1.3.4 Conda revealjs nodejs Environment

revealjs, decktape to pdf conversions

# Set up
conda deactivate
conda list env
conda env remove -n wk_pyfan
conda create -n wk_pyfan -y
conda activate wk_pyfan

# add channel
conda config --env --add channels conda-forge
conda config --get channels
conda config --get channels --env

# yaml
conda install python-frontmatter -y
conda install pyyaml -y

# environment data
conda install cdsapi -y
conda install -c conda-forge eccodes -y

# pyfan install 
pip install pyfan

3.1.3.5 Conda Bookdown for pyfan

To run some perl tools in an isolated environment:

  1. latexpand
# Set up
conda deactivate
conda list env
conda env remove -n wk_perl
conda create -n wk_perl -y
conda activate wk_perl
conda install -c anaconda perl -y
conda list
conda deactivate

# intall packages in virtual env
cd "C:\Users\fan\.conda\envs\wk_perl\"
cd "C:\ProgramData\Anaconda3\envs\wk_perl\"
git clone https://gitlab.com/latexpand/latexpand.git

# use latexpand
conda activate wk_perl
cd "C:\Users\fan\Documents\Dropbox (UH-ECON)\Project Emily Minority Survey\Drafts_RR\OverleafChinaSchoolClosure"
perl "C:\Users\fan\.conda\envs\wk_perl\latexpand\latexpand" draft_edcc_submit_rr.tex > draft_edcc_submit_rr_flat.tex

3.2 Essential Programs to Install

Install Packages, note that some packages are dependencies for fantools and fanecon packages, they would be installed jointly with those packages.

pip install --user --upgrade setuptools wheel
pip install sphinx
pip install fantools --ignore-installed

Previously:

# install additional packages
# linux utilites
# conda install -y m2-base
# stats and graphs
# conda install -y statsmodels datashape seaborn
# various
# conda install -c conda-forge -y interpolation awscli
# aws communicate
# conda install -c anaconda -y boto3

3.3 Fist test python under command line:

# windows
python "C:/Users/fan/PyFan/ProjectSupport/Testing/Numpy/Functions.py"
python "C:/Users/fan/PyFan/ProjectSupport/graph/subplot.py"
# linux
python ~/PyFan/ProjectSupport/Testing/Numpy/Functions.py
python ~/PyFan/ProjectSupport/graph/subplot.py

4 R Installation

R could be installed from Anaconda or directly on windows outside of the Anaconda directories. When installing from within Anaconda, could create R environment that are isolated from the rest of the computer. The R environment could be updated and deleted without disturbing dependencies in conda main or the rest of the computer.

The computer could have multiple R installations. Several in different conda R environment, and also several under windows/linux primary main user R directories. Type which R/where R to see if you open up R from command line at the moment, which directory’s R will be used. Inside Conda R environment, it will be a different R if there is a different R version there.

4.1 Fully Uninstall r

If R was installed in R enviornments, just delete the environment. Otherwise, use system’s uninstaller. Before that, from terminal/command-prompt, type R, and .libPaths() to find paths. Do so inside conda main, outside of conda main, inside different environments, find all paths. After uninstaller finishes, check in the libPath folders to see if there are still stuff there, delete all, delete the folders.

.libPath()
# C:/Users/fan/Documents/R/win-library/3.6
# C:/Program Files/R/R-3.6.1/library

For Linux and for unsintalling inside conda:

# Exit Conda
conda deactivate
# where is R installed outside of Conda
which R
# /usr/bin/R
# To remove all
sudo apt-get remove r-base
sudo apt-get remove r-base-core

# Inside Conda base
conda activate
# Conda r_env
conda activate r_env
# Where is it installed?
which R
# /home/wangfanbsg75/anaconda3/bin/R
conda uninstall r-base

4.2 Install R (outside of Conda)

Need to install R. Then need to install also an editor/IDE for R. Rstudio is the dominant R IDE. VSCode and Atom also works well, especially VSCode as editors. Dramatically better experience for writing scripts outside of Rstudio which feels unwieldy and slow especially compared to VSCode.

Overall plan:

  1. download R
    • for debian: Johannes Ranke. For Linux/Debian installation, crucial to update the source.list to include sources that have more recent versions of R. If not, will get very old R versions that is not compatible with many packages.
    • add R to path for Windows. In Windows Path, add for example: C:/Program Files/R/R-3.6.2/bin/x64/ and C:/Rtools/bin
  2. download R-studio
  3. Open R-studio and auto-detect R
  4. Install additional packages (from commandline)
    • various R data-science packages have dependencies, and additional not-R programs might need to be installed. Install them as needed, for example: libcurl4-openssl-dev and libssl-dev from command prompt first inside Debian.

4.2.1 Linux R Install

For linux/Debian, to Install latest R:

# Go to get latesdebian latest r sources.list
cat /etc/apt/sources.list
# Install this First (should already be installed)
sudo apt install dirmngr

# Debian R is maintained by Johannes Ranke, copied from https://cran.r-project.org/bin/linux/debian/:
apt-key adv --keyserver keys.gnupg.net --recv-key 'E19F5F87128899B192B1A2C2AD5F960A256A04AF'
# Add to source.list, for debian stretch (9)
# sudo su added for security issue as super-user
sudo su -c "sudo echo 'deb http://cloud.r-project.org/bin/linux/debian stretch-cran35/' >> /etc/apt/sources.list"
# if added wrong lines, delete 3rd line
sudo sed '3d' /etc/apt/sources.list

# Update and Install R, should say updated from cloud.r
sudo apt-get update
sudo apt-get install r-base r-base-dev

# Also install these, otherwise r-packages do not install
# libxml2 seems need for tidymodels
sudo apt-get install libcurl4-openssl-dev
sudo apt-get install libssl-dev
sudo apt-get install libxml2-dev

4.2.2 Install Packages for R

What are the packages I need in R:

Use Multiple Languages:

Visualization, Packaging Etc:

From command prompt, enter R by typeing R, and install packages:

# Install RTools First!
# https://cran.r-project.org/bin/windows/Rtools/

# Install tidyverse
install.packages(c("tidyverse", "tidymodels"))

# Install Packaging tools
install.packages(c("devtools", "pkgdown", "roxygen2", "bookdown", "knitr", "kableExtra", "formatR"))

# Install Statistics models
install.packages(c("AER", "minpack.lm"))

# Install Tools to Work with Other Packages
install.packages(c("reticulate", "JuliaCall"))

# Personal Packages
devtools::install_github("fanwangecon/REconTools")
# Can enter R from Command Prompt Conda ENV, much faster than from r-studio
# Install R-tools, RTOOLS is for WINDOWS
# install.packages("installr")
# library("installr")
# install.Rtools()
# c:/Rtools/bin;
# c:/Rtools/mingw_32/bin;
# c:/Rtools/mingw_64/bin;

# Install RTools First!
https://cran.r-project.org/bin/windows/Rtools/

# main packagevps
# tidymodel installation on linux difficult
# install.packages(c("tidyverse", "tidymodels", "tidyr"))
# development packages
# install.packages(c("devtools", "IRkernel", "pkgdown", "roxygen2", "kableExtra"))
# other packages
install.packages(c("AER", "minpack.lm"))
# install.packages(c("AER", "minpack.lm", "knitr", "matlab"))

# for working with other tools
# install.packages(c("reticulate"))

# Install my package, need to prepare package to work for ubuntu
# if install does not work, load locally
devtools::install_github("fanwangecon/REconTools")

# Present
install.packages("revealjs")
# Kernel
# install.packages(c('repr', 'IRdisplay', 'evaluate', 'crayon', 'pbdZMQ', 'devtools', 'uuid', 'digest'))
# IRkernel::installspec()

4.3 Install R (inside Conda)

Key packages are generally available in conda’s default channel (official distribution) and also the more frequently updated conda-forge channel. Prioritize conda-forge to get the latest packages. After adding conda-forge to channels, that will be prioritized, so when creating a new environment, conda will install first from conda-forge if package exists there. Try the r_env generation line with and without first adding conda-forge to channel, and see that the packages installed will have different versions, reflecting versions in the default channel and in the conda-forge channels.

4.3.1 Set up Conda R environment

Create a Conda Environment called r_env, and update its channels.

  • Location: Installed environments are shown appear in the envs folder of C:/ProgramData/Anaconda3/envs/. Can be easily deleted without disrupting the main base installation for conda. This is very important. During installation, could easily run into problems, and need to re-install. Much better to only re-install a folder. Note that when R is installed in envs, it will not show up under add or remove programs for uninstallation from there, has to be uninstalled, deleted directly here.
  • Use only in Env: Note that if we installed r inside r_env, if we type r under the base environment, we can not enter r. We can only enter r within r_env. With PATH properly set-up, this happens under Conda Prompt, Windows Prompt, etc.
  • Multiple Pythons: With the above installation for R, one benefit is that if R requires a different version of Python than what the base environment uses, the r_env could have a different python version. So type python in the base environment as well as inside the r_env. This also means potentially there could be duplicated installations I think. Look at the python versions under conda list in the base and in the r-environment.
  • File Size: Note that this creates a large installation folder, C:/ProgramData/Anaconda3/envs/r_env/, without additional packages, is 1.3 GB in size
# outside of conda, install some depencies
sudo apt-get install libcurl4-openssl-dev
sudo apt-get install libssl-dev
sudo apt-get install libxml2-dev

# install key r packages within a r-environment
conda env remove -n r_env
# start empty environment
conda create -n r_env
# activate env and check, nothing yet
conda activate r_env
conda list

# check channel and add env specific channel
conda config --get channels
# to get latest r, use conda-forge, which has more recent Rs
# conda config --env --add channels conda-forge
# channels for base
conda config --get channels
# channels for cur env
conda config --get channels --env

# see all installed environments
conda env list
# activate an environment to use it, if in base, r does not exist, isolated in r_env
conda activate r_env
# see packages in environment
conda list
# to quit
conda deactivate

4.3.2 Install R and Some Packages in Conda

Either inside a R specific environment, or outside of it, now do additional installations for R and also R packages.

During installation, the R programs might require downgrading other programs in the Conda environment that Python for example also uses. That is why potentially it is safer to install in R environment. However, when installing in R enviornment, certain packages could have issues if installer does not realize it is in an environment with special folder structure, so installation could fail.

# install files
conda install -n r_env -c r r r-essentials r-tidyverse r-tidymodels -y

# trouble with igraph installation, install here from conda
# conda install -n r_env r-essentials r-base r-tidyr r-tidyverse r-devtools r-irkernel r-pkgdown r-roxygen2
conda install -n r_env -c r r-igraph
conda install -n r_env -c conda-forge igraph
conda install -n r_env -c r r-rstantools r-rstan r-rstanarm -y

4.4 Install Additional Packages inside the R

These are packages that I use, these should be installed inside the environment. Or they could be installed later from inside r-studio. Installing inside r-studio is a lot better. tidymodel has installation issues sometimes.

In r_env, type in R, and then install from inside terminal’s R. For conda env, devtools will be installed and will install packages inside envs/r_env/Lib/R.

# enter into env
# conda activate r_env
# enter R or type in rstudio
# sicne R is only install inside r_env, has to enter rstudio from r_env
# R
# rstudio

# main packagevps
# tidymodel installation on linux difficult
install.packages(c("rstantools", "rstan", "rstanarm"))
install.packages(c("tidyr", "tidyverse", "tidymodels"))

# tidymodels
# development packages
install.packages(c("devtools", "IRkernel", "pkgdown", "roxygen2", "kableExtra"))
# other packages
install.packages(c("AER", "minpack.lm", "knitr", "matlab"))

# Install my package, need to prepare package to work for ubuntu
# if install does not work, load locally
devtools::install_github("fanwangecon/REconTools")

# Present
install.packages("revealjs")
# Kernel
install.packages(c('repr', 'IRdisplay', 'evaluate', 'crayon', 'pbdZMQ', 'devtools', 'uuid', 'digest'))
IRkernel::installspec()

4.5 R-Studio Set-up Download from Rstudio

  1. Download R-studio, and install, as normal (not from conda, directly from rstudio website latest windows version)
  2. Open up anaconda prompt, enter r_env environment: activate r_env
  3. cd into rstudio exe file folder: cd “C:/Program Files/RStudio/bin”
  4. start r-studio from inside r_env: rstudio.exe
  5. inside r-studio, check: .libPaths()
    • “C:/ProgramData/Anaconda3/envs/r_env/Lib/R/library”
activate r_env
cd "C:/Program Files/RStudio/bin"
rstudio.exe

cd "C:/ProgramData/Anaconda3/envs/r_env/Lib/R/library"
"C:/Program Files/RStudio/bin/rstudio.exe"

For linux, note that rstudio offers Ubuntu as well as Debian versions that can be installed with wget:

<!-- for ubuntu -->
wget "https://download1.rstudio.org/desktop/bionic/amd64/rstudio-1.2.5033-amd64.deb"
<!-- for debian -->
sudo apt install ./rstudio-1.2.5033-amd64.deb
wget "https://download1.rstudio.org/desktop/debian9/x86_64/rstudio-1.2.5033-amd64.deb"
sudo apt install ./rstudio-1.2.5033-amd64.deb
conda install -c r rstudio
cd "C:/Program Files/RStudio/bin"
rstudio.exe

cd "C:/ProgramData/Anaconda3/envs/r_env/Lib/R/library"
"C:/Program Files/RStudio/bin/rstudio.exe"

Is RSTUDIO pointing to the R installation you want? Note you can open Rstudio from differen conda env command line, that will use different R installations. Or also set:

  • export RSTUDIO_WHICH_R=/usr/bin/R
  • export RSTUDIO_WHICH_R=/home/wangfanbsg75/anaconda3/bin/R

R-studio theme, VSCode theme for Rstudio works very well:

# admin rights
sudo rstudio --no-sandbox
remotes::install_github("anthonynorth/rscodeio")
rscodeio::install_theme()

4.6 R Tests

Test the following file to see if we can execute a R file. Do it inside r_env and inside a r session.

# A simple file with summary statistics using tidyverse
source('C:/Users/fan/R4Econ/summarize/dist/fst_hist_onevar.R')
# Another simple file with summary statistics using tidyverse
source('C:/Users/fan/R4Econ/support/tibble/fs_tib_basics.R')
# A file involving estimation
source('C:/Users/fan/R4Econ/optimization/cesloglin/fst_ces_plan_linlog.R')

# C:/Users/fan/R4Econ/summarize/dist/fst_hist_onevar.Rmd
# C:/Users/fan/R4Econ/support/tibble/fs_tib_basics.Rmd
# C:/Users/fan/R4Econ/optimization/cesloglin/fst_ces_plan_linlog.Rmd

5 Install Latex and PDF

Use a combination of overleaf and local compile. The goal is to have the same installation set-up for windows as well as linux. And hopefully, locally compilable file also can be compiled on overleaf (only use packages also available on overleaf).

There are several pieces of things to install:

  1. latex compiler: pdflatex, etc
  2. tex distributions: MikTex, texlive
    • these folders, once various packages are installed, could be very large, many GB
  3. latex packages from distributions
  4. editor (gui): Atom, Sublime text, texStudio, etc

5.1 Uninstall

  • uninstall MiKTeX: delete from programs, then delete remaining folder in Program Files.
  • uninstall texlive: go to c:/texlive, delete the folder

5.2 Install

Install on Windows

Install on Ubuntu

install texlive and texstudio ubuntu/debian.

sudo apt-get update
sudo apt-get install texlive-full
sudo apt-get install texstudio

Test file compilations in TexStudio.

5.3 SumatraPDF

For windows, install SumatraPDF. Can easily modify colors.

Settings

Kindle Sepia modifications:

  • Background color:
    • Kindle Sepia: fbf0d9
    • Kindle Sepia Light: fff8e6
  • Text color:
    • Kindle Sepia: 5f4b32
    • Kindle Sepia Darker: 503f2a
    • Kindle Sepia Darkerer: 1e170f
FixedPageUI [
    TextColor = #1e170f
    BackgroundColor = #fff8e6
    SelectionColor = #f5fc0c
    WindowMargin = 2 4 2 4
    PageSpacing = 4 4
]

5.4 Okular PDF

Linux: sudo apt-get install okular Windows: install okular from windows app store

5.4.1 Okular Shortcuts and Features

Settings:

  • Background color:
    • background behind pdf: configure okular, general, use custom backgroun color, Gainsboro gray: DCDCDC, kindle sepia: FBF0D9
    • pdf background itself: configure okular, accessbility, change color, change paper color, Light Kindle Sepia: fff8e6

Features:

  • Okular 3 pages per row view: view, overview
  • Automatic Update when PDF updated elsewhere: this is one of the key reasons to use okular, when I knit a Rmd file to PDF, the currently open Okular PDF file automatically updates and does not give file is open error like Acrobat.
  • Background color:
    • background behind pdf: configure okular, general, use custom backgroun color, Gainsboro gray: DCDCDC, kindle sepia: FBF0D9
    • pdf background itself: configure okular, accessbility, change color, change paper color, Light Kindle Sepia: fff8e6

Shortcuts:

  • Okular configure shortcuts: setting, configure shortcuts
  • Okular if menu bar lost: Ctrl + m
  • Okular comment pane: F6
  • Okular close left pane: F7
  • Okular full scrren: Ctrl + Shift + F
    • unlike acrobat, pdf allows for multiple scrrens

5.5 Adobe PDF

PDF Adobe Acrobat Installation + go to adobe website, log in using + go to my account, choose view and download my apps, choose acrobat & PDF, download Acrobat DC + UH account can only be activated on two accounts at once, so need to kick other computers out temporarily potentially

6 Install Various Editors

Atom, VSCode, Notepad++, Sublime, etc. These are essential editors that allows for pleasant programming and writing experiences. See here for fan instructions on vim installations.

All settings here JSON files here:

6.1 VSCode Installation

VSCode is much more light-weight than Atom, much faster. On Windows, download here. VScode settings easy to change for various extensions. Very very good Latex Editor, probably the best I have used.

For Linux:

# Debian Install
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo install -o root -g root -m 644 microsoft.gpg /usr/share/keyrings/microsoft-archive-keyring.gpg
sudo sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/microsoft-archive-keyring.gpg] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install code # or code-insiders

# Change Font (https://wwFw.reddit.com/r/Crostini/comments/cesxr0/vscode_ui_and_fonts_too_small_heres_how_to_fix_it/)
# modify: /usr/share/applications/code.desktop
nvim /usr/share/applications/code.desktop
#replace this line: Exec=/usr/share/code/code --unity-launch %F
Exec=sommelier -X --scale=0.8 --dpi=160 /usr/share/code/code "--unity-launch %F"
#replace this line: Exec=/usr/share/code/code --new-window %F
Exec=sommelier -X --scale=0.8 --dpi=160 /usr/share/code/code "--new-window %F"

# Open File
# low density mode to see cleary
code

6.1.1 VSCode Extensions

Press Ctrl + P, paste the ext install commands below to install extensions. Extensions are in: %USERPROFILE%/.vscode/extensions for windows. Can change code for extension there.

# [Microsoft Python Extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python)
ext install ms-python.python
# [Latex Workshop](https://marketplace.visualstudio.com/items?itemName=James-Yu.latex-workshop)
ext install latex-workshop
# [R](https://marketplace.visualstudio.com/items?itemName=Ikuyadeu.r)
ext install ikuyadeu.r

6.1.2 VSCode Shortcuts and Features

Features:

  1. open a file, if have workspace where the file is in, automatically expand tree and show where file is on left pane.

Shortcuts :

  • see all shortcuts: Ctrl + k, ctrl + s
  • Zen model: Ctrl + k, z, this means press control k, then press z
    • note that z mode can be adjusted by shiftin the bar on the left
  • Open up Settings: Ctrl + ,
  • Open up Extensions: Ctrl + Shift + X
  • Preview Markdown in VSCode: Ctrl + Shift + v
  • Open up left pane: Ctrl + b
  • Wrap Text: alt + z, switch between wrap or not to save screen space for example.
  • Hide Console Pane (running code): Ctrl + j

personal bindings

  • toggle minimap: Ctrl + k, ctrl + z

6.1.3 VSCode and R

VSCode works pretty well it seems with Rmd files. Rmd files are useful to work through algorithm, examples in preparation for writing usable functions. Rstudio has been often very slow for me, and I generally feel like can not stretch around and work in RStudio IDE.

In Atom, Hydrogen works, but really only works with very basic line by line testing.

Using VSCode with RMD files seems much quicker, and there is room to breath.

  1. Install R following R installation instructions.
  2. Then install extension R. Microsoft stopped supporting rmd in VSCode: R Tools for Visual Studio
  3. properly set path (might have to call vscode from r env)
    • r.rterm.linux: /home/wangfanbsg75/anaconda3/envs/r_env/bin/R
    • r.rterm.windows: C:/Program Files/R/R-3.6.2/bin/x64/R.exe
      • note R.exe at the end
  4. Run:
    • ctrl + enter to activate R terminal
    • VSCode 2019 allows for Knit Rmd: Ctrl + Shift + K
    • also can run code segment: select lines and ctrl + enter

6.1.4 VSCode and Latex

Download Latex Workshop.

Shortcuts

  • compile: ctrl + alt + b
  • sync: ctrl + alt + j
  • terminate pdflatex compile hang: ctrl + shift + p; kill latex compiler process
  • live save

Settings

change background border pdf color (not pdf but border): ctrl + shift + x, LatexWorkshop, Settings, Configure Extension Settings, Search for background, change color name. or enter: latex-workshop.view.pdf.backgroundColor. use gainsboro gray: #DCDCDC, or lightgray #D3D3D3.

To take advantage of Okular (where background color can be none-white for example). Go to settings, and: 1. Change: latex-workshop.view.pdf.viewer to external 2. Change: latex-workshop.view.pdf.external.viewer.command to C:/Program Files/SumatraPDF/SumatraPDF.exe open up C:/Users/fan/AppData/Roaming/Code/User/settings.json, and copy these in:

"latex-workshop.view.pdf.external.synctex.command": "C:/Program Files/SumatraPDF/SumatraPDF.exe",
"latex-workshop.view.pdf.external.synctex.args": [
  "-forward-search",
  "%TEX%",
  "%LINE%",
  "-reuse-instance",
  "-inverse-search",
  "code \"C:\\Users\\fan\\AppData\\Local\\Programs\\Microsoft VS Code\\resources\\app\\out\\cli.js\" -r -g \"%f:%l\"",
  "%PDF%",
]

Features:

  1. upon savings, rebuid file and update pdf (similar to overleaf)
  2. hover offers equation preview

Shortcuts:

  1. preview: ctrl + alt + v
  2. synctex location: ctrl + alt + j

6.1.5 VSCode Spell Check

Download Spell Right.

6.1.6 VSCode settings.json

The settings.json file, %APPDATA%/Code/User/settings.json:

{
  "r.rterm.windows": "C:/Program Files/R/R-3.6.2/bin/x64/R.exe",
  "window.zoomLevel": 0,
  "latex-workshop.view.pdf.viewer": "external",
  "latex-workshop.view.pdf.external.synctex.command": "C:/Program Files/SumatraPDF/SumatraPDF.exe",
  "latex-workshop.view.pdf.external.synctex.args": [
        "-forward-search",
        "%TEX%",
        "%LINE%",
        "-reuse-instance",
        "-inverse-search",
        "code \"C:\\Users\\fan\\AppData\\Local\\Programs\\Microsoft VS Code\\resources\\app\\out\\cli.js\" -r -g \"%f:%l\"",
        "%PDF%",
    ],
    "latex-workshop.view.pdf.external.viewer.command": "C:/Program Files/SumatraPDF/SumatraPDF.exe",
  "editor.minimap.enabled": false,
  "latex-workshop.latex.clean.fileTypes": [
    "*.aux",
    "*.bbl",
    "*.blg",
    "*.idx",
    "*.ind",
    "*.lof",
    "*.lot",
    "*.out",
    "*.toc",
    "*.acn",
    "*.acr",
    "*.alg",
    "*.glg",
    "*.glo",
    "*.gls",
    "*.fls",
    "*.log",
    "*.fdb_latexmk",
    "*.snm",
    "*.synctex(busy)",
    "*.synctex.gz(busy)",
    "*.nav",
    "*.xml",
    "*.gz",
    "*.bcf"
  ]
}

6.2 Atom Installation

Atom is slow but has clean look and nice git/github integration.

6.2.1 Atom Extensions

Terminal in windows or linux after installation: apm install hydrogen. apm is the atom package manager:

# Once atom is installed, can use apm to install packages
apm install hydrogen
# apm install project-manager

# r-related
# for me the best r-syntax highlighter I have found
apm install r-syntax

# latex-related
apm install language-latex
apm install latex

# Selection and Overview
apm install sublime-style-column-selection
apm install minimap
apm install highlight-selected
apm install split-diff

# vim distraction free editing
apm install vim-mode-plus
apm install ex-mode
apm install zen

# support
apm install date

# Inside Conda open up atom
atom

6.2.2 Atom Shortcuts and Features

Features:

# open multiple projects from atom at the same time, after repos synced
atom ~/fanwangecon.github.io ~/Pyfan ~/Teaching ~/Tex4Econ ~/R4Econ ~/M4Econ ~/Py4Econ
# open up any py from from within, try 1+1, does it work?
# also use script, try ctrl + shift + b, run whole file (not hydrogen)

Shortcuts :

  • Git Pane: Ctrl + 9, Ctrl + 8

6.2.3 Atom and Python

Having installed Anaconda, now install Atom and Hydrogen to Test with Python. For both Jupyter as well as Atom, always open from command prompt, open from Anaconda Prompt with admin rights.

Open up Jupyter Notebook, does the python kernel work? If does not, uninstall and re-install Anaconda.

# start jupyter lab, from prompt
jupyter lab

Try 1+1.

6.2.4 Atom and Latex

Install the following two packages in atom:

  1. latex package on atom.
  2. language-latex package on atom

In Atom Latex setting, note that on different platforms, paths to texlive are different:

  • Path:
    • Windows: C:/texlive/2019/bin/win32
    • Chromebook Linux: /usr/bin/latex
  • Which PDF viewer to use:
    • download Okular and use Okular
# set TeX path
# C:/texlive/2019/bin/win32

# Latex, after installation
# see where texlive appears under root folder directories
sudo find / -name "texlive"
# see latex version, if texlive is used
latex -v
# see main exe directory
which latex
# /usr/bin/latex

Latex Package Option

clean pattern

**/*.run.xml, **/*.aux, **/*.aux.bak, **/*.bbl, **/*.bcf, **/*.blg, **/*.dvi, **/*.fdb_latexmk, **/*.fls, **/*.idx, **/*.idx.bak, **/*.ilg, **/*.ind, **/*.lof, **/*.log, **/*.lol, **/*.lot, **/*.nav, **/*.out, **/*.ps, **/*.snm, **/*.synctex.gz, **/*.toc, /**/_minted-{jobname}, /{output_dir}/sage-plots-for-{jobname}.tex, /missfont.log, /texput.log, /texput.aux

6.2.5 Atom settings file

Atom keymap.cson file:

'atom-text-editor':
  'ctrl-shift-j': 'pretty-json:prettify'
  'alt-z':'date:date'
  'alt-x':'date:time'
  'alt-c':'date:datetime'
'body':
  'ctrl-tab': 'pane:show-next-item'
  'ctrl-shift-tab': 'pane:show-previous-item'
'.editor':
  'ctrl-alt-z': 'atom-beautify:beautify-editor'
  'ctrl-shift-r': 'hydrogen:restart-kernel'
'atom-text-editor:not([mini])':
  'ctrl-*': 'highlight-selected:select-all'
'atom-text-editor, webview.markdown-preview-plus':
  'ctrl-shift-X': 'markdown-preview-plus:toggle-render-latex'

# https://github.com/t9md/atom-vim-mode-plus/issues/130
'atom-text-editor.vim-mode-plus.insert-mode':
  'j j': 'vim-mode-plus:activate-normal-mode'

6.3 Sublime

Sublime

there are some issues with atom. When a file is edited inside Vim, when changes are saved, a temp file is created in atom. Sublime would, however, still show the file with the changes. This makes dual screen editing very difficult. Sublime text is used for this situation.

# Sublime Linux
wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add -
sudo apt-get install apt-transport-https
echo "deb https://download.sublimetext.com/ apt/stable/" | sudo tee /etc/apt/sources.list.d/sublime-text.list
sudo apt-get update
sudo apt-get install sublime-text
# launch from terminal
subl

Sublime Setup:

Put sublime path in windows path for example:

C:\Program Files\Sublime Text 3\

6.3.1 Sublime and Outline (Markdown)

To get the markdown outline of the current document if it is markdown press Ctrl + R, shows tabbed nested outline view.

6.3.2 Sublime Search and Replace

Append to end of all lines with starting string some suffix. We have many lines that start with print(‘abc’), but the printed statements differ in length. Within the document, want to change all print statements to message statements, and have: message(print(‘abc’)):

  1. cursor select print(
  2. alt + F3, this selects all occurances
  3. ctrl + end, moves cursor to end of each line, any other single cursor point speicifc left right end start operation should work
  4. append new text suffix
  5. ctrl + home, move cursor to the start of all print lines, and append prefix as needed.

6.4 PyCharm

pycharmjetbrains.com/pycharm/)

6.4.1 PyCharm Short Cuts

  • Refractor Names: CTRL + SHIFT + F6
  • Auto-Indent: CTRL + SHIFT + ALT + L

6.5 Other Editors

6.5.1 Notepad++

Notepad++. Many old projects have xml folder structures for Notepad++.

  • Silently update file changes made in other editors: Settings, Preferences, MISC., File Status Auto Detection, Update Silently.

7 Other Programs to Install

7.1 Package Managers

To install packages in windows more easily:

# in powershell
# enter cmd
# type powershell in cmd to enter powershell from cmd
powershell
# permission
set-executionpolicy remotesigned -scope currentuser
# install
iwr -useb get.scoop.sh | iex

7.2 Utilities

7.3 Security

  • keepass
  • Proton VPN: unlimited usage free version available.
  • Express VPN