Hello there
There is a recurring theme when it comes to installing R packages with Alteryx and it's my hope that this post resolves the situation for you.
The Problem
Running install.packages() in Alteryx, without directing it to the right location, will install the latest version of that package. The latest version of R on CRAN is R 4.2.2, so it is unlikely that the package that will be installed is compatible with the Alteryx that you run. I currently use Alteryx Designer 2022.1.1.40869, patch 2, which uses R 4.1.3. What we need to do is tell install.packages() to look for your package in the right place.
A Solution
As Alteryx is based on Windows (#alteryx4linux), we need the binaries associated with the package. Take a look at this webpage https://cran.r-project.org/bin/windows/contrib and you will find the location of pre-built Windows binaries for all the packages available for R => 3.4.x.
So, now that we've found the parent directory, which one do we pick? Good question. Let's use R to help us decide.
library(dplyr)
library(tidyr)
library(stringr)
# contriburl R 4.0 packages
r40 <- readLines(con = "https://cran.r-project.org/bin/windows/contrib/4.0/PACKAGES")
pkgname <- str_remove(r40[grep("Package:", r40)], "Package: ")
pkgver <- str_remove(r40[grep("Version:", r40)], "Version: ")
r40df <- data.frame(pkg = pkgname, r40v = pkgver)
# contriburl R 4.1 packages
r41 <- readLines(con = "https://cran.r-project.org/bin/windows/contrib/4.1/PACKAGES")
pkgname <- str_remove(r41[grep("Package:", r41)], "Package: ")
pkgver <- str_remove(r41[grep("Version:", r41)], "Version: ")
r41df <- data.frame(pkg = pkgname, r41v = pkgver)
# "base" Alteryx packages
altdf <- as.data.frame(installed.packages(lib.loc = .libPaths()[1]))
altdf <- data.frame(pkg = altdf$Package, altv = altdf$Version)
# create final data frame
temp <- merge(r40df, r41df, all = TRUE)
df <- merge(temp, altdf, all = TRUE) %>% drop_na()The above code creates a table of all the R packages bundled with my Alteryx with the version numbers for each one. It also contains a column of version numbers of the packages contained in the 4.0 and 4.1 directories. Direct the lib.loc argument to where R is installed (for me this is C:/Program Files/Alteryx/R-4.1.3/library), run the script and inspect the data frame. What you will likely see is that some versions in Alteryx are the same as those in 4.0, whilst others are the same in 4.1. It is a mixed bag! When you install a new package, you need to decide which location to grab it from; there will be some trial and error with this.
This is what I use
install.packages(<pkg>, lib = .libPaths()[4], contriburl = "https://cran.r-project.org/bin/windows/contrib/4.0", dependencies = TRUE)
For my employer's setup, .libPaths()[4] is what I need because I do not have write access to the other directories in the .libPaths() character vector; you may wish to specify another location. The contriburl argument is the location of all the packages in the version you choose (4.0 for me). With this command, install.packages() looks for the PACKAGES file on this webpage for information about the packages available (see the readLines() con argument above), including the suggests/imports/etc. and installs what you request according to the dependencies argument.
Just like a MS Windows installation, an empty R Tool automatically loads many packages - bloatware! If you want to install a new package that has a dependency, which is newer version than the one the R Tool loads, then you need to detach the package before you install the newer dependency. This can be very convoluted. For example, the caret package from 4.1 depends on rlang => 1.03, but the R Tool loads rlang 1.02. This is what I did to remove rlang 1.02 from the R Tool before the newer rlang was installed.
unloadNamespace("dplyr")
unloadNamespace("tidyselect")
unloadNamespace("purrr")
unloadNamespace("AlteryxPredictive")
unloadNamespace("ggplot2")
unloadNamespace("tibble")
unloadNamespace("pillar")
unloadNamespace("vctrs")
unloadNamespace("ellipsis")
unloadNamespace("scales")
unloadNamespace("lifecycle")
unloadNamespace("flightdeck")
unloadNamespace("DT")
unloadNamespace("htmlwidgets")
unloadNamespace("htmltools")
unloadNamespace("rlang")What a mess! It was far easier for me to install the caret from 4.0, which depends on the rlang bundled with Alteryx. This is a good example of why you need to determine which contriburl argument is right for your needs!
Final thoughts
R development is continual and dynamic, whereas the version of R compatible with Alteryx is not... well, you are free to update R in whatever way you'd like, but you run the risk of it not working with Alteryx! To get the right package, you need to get the right version for the Alteryx you have. Use the contriburl argument with install.packages() to find the right version for your needs!
I hope this helps!