Install R new, or update an existing R installation.
Go to the RMD, R, PDF, or HTML version of this file. Go back to fan’s REconTools research support package, R4Econ examples page, PkgTestR packaging guide, or Stat4Econ course page.
Uninstall R, RStudio and RTools from Windows “Programs and Features” menu. After uninstaller finishes, check in the libPath folders to see if there are still stuff there, delete all, delete the folders.
If R was installed in a virtual environment, delete the environment. Otherwise, use system’s uninstaller. To check on this, from terminal/command-prompt for each virtual environment, type R, and .libPaths() to find paths. Do so inside conda main, outside of conda main, inside different environments, find all paths.
Inside R:
ls_spn_paths <- .libPaths()
print(ls_spn_paths)
## [1] "C:/Users/fan/AppData/Local/R/win-library/4.2"
## [2] "C:/Program Files/R/R-4.2.1/library"
# C:/Users/fan/Documents/R/win-library/3.6
# C:/Program Files/R/R-3.6.1/library
Check which conda env is installed, if there is an env installed for R.
# Show All installed environments
conda info --envs
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_envr
# Where is it installed?
which R
# /home/wangfanbsg75/anaconda3/bin/R
conda uninstall r-base
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
First, use the updateR() function from the installr package.
New R will have new package directory, could keep all in old, should copy all to new, and not keep old. Can choose to copy all old packages to new folder, but still keep old packages in prior folder as they were
# https://www.r-project.org/nosvn/pandoc/installr.html
install.packages('installr')
# update R from inside R (not Rstudio)
require(installr)
# this will open dialog boxes to take you through the steps.
updateR()
# Set Rstudio to the Latest R
Second, after updating, might go into “Apps and Features” on Windows to unstaill the previous R version.
Third, update RTools. Uninstall RTools. Installation can be very large.
Fourth, update RStudio. Upon opening RStudio, if prior installations of R have been uninstalled, RStudio will auto-detect as ask if the latest version of R should be used. Choose yes. Upon entering RStudio, if there are updates, might prompt to RStudio website to download and update.
Fifth, follow the package installation directions below to update that.
To be able to use R via command line, make sure Windows knows where the path to R.exe is.
First, find where the installed R.exe path is. Open up the R installation, and then check path as below.
ls_spn_paths <- .libPaths()
print(ls_spn_paths)
# "C:/Users/fan/AppData/Local/R/win-library/4.2"
# "C:/Program Files/R/R-4.2.1/library"
Second, given the path found, the R.exe is at “C:/Program Files/R/R-4.2.1/bin”. So now, in windows, System Properties -> Advanced -> Environment Variables -> System Variables -> Path -> Edit -> New -> Paste “C:/Program Files/R/R-4.2.1/bin”
Third, now open up command-prompt/terminal/git-bash, enter R, this will take us into the R console via command-line.
# To exit command line:
q()
After installing R, change the path sequence so that packages install for all users.
ls_spn_paths <- .libPaths()
print(ls_spn_paths)
# [1] "C:/Users/fan/AppData/Local/R/win-library/4.2" "C:/Program Files/R/R-4.2.1/library"
ls_spn_paths <- c(ls_spn_paths[2], ls_spn_paths[1])
.libPaths(ls_spn_paths)
ls_spn_paths <- .libPaths()
print(ls_spn_paths)
# [1] "C:/Program Files/R/R-4.2.1/library" "C:/Users/fan/AppData/Local/R/win-library/4.2"
After updating R, sometimes, old packages are not copied over to new directory, so need to reinstall all packages.
Having set the directories earlier so that packages do not install in user’s personal folder, but the library folder where the R version is installed, we can find all installed packages in the C:/Program Files/R/R-4.2.1/library folder.
# Install RTools First!
# https://cran.r-project.org/bin/windows/Rtools/
# Install system tools
install.packages(c("backports"))
# Install tidyverse
install.packages(c("tidyverse", "tidymodels", "vroom"))
# Install Packaging tools
install.packages(c("devtools", "pkgdown", "roxygen2", "bookdown", "knitr", "kableExtra", "formatR", "revealjs"))
# Install Statistics models
install.packages(c("AER", "minpack.lm"))
install.packages(c("quantreg"))
# Install Tools to Work with Other Packages
# matconv: converts matlab programs to R
install.packages(c("reticulate", "JuliaCall", "matconv"))
install.packages(c("matconv"))
# for reticulate errors, install directly from: devtools::install_github("rstudio/reticulate")
# Install Paralell Tools
install.packages(c("parallel", "doParallel", "foreach"))
# Install personal Packages
devtools::install_github("fanwangecon/REconTools")
devtools::install_github("fanwangecon/PrjOptiAlloc")
# Stata in Rmd
# devtools::install_github("Hemken/Statamarkdown")
# VScode integration and also sublime r-ide
install.packages("languageserver")
Temp Installs:
# 2020-10-19
# Temp install development version due to but
# https://github.com/rstudio/reticulate/issues/831
devtools::install_github("rstudio/reticulate")
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/htmlpdfr/fst_hist_onevar.R')
# source('G:/repos/R4Econ/summarize/dist/htmlpdfr/fst_hist_onevar.R')
# # Another simple file with summary statistics using tidyverse
# source('C:/Users/fan/R4Econ/support/tibble/htmlpdfr/fs_tib_basics.R')
# source('G:/repos/R4Econ/support/tibble/htmlpdfr/fs_tib_basics.R')
# # A file involving estimation
# source('C:/Users/fan/R4Econ/optimization/cesloglin/htmlpdfr/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
Rstudio seems laggy sometimes, nice to be able to run R from VSCode, use VSCode as an alternative editor.
To Run .R scripts from inside VSCode. Here is the offical guide: R in Visual Studio Code.
Steps for RMD:
Change File Extension Association
"files.associations": {
"*.Rmd": "markdown"
}