Search code examples
rshinyubuntu-18.04shiny-server

Issues with strsplit only on Shiny Server


I am trying to perform a strsplit on a character vector to extract a date. It works like a charm in RStudio, but throws a Error in strsplit: non-character argument when deployed on an Ubuntu server with shiny.

the data

The data I am working with is the Advisory Forecast Track of Hurricanes, provided by the NOAA. I load the data into R as a kml file, which becomes a SpatialPointsDataFrame. This df includes HTML-tables at points@data$Description that look like the following (in my example length = 5, when using the NOAA-file AL022019_018adv_TRACK.kmz):

[1] <table>  <tr><td><font color=black><b>Tropical Depression Barry (AL022019)</b></font></td></tr>  <tr><td>Advisory #18</td></tr>  <tr><td><hr></td></tr>  <tr><td nowrap>Advisory Information</td></tr>  <tr><td nowrap>Valid at:  4:00 PM CDT July 14, 2019 </td></tr>  <tr><td nowrap>Location: 32.8 N, -93.6 W </td></tr>  <tr><td nowrap>Maximum Wind: 30 knots (35 mph) </td></tr>  <tr><td nowrap>Wind Gusts: 40 knots (45 mph) </td></tr>  <tr><td nowrap>Motion: N </td></tr> <tr><td nowrap>Minimum Pressure: 1008 mb </td></tr>

[2] <table>  <tr><td><font color=black><b>Tropical Depression Barry (AL022019)</b></font></td></tr>  <tr><td>Advisory #18</td></tr>  <tr><td><hr></td></tr>  <tr><td nowrap>12 hr Forecast</td></tr>  <tr><td nowrap>Valid at:  1:00 AM CDT July 15, 2019 </td></tr>  <tr><td nowrap>Location: 33.9 N, -93.6 W </td></tr>  <tr><td nowrap>Maximum Wind: 25 knots (30 mph) </td></tr>  <tr><td nowrap>Wind Gusts: 35 knots (40 mph) </td></tr>  <!-- HIDE_MOTION --> <!-- HIDE_PRES -->   

the code

The Code I'm using to extract the date from these table-vectors is the following:

points <- rgdal::readOGR("al022019_018adv_TRACK.kml"), require_geomType = "wkbPoint")

    day <- strsplit(as.character(points$Description), "Valid at: ") %>%
      sapply(.,`[`,2) %>%
      strsplit(., ", 2019") %>%
      sapply(.,`[`,1) %>%
      strsplit(., "MDT | PDT | EDT | CDT ") %>%
      sapply(., `[`, 2) %>%
      strsplit(., " ") %>%
      sapply(., `[`, 2)

Interestingly, I can print a class(as.character(points$Description))-function right before the strsplit which results in character.

Is it something in the piping operation?

the problem

When I run the code locally - whether in an R script or an shiny app, it runs smooth. The error occurs only when running the app on an Ubuntu 18.04.2 LTS server:

Warning: Error in strsplit: non-character argument
  [No stack trace available]

What am I missing? Thanks for your help!


Solution

  • If your goal is to extract the digits after the month (it looks like that is what your code is doing), then something like this could be more robust for Shiny. It is adaptable, but right now assumes that you always want the digit(s) after the full month name, and the the month will always need to be in title case.

    library(stringr)
    
    # Create pattern using a lookbehind to extract at least one digit
    # following a capitalized month name with whitespace preceding it
    pattern <- paste(paste0("(?<=", month.name, "\\s)\\d+"), collapse = "|")
    
    # Extract digits (could use str-extract all for multiple matches)
    stringr::str_extract(strings, pattern)
    [1] "14" "15"
    

    Data:

    strings <- c("[<tr><td><font color=black><b>Tropical Depression Barry (AL022019)</b></font></td></tr>  <tr><td>Advisory #18</td></tr>  <tr><td><hr></td></tr>  <tr><td nowrap>Advisory Information</td></tr>  <tr><td nowrap>Valid at:  4:00 PM CDT July 14, 2019 </td></tr>  <tr><td nowrap>Location: 32.8 N, -93.6 W </td></tr>  <tr><td nowrap>Maximum Wind: 30 knots (35 mph) </td></tr>  <tr><td nowrap>Wind Gusts: 40 knots (45 mph) </td></tr>  <tr><td nowrap>Motion: N </td></tr> <tr><td nowrap>Minimum Pressure: 1008 mb </td></tr>",
                 "<tr><td><font color=black><b>Tropical Depression Barry (AL022019)</b></font></td></tr>  <tr><td>Advisory #18</td></tr>  <tr><td><hr></td></tr>  <tr><td nowrap>12 hr Forecast</td></tr>  <tr><td nowrap>Valid at:  1:00 AM CDT July 15, 2019 </td></tr>  <tr><td nowrap>Location: 33.9 N, -93.6 W </td></tr>  <tr><td nowrap>Maximum Wind: 25 knots (30 mph) </td></tr>  <tr><td nowrap>Wind Gusts: 35 knots (40 mph) </td></tr>  <!-- HIDE_MOTION --> <!-- HIDE_PRES -->   ")