3 Ways to Install R on Windows

When it comes to software applications, it is always handy to have more than one way to go about installing them. For R on Windows, there are several ways to accomplish this. In this post, I will be sharing a few that I have encountered and actually used.

1. From CRAN Homepage

The commonest approach to installing R is to visit the Comprehensive R Archive Network (CRAN) homepage at https://cran.r-project.org. On that page, one will see links referring to installation pages for different operating systems. Our interest in this is post is on Windows.

When we click on the link for Windows, we are taken to the R for Windows homepage:

Click on “install R for the first time” and you will be taken to the installation page for the current version of R (version 4.2.2 at the time of writing).

Click on “Download R-x.x.x for Windows…” and the installer will be downloaded to your machine. Save it at a destination of your choice (usually the ‘Downloads’ folder) and then run the file to start the installation proper. This will launch the installation wizard and you can safely use the default options to complete the process as indicated in the next few images:

When setup is complete, R can be launched from the Start Menu

2. From R with the installr package

If R is already installed, then it is easier to install updates with the installr package (this works best with R GUI). This is very useful since R Core is guaranteed to release key updates at least 2-3 times every year. This is the code to use

# install.packages("installr")  # => run only if  package is not already installed
installr::updateR()

If there’s a more recent version of R available, the following dialog will appear:

This is followed by an offer to see what’s new in R; this can be safely ignored:

Then you’ll be asked if you actually want to install the new R version. Selecting ‘No’ will terminate setup.

Once accepted, a progress bar appears for downloading the installer from CRAN:

This is followed by the wizard prompts as seen in the first section. However, this can be turned off in the function by setting the appropriate arguments. I won’t go into that in this post, but interested readers should look at the help file with ?installr::updateR. (I do have a utility package I created for personal use that has a function, easy_updateR, to do this without the bells and whistles)

After the installation, there will be additional housekeeping prompts. If there’s been a major/minor version change (following the scheme MAJOR.MINOR.PATCH), there may be a need to accept the invitation to update your other packages, but this can always be done later.

3. From the Command Line

By “command line” we are referring to Windows Terminal, PowerShell, or Command Prompt.

Windows now has a package management tool, winget, that eases the installation of software via the command line. Recent builds of Windows 10 and now Windows 11, come with it pre-installed, but if you’re using an older version, it has to be installed from the Microsoft Store. To check whether you have it, simply run winget at the prompt, and you will see a help message:

To install R, open the command line as an administrator (I’m using PowerShell) and run the tool as follows:

winget install Rproject.R

Follow any prompts, accept the defaults, and R will be installed–this time you won’t have to resort to the wizard. The great thing about this method is that it can be fully automated and it’s the most efficient!

Conclusion

There you have it, 3 easy ways to install R on Windows! Do you know of any other ways to do this? If your answer is ‘yes!’, feel free to tell us in the comments.

Leave a comment

Filed under Computers & Internet

naijR 0.5.1

A new version of the naijR package was released last weekend on CRAN, comprising internal changes that modify certain behaviour. I will try to quickly outline them in this post, but because the update followed soon after the last minor revision, it might be useful to look at the last blog post on v0.5.0.

User-visible Changes

Interactivity on Windows

As stated in previous articles, the function `fix_region()` provides 2 ways (automatic and interactive) to repair misspelt Local Government Areas (LGAs), a problem that is quite common when working with raw data containing this subnational division. Previously, any interactive work was done solely in the R console. Beginning with this version, Windows users have the benefit of using the native windowing system by way of dialogs, text controls, and message boxes, which should be more intuitive. On other platforms, the interaction still defaults to the Console. For details on how this works, read the vignette on the package website or locally via the package documentation.

Handling of the Federal Capital Territory

There are peculiarities with data that involve the Federal Capital Territory (FCT). First, it is not a State but is usually lumped along with States in most data sets. Also, it is either used as a whole or as an acronym. Recently, a researcher requested my support in plotting data on a map—one of the issues I realized he was facing was that the word “Abuja” (the capital city) was used to represent FCT, and as a State. Going forward, whenever the term Abuja is used as a State, a warning will be issued to the user.

Suppression of A Superfluous Warning

When the function `lgas` is given a string that is not a valid (i.e. correctly spelled) LGA, a warning is signaled. In a nested call to `fix_region()`, this warning becomes unnecessary and, quite frankly, irritating. So, the function was updated to recognize and suppress the warning whenever these 2 functions are used together. To be clear, in the old version this code signals a warning

lg <- c("Bwari", "Buari", "Kwali", "Kuali")

fix_region(lgas(lg))
## Successful fix(es):
## -------------------
## * Buari => Bwari
## * Kuali => Kwali
## Warning message:
## One or more items is not an LGA. Spelling error? 

The warning doesn’t make sense here, since the spelling mistake has been fixed; but it came from the call to lgas(), not fix_region(). In the update, the fix is reported on but without the warning.

Modifying Map Labels

An oversight was spotted, in that the size of region labels in the map could not be changed—a bug that was introduced during an earlier stage of development. This has now been fixed. The default character expansion factor (`cex`) is 0.75, adopted from the `maps` upon which this version of naijR depends. To adjust this, we can now simply use the undocumented `cex` argument which is used by `map_ng()` via its `…` parameter.

map_ng(show.text = TRUE)

We now reduce the text size:

map_ng(show.text = T, cex = .5)

Conclusion

To get more information on how to use the package, visit the GitHub page or the website. Feedback and/or contributions of code are also welcome.

Leave a comment

Filed under data science

naijR 0.5.0

A new version of this R package has been released on CRAN, the Comprehensive R Archive Network. It introduces a number of updates that will be described in somewhat detail in this blog post. For a synopsis, read the release notes.

