仅在Shiny Server上的strsplit问题

时间:2019-07-26 16:14:29

标签: r shiny ubuntu-18.04 shiny-server

我正在尝试对字符向量执行strsplit以提取日期。它像RStudio中的超级按钮一样工作,但是当部署在具有光泽的Ubuntu服务器上时会抛出Error in strsplit: non-character argument

数据

我正在使用的数据是NOAA提供的飓风预警预报轨迹。我将数据作为kml文件加载到R中,该文件成为SpatialPointsDataFrame。此df在points @ data $ Description处包含HTML表格,这些表格如下所示(在我的示例中,使用NOAA-file AL022019_018adv_TRACK.kmz时,length = 5):

[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 -->   

代码

我用来从这些表向量中提取日期的代码如下:

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)

有趣的是,我可以在class(as.character(points$Description))之前打印strsplit函数,结果为character

管道作业中有东西吗?

问题

当我在本地运行代码时(无论是在R脚本中还是在闪亮的应用程序中),它都能顺利运行。仅当在Ubuntu 18.04.2 LTS服务器上运行应用程序时,才会发生错误:

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

我想念什么? 感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

如果您的目标是在一个月后提取数字(看起来就是您的代码正在执行的操作),那么类似对于Shiny可能会更健壮。它是可适应的,但现在假设您始终希望在完整月份名称之后输入数字,并且月份始终需要以大写字母表示。

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"

数据

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 -->   ")