- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望有人能帮助我解决我的代码的问题。我花了大约 5 个小时试图找出代码的问题,并检查了这些链接 1 , 2但无法找出我的代码的问题我正在尝试构建一个 Shiny 的应用程序,但是当我运行代码时,我不断收到错误消息:
Error: Problem with `mutate()` column `Date`.
ℹ `Date = ymd(Date)`.
x cannot coerce type 'closure' to vector of type 'character'
看起来问题在于创建日期和月份列,但我的数据中有它们,正如您在我的数据快照中看到的那样:
这是我用于读取数据和争论数据的代码
#Read and wrangle data
dvc_read <- st_read("/Users/Desktop/shap/accident_data.shp", # shapefile stored in repo
stringsAsFactors = FALSE # read factor columns as character columns
)
dvc_wrangle <- dvc_read %>%
st_transform(crs = 4326) %>% # transform coords to latlong
# decapitalise everything for easy handling
rename_all(tolower) %>%
mutate_if(is.character, tolower) %>% # simplify strings
# deal with dates
mutate(
Date = ymd(Date), # the issue is with line
Month = case_when( # and this line
Month == 1 ~ "Jan", Month == 2 ~ "Feb",
Month == 3 ~ "Mar", Month == 4 ~ "Apr",
Month == 5 ~ "May", Month == 6 ~ "Jun",
Month == 7 ~ "Jul", Month == 8 ~ "Aug",
Month == 9 ~ "Sep", Month == 10 ~ "Oct",
Month == 11 ~ "Nov", Month == 12 ~ "Dec",
TRUE ~ "Unknown"
),
# clean up strings (not perfect)
Severity = if_else(Severity %in% c("1", "2", "3"), "unknown", Severity),
Accident.Description = if_else(Accident.Description == "Road closed", "Road_closed", Accident.Description),
City = if_else(str_detect(City, "x") == TRUE, "unknown", City),
City = if_else(
City %in% c(
"Brooklyn", "Jamaica", "Merrick", "Roosevelt", "unclassified",
), "unknown", City
),
# final name tidy-up
Accident.Description = str_replace_all(Accident.Description, "_", " ")
) %>%
# title case for these columns
mutate_at(vars(Severity, Accident.Description, City), tools::toTitleCase) # To Title Case
###
# extract latlong cols from sf geometry and bind back to df
dvc_xy <- as.data.frame(st_coordinates(dvc_wrangle))
dvc <- bind_cols(dvc_wrangle, dvc_xy) %>% rename(latitude = X, longitude = Y)
saveRDS(dvc,"/Users/Desktop/shap/dvc.RDS")
dvc <- readRDS("data/dvc.RDS")
# Month order for dropdown input
mo_order <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
为了更好地理解,我将添加 Ui 代码和服务器代码
用户界面代码:
ui <- dashboardPage(
dashboardHeader(
title = "Title",
titleWidth = 450
), # end dashboardHeader()
dashboardSidebar(
HTML("<br>"),
box(
title = "Filters",
width = 12,
background = "blue",
collapsible = TRUE, collapsed = FALSE,
selectInput(
inputId = "input_year",
label = "Year",
choices = sort(unique(dvc$Year)),
multiple = TRUE,
selected = sample(unique(dvc$Year), 1)
),
selectInput(
inputId = "input_month",
label = "Month",
choices = unique(dvc$Month[order(match(dvc$Month, mo_order))]),
multiple = TRUE,
selected = sample(unique(dvc$Month), 3)
),
selectInput(
inputId = "input_la",
label = "Accident.Description",
choices = sort(unique(dvc$Accident.Description)),
multiple = TRUE,
selected = sample(unique(dvc$Accident.Description), 3)
)
) # end box()
), # end dashboardSidebar()
dashboardBody(
fluidRow(
valueBoxOutput("output_valueselection"),
valueBoxOutput("output_valueyearla"),
valueBoxOutput("output_valueyear"),
tabBox(
id = "tabset1",
width = 12,
tabPanel("Map", leafletOutput("output_map", height = "600px")),
tabPanel("Table", dataTableOutput("output_table"))
)
) # end fluidRow()
) # end dashboardBody()
) # end of ui dashboardPage()
服务器代码:
server <- function(input, output) {
# Value box - year
output$output_valueyear <- renderValueBox({
shinydashboard::valueBox(
value = dvc %>% st_drop_geometry() %>% filter(Year %in% input$input_year) %>% count() %>% pull(),
subtitle = "Collisions in selected year(s)",
icon = icon("calendar", lib = "font-awesome"),
color = "blue",
width = 4
)
}) # end of renderValueBox
# Value box - year by la
output$output_valueyearla <- renderValueBox({
shinydashboard::valueBox(
value = dvc %>% st_drop_geometry() %>% filter(Year %in% input$input_year, Accident.Description %in% input$input_la) %>% count() %>% pull(),
subtitle = "Collisions in selected LA(s) and year(s)",
icon = icon("map-o", lib = "font-awesome"),
color = "blue",
width = 4
)
}) # end of renderValueBox
# Value box - total in your selection
output$output_valueselection <- renderValueBox({
shinydashboard::valueBox(
value = dvc %>% st_drop_geometry() %>% filter(Year %in% input$input_year, Month %in% input$input_month, Accident.Description %in% input$input_la) %>% count() %>% pull(),
subtitle = "Collisions in selection",
icon = icon("car", lib = "font-awesome"),
color = "blue",
width = 4
)
}) # end of renderValueBox
# Interactive map with Leaflet
output$output_map <- renderLeaflet({
dvc %>%
filter(
Year %in% input$input_year,
Month %in% input$input_month,
Accident.Description %in% input$input_la
) %>%
leaflet() %>%
addProviderTiles(providers$OpenStreetMap) %>%
addAwesomeMarkers(
icon = awesomeIcons(
icon = "exclamation-circle",
iconColor = "#FFFFFF",
library = "fa",
markerColor = "darkblue"
),
popup = ~paste0(
"<style>
td, th {
text-align: left;
padding: 3px;
}
</style>",
"<table>",
"<tr>","<td>", "Date", "</td>", "<td>", Date, "</td>", "<tr>",
"<tr>","<td>", "LA", "</td>", "<td>", Accident.Description, "</td>", "<tr>",
"<tr>","<td>", "City", "</td>", "<td>", City, "</td>", "<tr>",
"<tr>","<td>", "Species", "</td>", "<td>", Severity, "</td>", "<tr>",
"</table>"
)
)
}) # end of renderLeaflet
# Interactive table with DT
output$output_table <- renderDataTable({
dvc %>%
st_drop_geometry() %>%
filter(
Year %in% input$input_year,
Month %in% input$input_month,
Accident.Description %in% input$input_la
) %>%
select(
Date = Date,
Year = Year,
Month = Month,
`Accident.Description` = Accident.Description,
City = City,
`Severity` = Severity
) %>%
datatable(
filter = "top",
extensions = c("Scroller", "Buttons"), # scroll instead of paginate
rownames = FALSE, # remove row names
style = "bootstrap", # style
width = "100%", # full width
height = "800px",
options = list(
deferRender = TRUE,
# scroll
scrollY = 300,
scroller = TRUE,
# button
autoWidth = TRUE, # column width consistent when making selections
dom = "Blrtip",
buttons =
list(
list(
extend = "collection",
buttons = c("csv", "excel"), # download extension options
text = "Download" # text to display
)
)
) # end of options = list()
) # end of datatable()
}) # end of renderDataTable()
} # end of server function
非常感谢您帮助解决这个问题并修复我的代码中的错误。
这是我使用 dput() 的数据:
structure(list(lat = c(40.61955, 40.61955, 40.61955, 40.61955,
40.61955, 40.61955), long = c(-74.02346, -74.02346, -74.02346,
-74.02346, -74.02346, -74.02346), Year = c("2020", "2020", "2019",
"2020", "2020", "2019"), Month = c("2", "11", "10", "9", "1",
"9"), Day = c("6", "21", "26", "20", "11", "15"), Date = c("2020-02-06",
"2020-11-21", "2019-10-26", "2020-09-20", "2020-01-11", "2019-09-15"
), Accident.Description = c("Right lane blocked", "Two lanes blocked",
"Two lanes blocked", "Right lane blocked", "Right lane blocked",
"One lane blocked"), Severity = c("3", "3", "3", "3", "3", "3"
), geometry = structure(list(structure(c(-74.02346, 40.61955), class = c("XY",
"POINT", "sfg")), structure(c(-74.02346, 40.61955), class = c("XY",
"POINT", "sfg")), structure(c(-74.02346, 40.61955), class = c("XY",
"POINT", "sfg")), structure(c(-74.02346, 40.61955), class = c("XY",
"POINT", "sfg")), structure(c(-74.02346, 40.61955), class = c("XY",
"POINT", "sfg")), structure(c(-74.02346, 40.61955), class = c("XY",
"POINT", "sfg"))), class = c("sfc_POINT", "sfc"), precision = 0, bbox = structure(c(xmin = -74.02346,
ymin = 40.61955, xmax = -74.02346, ymax = 40.61955), class = "bbox"), crs = structure(list(
input = "NAD83", wkt = "GEOGCRS[\"NAD83\",\n DATUM[\"North American Datum 1983\",\n ELLIPSOID[\"GRS 1980\",6378137,298.257222101,\n LENGTHUNIT[\"metre\",1]]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n CS[ellipsoidal,2],\n AXIS[\"latitude\",north,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n AXIS[\"longitude\",east,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n ID[\"EPSG\",4269]]"), class = "crs"), n_empty = 0L)), row.names = c(NA,
6L), class = c("sf", "data.frame"), sf_column = "geometry", agr = structure(c(lat = NA_integer_,
long = NA_integer_, Year = NA_integer_, Month = NA_integer_,
Day = NA_integer_, Date = NA_integer_, Accident.Description = NA_integer_, Severity = NA_integer_), .Label = c("constant", "aggregate",
"identity"), class = "factor"))
谢谢大家的帮助!我真的很感谢你给我的所有答案!我修复了代码问题,但我仍然面临一列“accident.description”列的问题。尽管我将名称更改为“意外”,但我认为该点导致了问题,但无法弄清楚。此列的错误消息如下:
Error: Problem with `mutate()` column `accident.description`.
ℹ `accident.description = if_else(...)`.
x object 'accident.description' not found
这是我更新的代码:
dvc_wrangle <- dvc_read %>%
st_transform(crs = 4326) %>% # transform coords to latlong
# decapitalise everything for easy handling
rename_all(tolower) %>%
mutate_if(is.character, tolower) %>% # simplify strings
# deal with dates
mutate(
Date = ymd(date),
Month = case_when(
month == 1 ~ "Jan", month == 2 ~ "Feb",
month == 3 ~ "Mar", month == 4 ~ "Apr",
month == 5 ~ "May", month == 6 ~ "Jun",
month == 7 ~ "Jul", month == 8 ~ "Aug",
month == 9 ~ "Sep", month == 10 ~ "Oct",
month == 11 ~ "Nov", month == 12 ~ "Dec",
TRUE ~ "Unknown"
),
# clean up strings (not perfect)
Severity = if_else(severity %in% c("1", "2", "3"), "unknown", severity),
accident.description = if_else(accident.description == "Road closed", "Road_closed", accident.description),
City = if_else(str_detect(city, "x") == TRUE, "unknown", city),
City = if_else(
city %in% c(
"Brooklyn", "Jamaica", "Merrick", "Roosevelt", "unclassified",
), "unknown", city
),
# final name tidy-up
Accident.Description = str_replace_all(Accident.Description, "_", " ")
) %>%
# title case for these columns
mutate_at(vars(Severity, Accident.Description, City), tools::toTitleCase) # To Title Case
我将“道路封闭”更改为“道路封闭”,并将列名称更改为“事故”,如下所示,但仍然收到相同的错误消息:
Accident = if_else(accident %in% c("Road closed", "Two lanes blocked", "One lane blocked"), "unknown", accident),
感谢大家的帮助和支持!你们都太棒了!我根据您的注释和有关更新 II 的内容修复了代码,我将 LHS 上的“Accident.Description”列名称更改为大写而不是小写。但是,我收到一条新的错误消息:
Error in rename.sf(.tbl, !!!syms) :
internal error: can't find `agr` columns
这是修改后的代码:
dvc_wrangle <- dvc_read %>%
st_transform(crs = 4326) %>% # transform coords to latlong
# decapitalise everything for easy handling
rename_all(tolower) %>%
mutate_if(is.character, tolower) %>% # simplify strings
# deal with dates
mutate(
Date = ymd(date),
Month = case_when(
month == 1 ~ "Jan", month == 2 ~ "Feb",
month == 3 ~ "Mar", month == 4 ~ "Apr",
month == 5 ~ "May", month == 6 ~ "Jun",
month == 7 ~ "Jul", month == 8 ~ "Aug",
month == 9 ~ "Sep", month == 10 ~ "Oct",
month == 11 ~ "Nov", month == 12 ~ "Dec",
TRUE ~ "Unknown"
),
# clean up strings (not perfect)
Severity = if_else(severity %in% c("1", "2", "3"), "unknown", severity),
Accident.Description = if_else(accident.description == "Road closed", "Road_closed", accident.description),
City = if_else(str_detect(city, "x") == TRUE, "unknown", city),
City = if_else(
city %in% c(
"Brooklyn", "Jamaica", "Merrick", "Roosevelt", "unclassified",
), "unknown", city
),
# final name tidy-up
Accident.Description = str_replace_all(accident.description, "_", " ")
) %>%
# title case for these columns
mutate_at(vars(Severity, Accident.Description, City), tools::toTitleCase) # To Title Case
最佳答案
你的问题很简单。一旦Date
列重命名为 date
# decapitalise everything for easy handling
rename_all(tolower) %>%
然后是有问题的行
# deal with dates
mutate(
Date = ymd(Date), # the issue is with line
包含表达式ymd(Date)
引用不存在列 Date
.
所以 R 在 data mask 之外搜索对于名为Date
的东西...它找到的第一件事是函数 lubridate::Date()
,它(大概)已作为 Date
加载到您的工作区中,通过library(lubridate)
。请注意,函数也称为 "closures"在 R 中。
在那个有问题的表达中
ymd(Date)
因此,您将提供一个闭包作为 ymd()
的输入,其中 expects一个character
向量。因此出现错误:
x cannot coerce type 'closure' to vector of type 'character'
只需更改您的代码以引用 date
列,现在为小写。
# deal with dates
mutate(
Date = ymd(date),
# ^^^^
关于运行 `rlang::last_error()`看看错误发生在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70781242/
我从文档中可以看到rlang::enquo()和rlang::quo()用于不同的上下文。因此,我使用了 rlang::enysm()最近在函数声明中(见下文)。然而,在另一个 SE 函数调用中,我收
我正在尝试编写一个有点复杂的自定义函数,因此为了简单起见,我创建了玩具示例。 假设我想编写一个函数 - 自动决定运行适当的函数:例如,t 检验或方差分析。 接受“带引号” 和不带引号 参数 所以我编写
当我建立像决策树、随机森林这样的机器学习分类模型时,我会遇到错误。更新程序包(标准程序包)
我再次对 rlang 的文档感到困惑和错误信息。我使用双刘海和三刘海尝试了 20 次不同的迭代,:= , quo , enquo , ensym ,以及所有其他名称不明确的 rlang 函数。 如果您
我使用 memoise 包将查询缓存到 arrow 数据集,但我有时会在哈希中遇到不匹配/“冲突”,因此会返回错误的值。 我已经隔离了问题并将其复制到下面的 MWE 中。问题在于,首先过滤然后汇总的箭
假设我想计算 mean , min和 max对于自定义函数中任意数量的组。 玩具数据如下所示: library(tidyverse) df % gather(Variable, Value, -c
我创建了一个函数来将函数名转换为字符串。版本 1 func_to_string1 运行良好,但版本 2 func_to_string2 不起作用。 func_to_string1 func_to_s
我正在编写一个函数,其中我提供一个变量名作为符号。在函数的不同步骤中,我想将变量名称用作字符串。根据文档,rlang::as_string“将符号转换为字符串。” 这是一个基本的例子。此函数返回带有标
我正在写一个包,我的一个函数生成了一个 ggplot。我只想导入 ggplot2 或 rlang(不依赖于它们)。经过一些尝试和错误后,我设法让它工作,但现在我不确定为什么它能工作。 所以我的问题是,
我正在编写一个使用 tidyverse 函数的包,即使用非标准评估,如 dplyr::filter例如: setMethod("filter_by_id", signature(x
是否可以使用 rlang 整洁评估运算符 {{在 lm 公式中? 我知道您可以使用双花括号来定义一个通用函数,例如: my_scatter <- function(df, xvar, yvar) {
假设我有一个函数 f,它接受一堆参数,以及一个可选的额外参数。 f <- function(..., extra) { arglst <- lapply(quos(...), get_expr
说我有一个嵌套列表 tmp rlang::flatten(tmp) $a [1] 1 [[2]] [[2]]$x [1] 1 [[2]]$y [1] "a" [[3]] [[3]]$z [1] 2
文档表明这是可能的: fn % base::as.list() tmp %>% stringr::str_detect('x') %>%
我正在尝试创建一个字符串,其中包含“大括号”和用户基于的对象。然后我将在 ggplot 命令中解析和评估这个表达式。 test_func % rlang::expr() df %>%
有时,在使用 dplyr 时,会有一个列名称的字符向量,用于对数据进行操作,例如: cols_of_interest % select_(.dots = my_cols) 现在已弃用 verb_
我希望有人能帮助我解决我的代码的问题。我花了大约 5 个小时试图找出代码的问题,并检查了这些链接 1 , 2但无法找出我的代码的问题我正在尝试构建一个 Shiny 的应用程序,但是当我运行代码时,我不
我正在构建一个函数,我希望用户能够为其传递未加引号的变量。稍后,在准备输出时,我将需要这些变量的名称作为字符串。 如果每个参数只携带一个变量,这没有问题。我可以使用 deparse(substitut
我正在尝试安装 tidyr 的开发版本。当我尝试 devtools::install_github("tidyverse/tidyr") 或 remotes::install_github("tidy
我希望有人能帮助我解决我的代码的问题。我花了大约 5 个小时试图找出代码的问题,并检查了这些链接 1 , 2但无法找出我的代码的问题我正在尝试构建一个 Shiny 的应用程序,但是当我运行代码时,我不
我是一名优秀的程序员,十分优秀!