0% found this document useful (0 votes)
595 views9 pages

Installing Required Software-Packages - Panama Workshop

This document provides instructions for installing R, RStudio, the R packages, and QGIS. It outlines 4 steps to install R and RStudio, then describes installing the R packages jsonlite and rstan. It also explains how to install QGIS 3.28 LTR and the preEA tool plugin for population estimates and census data analysis in QGIS.

Uploaded by

Diego Caceres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
595 views9 pages

Installing Required Software-Packages - Panama Workshop

This document provides instructions for installing R, RStudio, the R packages, and QGIS. It outlines 4 steps to install R and RStudio, then describes installing the R packages jsonlite and rstan. It also explains how to install QGIS 3.28 LTR and the preEA tool plugin for population estimates and census data analysis in QGIS.

Uploaded by

Diego Caceres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Project Proposal

Technical assistance and training on modeled population estimates to


support census in Latin America and the Caribbean region
12-16 Junio de 2023, Panama City, Panama

A collaboration between the Regional Office for Latin America and the Caribbean and
the Panama Country Office of the United Nations Population Fund (UNFPA), the
University of Southampton's WorldPop Research Group and the National Institute of
Statistics and Census in Panama with support from the Inter-American Development
Bank

HOW TO INSTALL R, RStudio, R packages, QGIS

A- Install R, RStudio, R packages


If you have not installed R and RStudio, please follow the following instruction. If they are
installed, please go to STEP 1.
1. Open the following website: https://posit.co/download/rstudio-desktop/
2. Download and Install R version 4.3.0
3. Download and install R Studio

HOW TO INSTALL RSTAN


STEP 1
1. Open R Studio and run this code:
install.packages("jsonlite", type = "source")
2. If you get a warning message in the installation, continue with STEP 2
Example of the error:
Error in system(paste(MAKE, p1(paste("-f", shQuote(makefiles))),
"compilers"), :
'make' not found
* removing 'C:/Users/shq1g12/AppData/Local/R/win-library/4.3/jsonlite'
Warning in install.packages :
installation of package ‘jsonlite’ had non-zero exit status

3. If the installation works properly then we can go ahead and run the following code:
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/",
getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/",
getOption("repos")))
4. We then restart r and verify if rstan runs properly by running this code
library(rstan)
example(stan_model, package = "rstan", run.dontrun = TRUE)

If rstan is working correctly then you will see the model compile as seen in the image
below

STEP 2 – We need to install and configure rtools

1. Open this link https://cran.r-project.org/bin/windows/Rtools/rtools40.html


2. Download rtools and install it

3. Copy the code below and run it in r to put rtools on the right path
writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con =
"~/.Renviron")
4. Then restart R (Sessions/Restart R)
5. Verify that rtools is being found using this code
Sys.getenv("BINPREF")
You will see something like this
## [1] ""
However if you see something like this, move to STEP 4
##[1] "C:/Rtools/mingw_$(WIN)/bin/"

6. If the rtools is correctly installed we should be able to install package from source
using this code:
install.packages("jsonlite", type = "source")
7. If installation fails or gives a warning message move to STEP 3. However, if the
installation works then we are good to go to install rstan.
Example of the error message:
Error in system(paste(MAKE, p1(paste("-f", shQuote(makefiles))),
"compilers"), :
'make' not found
* removing 'C:/Users/shq1g12/AppData/Local/R/win-library/4.3/jsonlite'
Warning in install.packages :
installation of package ‘jsonlite’ had non-zero exit status

8. Install rstan by copying and running this script


install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/",
getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/",
getOption("repos")))
9. We need to restart r and verify whether rstan is installed properly by using the
following code
library(rstan)
example(stan_model, package = "rstan", run.dontrun = TRUE)
10. if rstan is installed correctly the model will compile and you would see the
following

STEP 3