New methods

Methods for some common generic R functions were included in the update. Some of these include c(), [, [[, na.exclude, to mention a few. This confers consistent behaviour to some of the **naijR** objects. For example, when concatenating 2 objects that represent the Nigerian States, we have the following:

library(naijR)
s1 <- states("Imo")
s2 <- states(c("Abia", "Anambra"))
s3 <- c(s1, s2)
s3
## States
## ------ 
## * Imo
## * Abia
## * Anambra 

Similarly, when indexing, the output formatting is retained:

s3[1]
## States
## ------ 
## * Imo 

Other methods added are as follows:

  • head.regions
  • tail.regions
  • print.regions
  • na.exclude.regions
  • na.omit.regions

Mobile numbers

The function fix_mobile can now repair phone numbers where zeros (0) are mistakenly replaced with the letter O (whether upper or lower case). As an example:

nums <- c("8034510441", "070304", "09O14358956")

# nums[3] has a letter 'O' in it and is fixed by the function
fix_mobile(nums)
[1] "08034510441" NA            "09014358956"

Map legends

Changes were made to the arguments that control how legends are displayed in the maps, particularly choropleths. The way the argument legend.text works is similar to that of the barplot function (from the **graphics** package). Only when this argument is TRUE will the legend be plotted. Also, other arguments that were used for fine-tuning were deprecated – leg.x, leg.y, and leg.orient. Going forward, any low-level legend manipulations should be done with graphics::legend().

As an example, let us create a 2-column data frame with hypothetical data on the

# Create a factor
set.seed(12)

x <- states() |>
  length() |>
  rnorm(100, 10) |>
  round() |>
  sample() |>
  cut(
    breaks = c(80, 89, 99, 109, 119, 129), 
    labels = c("80-89", "90-99", "100-109", "110-119", "120+"),
    include.lowest = TRUE
  )

# Make a 2-column data frame and take a look at it
df <- data.frame(state = states(), x)
head(df)

This gives the following output:

##       state       x
## 1      Abia   90-99
## 2   Adamawa   90-99
## 3 Akwa Ibom    120+
## 4   Anambra 100-109
## 5    Bauchi   90-99
## 6   Bayelsa   90-99

Now, we will use this data frame to create a choropleth map. One of the improvements in this version is that the function map_ng(), when given a two-column data frame will inspect each column, and if one column has valid States or LGAs and the other is a factor, it will automatically create the choropleth visualization. So using our data frame df,

map_ng(data = df)

With the new control given in the current version, we can do away with the legend.

# note partial matching with argument 'legend.text'
map_ng(data = df, legend = FALSE)

Due to the shape of the map, the best place to place any legend is in the bottom right corner. However, if the user wants to place it elsewhere, we can now use the Base R plotting constructs to do fine-grained touch-ups. This, of course, is for users who are more comfortable coding in R. As an example,

# Create objects containing the factor levels and then the colours
# map_ng() uses RColorBrewer internally, so we are using this
# to manually define the colours for the legend itself
categories <- levels(df$x)
colors <- RColorBrewer::brewer.pal(length(categories), "Greys")

# The legend's data, colours, orientation, justification, size and spacing are defined
res <- map_ng(data = df, legend = FALSE)
legend("top", categories, fill = colors, horiz = TRUE, xjust = .8, cex = .55, x.intersp = .5)

So much more can be done with this; details can be found in the documentation via ?legend.

Installation

The new version of the package can be installed from CRAN with the following code:

install.packages("naijR")

Feedback on the package is welcome – feel free to post a GitHub issue at https://github.com/BroVic/naijR

Happy coding!

1 Comment

Filed under data science, Uncategorized

naijR 0.4.4

This is to announce the release of a new version of naijR, a package designed to make it easier to work with data related to Nigeria. In this update, a bug related to the fix_mobile function was fixed. In older versions, whenever a vector made up of only missing values (NA) was passed to it, it failed with the following error:

# The first number is fixed and the missing value untouched
> raw.numbers <- c("8031111111", NA, "07061234567")
> fix_mobile(raw.numbers)
[1] "08031111111" NA            "07061234567"

# But if all are missing values, it fails
> all.missing <- rep(NA, 6)
> fix_mobile(all.missing)
Error in fix_mobile(all.missing) : 
  Objects of type ‘logical’ are not supported

So if, for example, one was trying to clean up column(s) of a data frame that have phone numbers, whenever it encountered one that was empty, it failed. This has now been fixed in the current version: the function now returns such data unchanged.

> fix_mobile(all.missing)
[1] NA NA NA NA NA NA

For more information on the package, including how to install and use it, visit https://brovic.github.io/naijR

Leave a comment

Filed under data science

A strange source of computer bugs

Today I looked at a tutorial on Visual Basic for Applications, the language that enables power users to program in Microsoft Office apps, notably Excel.

Now, in VBA, code comments can be defined with the single quote () character. Thus, this line of code would be a comment and ignored during program execution.

` This line of code is a comment

Somewhere down the line, I was reading the part where they described user-defined functions. Here’s the example:

Function Functionname(parameter-list) '
statement 1
statement 2
statement 3
.......
statement n
End Function

Notice what I saw on the far-right end of line 1. This confused me a little because I remembered that the ' characters are for comments. So how could it possibly be used here?

After a little bit of searching without success, I returned to look at the example again, only to find that the character had disappeared! I nearly kicked myself: what I thought was a character was nothing but a speck on my screen. I lost precious minutes because I did not keep my screen clean enough.

Facepalm
Sometimes one can be so careless!

This reminded me of how the earliest computer bugs were discovered, for example, at Harvard University. It’s a fascinating story–you can read more about it here.

Leave a comment

Filed under Computers & Internet