'Error:'from' must be a finite number'

Hi Community,

I'm trying to follow the tutorial on choropleth maps on lesson 5.

https://shiny.rstudio.com/tutorial/written-tutorial/lesson5/

After running my 'R' code I get the following error:

'Error:'from' must be a finite number'

I am trying to substitute the '%' data for total numbers of housing completions. The error appears to be relating to packages not loading but I suspect it may be to do with my 'helpers.R' code (re-worked from the demo script). In short I cannot pinpoint the issue. Hopefully some of you guys can spot the problem.

Here is the 'R' console code:-

install.packages("shiny")
install.packages("shinythemes")
install.packages("rsconnect")
install.packages("RPostgreSQL")
install.packages("rpostgis")
install.packages("sp")
install.packages("maps")
install.packages("mapproj")
library(shiny)
library(shinythemes)
library(rsconnect)
library(RPostgreSQL)
library(rpostgis)
library(sp)
library(maps)
library(mapproj)
source("C:/Users/Administrator.XXXXXX/Desktop/R_Projects_Oct19/helpers.R")
# Specify Driver
pg <- dbDriver("PostgreSQL")
# Connection Details
con <- dbConnect(pg, user ='xxxxxx', password ="xxxxxx", host = "xxxxxx" , port = 5432, dbname="xxxxxx")
# List all Tables in database (to test the connection)
dbListTables(con)
# Checking that the table exists
dbExistsTable(con, c("_office_for_national_statistics", "completions_2015_to_2019"))
# Query the tables for each layer you require
query <- "SELECT * FROM completions_2015_to_2019"
# Give your table a name
pauldb <- pgGetGeom(con, c("_office_for_national_statistics","completions_2015_to_2019"), geom = "wkb_geometry", gid = "ogc_fid", other.cols = TRUE)
# Plot the layer in Viewer
plot(pauldb,axes=TRUE)
ui <- fluidPage(
  titlePanel("House buying completions 2015 - 2019"),
  
  sidebarLayout(
    sidebarPanel(
      helpText("Create demographic maps for housing completions between 2015 and 2019"),
      
      
    selectInput("var",
                label = "Choose a completion to display",
                choices = c("completions_2015", "completions_2016", 
                            "completions_2017", "completions_2018", "completions_2019",
                            "completions_Total"),
                selected = "completions_Total"),
    
    sliderInput("range",
                label = "Range of interest:",
                min = 0, max = 106, value = c(0, 106))
    ),
    
    mainPanel(plotOutput("pauldb"))
    
  )
  
)
server <- function(input, output) {
  output$pauldb <- renderPlot({
    data <- switch(input$var, 
                   "Completions 2015" = pauldb$completions_2015,
                   "Completions 2016" = pauldb$completions_2016,
                   "Completions 2017" = pauldb$completions_2017,
                   "Completions 2018" = pauldb$completions_2018,
                   "Completions 2019" = pauldb$completions_2019,
                   "Completions Total" = pauldb$completions_Total)
    
    color <- switch(input$var, 
                    "Completions 2015" = "darkgreen",
                    "Completions 2016" = "black",
                    "Completions 2017" = "darkorange",
                    "Completions 2018" = "darkviolet",
                    "Completions 2019" = "darkred",
                    "Completions Total" = "darkslateblue")
    
    legend <- switch(input$var, 
                     "Completions 2015" = "completions_2015",
                     "Completions 2016" = "completions_2016",
                     "Completions 2017" = "completions_2017",
                     "Completions 2018" = "completions_2018",
                     "Completions 2019" = "completions_2019",
                     "Completions Total" = "completions_Total")
    
    completions_map(data, color, legend, input$range[1], input$range[2])
  })
}
# ShinyApp Function:
shinyApp(ui = ui, server = server)

Here is the console output:-

Restarting R session...

> library(shiny); runApp('completions.R')
Error in install.packages : Updating loaded packages
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/Administrator.XXXXXX/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/shinythemes_1.1.2.zip'
Content type 'application/zip' length 988847 bytes (965 KB)
downloaded 965 KB

package ‘shinythemes’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	D:\RtmpYbBfIb\downloaded_packages
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/Administrator.XXXXXX/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)

  There is a binary version available but the source version is later:
          binary source needs_compilation
rsconnect 0.8.13 0.8.15             FALSE

installing the source package ‘rsconnect’

trying URL 'https://cran.rstudio.com/src/contrib/rsconnect_0.8.15.tar.gz'
Content type 'application/x-gzip' length 256670 bytes (250 KB)
downloaded 250 KB