1. Restart R (Session/restart R) and run the following scripts (copy all and past them
in your RStudio Source
install.packages("pkgbuild")
rt_path = gsub("\\","/",pkgbuild::rtools_path(),fixed=T)
rt_bin = paste0(substr(rt_path,1,nchar(rt_path)-4),"/mingw_$(WIN)/bin/")
writeLines(paste0('PATH="',rt_path,';${PATH}"'), con = "~/.Renviron")
writeLines(paste0('Sys.setenv(BINPREF = "',rt_bin,'")'), con = "~/.Rprofile")

2. Install jasonite to see if you’re able to install it from source


install.packages("jsonlite", type = "source")

3. If the installation does not work move to STEP 4. Example of the message error:
ERROR: compilation failed for package 'jsonlite'
* removing
'C:/Users/shq1g12/AppData/Local/R/win-library/4.3/jsonlite'
Warning in install.packages :
installation of package ‘jsonlite’ had non-zero exit status

4. However, if the installation works, we can go ahead and install rstan using the
code below
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/",
getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/",
getOption("repos")))
5. We need to restart r and verify whether rstan is installed properly by using the
following code
library(rstan)
example(stan_model, package = "rstan", run.dontrun = TRUE)

6. if rstan is installed correctly the model will compile and you would see the
following

STEP 4
1. Restart R (Sessions/Restart R)
2. We need to check whether the BINPREF environment variable is pointing to the old
RTools installation by running the following in R

Sys.getenv("BINPREF").
Your output should look like this
#[1] ""
3. However, if your output looks like:
#[1] "C:/Rtools/mingw_$(WIN)/bin/"
Then you need to check whether your .Rprofile file has been configured to set this
variable on R startup.
4. Run the following to see the rtool path
readLines("~/.Rprofile")

You should see the following result:


#[1] "Sys.setenv(BINPREF = "C:/Rtools/mingw_$(WIN)/bin/")"
5. You have to delete your .Rprofile file. You can get the location of this file by running:
file.path(Sys.getenv("HOME"), ".Rprofile")
Take a note of the directory in your result, open the folder manually and
delete .RProfile
6. Navigate to that file in your document and delete that file then restart R. If BINPREF
is no longer set, then you should get the following output when you run the script
Sys.getenv("BINPREF")
#[1] ""

7. We then need to install packages from source by running the following


install.packages("jsonlite", type = "source")

8. if you can install jsonlite from the source then now we can install rstan by running the
following code
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/",
getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/",
getOption("repos")))
9. You need to restart r and verify whether rstan is installed properly by using the
following code
library(rstan)
example(stan_model, package = "rstan", run.dontrun = TRUE)
10. if rstan is installed correctly the model will compile and you would see the following
Final test:
To ensure your rstan is working correctly, please copy the following script into your
R and run it line by line:

#test to check if rstan is running correctly

install.packages ("rstudioapi")

#Load rstan

library("rstan") # observe startup messages

options(mc.cores = parallel::detectCores())

rstan_options(auto_write = TRUE)

model_code <- "


data {
int<lower=0> N; // number of observations
vector[N] x; // predictor variable
vector[N] y; // outcome variable
}
parameters {
real alpha; // intercept
real beta; // slope
real<lower=0> sigma; // standard deviation
}
model {
y ~ normal(alpha + beta * x, sigma); // likelihood
}
"
x <- rnorm(100) # generate x from standard normal distribution

data <- list(N = 100, x = rnorm(100), y = rnorm(100, 2 + 3 * x, 1)) # some simulated


data
fit <- stan(model_code = model_code, data = data) # fit the model

print(fit) # print the summary

##################END OF INSTALLATION###########################
B- Install QGIS

The preEA tool is compatible with all versions of the QGIS but we recommend installing QGIS 3.28
LTR

1- To install QGIS 3.28 on your machine, open your internet browser and navigate to the QGIS
downloads webpage: https://www.qgis.org/en/site/forusers/download.html

2- Click on the Looking for the most stable version? Get QGIS 3.28 LTR
3- Follow the on-screen instructions to install QGIS 3.28 on your computer. Once it has
installed, open it up to ensure it has installed correctly – on loading, you should see the
following graphic:
preEA tool plugin in QGIS

A zip folder that contains the preEA tool package will be shared with participants during the online
training.

How to install the preEA QGIS olugin

1- Open QGIS software, from the menu item, click on the Plugins/Manage and Install
Plugins
2- In the dialogue that opens, find the Install from ZIP

3- You will be provided with a zip package containing a PreEA QGIS plugin to install (XX.zip),
please navigate the browser to this folder (C:\ XX).
4- Select the preEA tool plugin folder (XX.zip) and click on Open.
5- Click the Install Plugin button to install the preEA tool in your QGIS software
6- To confirm the tool has been downloaded, make sure you see the preEA tool icon on the

main menu

You might also like