仅在 Shiny Server 上使用 strsplit 的问题
Issues with strsplit only on Shiny Server
我正在尝试对字符向量执行 strsplit
以提取日期。它在 RStudio 中工作起来就像一个魅力,但在 Ubuntu 服务器上部署时会抛出 Error in strsplit: non-character argument
闪亮。
数据
我正在使用的数据是飓风咨询预报轨迹,由 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 -->
代码
我用来从这些 table 向量中提取日期的代码如下:
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)
有趣的是,我可以在 strsplit
之前打印一个 class(as.character(points$Description))
函数,结果是 character
.
是管道操作有问题吗?
问题
当我在本地 运行 代码时 - 无论是在 R 脚本还是闪亮的应用程序中,它 运行 都很流畅。仅当 运行 在 Ubuntu 18.04.2 LTS 服务器上运行应用程序时才会发生此错误:
Warning: Error in strsplit: non-character argument
[No stack trace available]
我错过了什么?
感谢您的帮助!
如果您的目标是在月份之后提取数字(看起来这就是您的代码正在做的事情),那么像这样的事情 可能 对 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 --> ")
我正在尝试对字符向量执行 strsplit
以提取日期。它在 RStudio 中工作起来就像一个魅力,但在 Ubuntu 服务器上部署时会抛出 Error in strsplit: non-character argument
闪亮。
数据
我正在使用的数据是飓风咨询预报轨迹,由 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 -->
代码
我用来从这些 table 向量中提取日期的代码如下:
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)
有趣的是,我可以在 strsplit
之前打印一个 class(as.character(points$Description))
函数,结果是 character
.
是管道操作有问题吗?
问题
当我在本地 运行 代码时 - 无论是在 R 脚本还是闪亮的应用程序中,它 运行 都很流畅。仅当 运行 在 Ubuntu 18.04.2 LTS 服务器上运行应用程序时才会发生此错误:
Warning: Error in strsplit: non-character argument
[No stack trace available]
我错过了什么? 感谢您的帮助!
如果您的目标是在月份之后提取数字(看起来这就是您的代码正在做的事情),那么像这样的事情 可能 对 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 --> ")