ERROR: failed to lock directory 'C:/Users/Administrator.XXXXXX/Documents/R/win-library/3.4' for modifying
Try removing 'C:/Users/Administrator.XXXXXX/Documents/R/win-library/3.4/00LOCK-rsconnect'
In R CMD INSTALL
Warning in install.packages :
  running command '"C:/PROGRA~1/R/R-34~1.4/bin/x64/R" CMD INSTALL -l "C:\Users\Administrator.XXXXXX\Documents\R\win-library\3.4" D:\\RtmpYbBfIb/downloaded_packages/rsconnect_0.8.15.tar.gz' had status 3
Warning in install.packages :
  installation of package ‘rsconnect’ had non-zero exit status

The downloaded source packages are in
	‘D:\RtmpYbBfIb\downloaded_packages’
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/Administrator.XXXXXX/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/RPostgreSQL_0.6-2.zip'
Content type 'application/zip' length 441573 bytes (431 KB)
downloaded 431 KB

package ‘RPostgreSQL’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	D:\RtmpYbBfIb\downloaded_packages
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/Administrator.XXXXXX/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/rpostgis_1.4.2.zip'
Content type 'application/zip' length 164225 bytes (160 KB)
downloaded 160 KB

package ‘rpostgis’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	D:\RtmpYbBfIb\downloaded_packages
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/Administrator.XXXXXX/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/sp_1.3-1.zip'
Content type 'application/zip' length 1538742 bytes (1.5 MB)
downloaded 1.5 MB

package ‘sp’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	D:\RtmpYbBfIb\downloaded_packages
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/Administrator.XXXXXX/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/maps_3.3.0.zip'
Content type 'application/zip' length 3635230 bytes (3.5 MB)
downloaded 3.5 MB

package ‘maps’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	D:\RtmpYbBfIb\downloaded_packages
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:

https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/Administrator.XXXXXX/Documents/R/win-library/3.4’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/mapproj_1.2.6.zip'
Content type 'application/zip' length 85497 bytes (83 KB)
downloaded 83 KB

package ‘mapproj’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	D:\RtmpYbBfIb\downloaded_packages

Attaching package: ‘rsconnect’

The following object is masked from ‘package:shiny’:

    serverInfo

Loading required package: DBI
Returning MultiPolygon types in SpatialPolygons*-class.

Listening on http://127.0.0.1:3039
Warning in min(x) : no non-missing arguments to min; returning Inf
Warning in max(x) : no non-missing arguments to max; returning -Inf
Warning: Error in seq.int: 'from' must be a finite number
  170: cut.default
  168: completions_map [C:/Users/Administrator.XXXXXX/Desktop/R_Projects_Oct19/helpers.R#12]
  167: renderPlot [C:/Users/Administrator.XXXXXX/Desktop/R_Projects_Oct19/completions.R#83]
  165: func
  125: drawPlot
  111: <reactive:plotObj>
   95: drawReactive
   82: origRenderFunc
   81: output$pauldb
    1: runApp

Here is the 'helpers.R' code:-

# Note: percent map is designed to work with the counties data set
# It may not work correctly with other data sets if their row order does 
# not exactly match the order in which the maps package plots counties
completions_map <- function(var, color, legend.title, min = 0, max = 100) {

  # generate vector of fill colors for map
  shades <- colorRampPalette(c("white", color))(100)
  
  # constrain gradient to numbers that occur between min and max
  var <- pmax(var, min)
  var <- pmin(var, max)
  numbers <- as.integer(cut(var, 106, 
    include.lowest = TRUE, ordered = TRUE))
  fills <- shades[numbers]

  # plot choropleth map
  map("completions_map", fill = TRUE, col = fills, 
    resolution = 0, lty = 0, projection = "polyconic", 
    myborder = 0, mar = c(0,0,0,0))
  
  # overlay completions_Total
  map("completions_Total", col = "white", fill = FALSE, add = TRUE,
    lty = 1, lwd = 1, projection = "polyconic", 
    myborder = 0, mar = c(0,0,0,0))
  
  # add a legend
  #inc <- (max - min) / 4
  #legend.text <- c(paste0(min, " % or less"),
    #paste0(min + inc, " %"),
    #paste0(min + 2 * inc, " %"),
    #paste0(min + 3 * inc, " %"),
    #paste0(max, " % or more"))
  
  legend("bottomleft", 
    legend = legend.text, 
    fill = shades[c(1, 25, 50, 75, 100)], 
    title = legend.title)
}

Many thanks,

Paul