尝试记录所有错误和警告futile.logger
.
对于处理错误有点满意:
library(futile.logger)
options(error = function() { flog.error(geterrmessage()) ; traceback() ; stop() })
log("a")
# Error in log("a") : argument non numérique pour une fonction mathématique
# ERROR [2016-12-01 21:12:07] Error in log("a") : argument non numérique pour une fonction mathématique
#
# No traceback available
# Erreur pendant l'emballage (wrapup) :
Run Code Online (Sandbox Code Playgroud)
有冗余,但我可以很容易地之间的分离stderr
,stdout
以及日志文件,所以它不是一个问题.它肯定不漂亮,有一个额外的"总结"错误消息,不知何故由stop()
我不理解的决赛造成,所以我愿意接受建议.
我找不到类似的警告解决方案.我尝试了什么:
options(warn = 1L)
options(warning.expression = expression(flog.warn(last.warning)))
log(- 1)
# [1] NaN
Run Code Online (Sandbox Code Playgroud)
但无济于事.
后续问题:我是否在不知不觉中忽略了最佳做法?
试图do.call()
在整洁评估的背景下工作:
library(rlang)
library(dplyr)
data <- tibble(item_name = c("apple", "bmw", "bmw"))
mutate(data, category = case_when(item_name == "apple" ~ "fruit",
item_name == "bmw" ~ "car"))
# # A tibble: 3 x 2
# item_name category
# <chr> <chr>
# 1 apple fruit
# 2 bmw car
# 3 bmw car
Run Code Online (Sandbox Code Playgroud)
有什么不同:
category_fn <- function(df, ...){
# browser()
cat1 <- quos(...)
mutate(df, category = case_when(!!! cat1))
}
category_fn(df = data, item_name == "apple" ~ "fruit",
item_name == "bmw" ~ "car")
# …
Run Code Online (Sandbox Code Playgroud) 我想以编程方式根据谓词函数(例如is.character
)和 "select helper"(例如starts_with("Z")
)同时选择要变异的列.
library(dplyr)
df <- tibble(V1 = "a", V2 = 1, Z1 = "a", Z2 = 1)
Run Code Online (Sandbox Code Playgroud)
期望的输出(mutate_at(df, "Z1", paste, "b")
但没有Z1
明确选择):
structure(list(V1 = "a", V2 = 1, Z1 = "a b", Z2 = 1), class = c(
"tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L))
Run Code Online (Sandbox Code Playgroud)
换句话说,如何"组合" mutate_at(df, vars(starts_with("Z")), paste, "b")
并mutate_if(df, is.character, paste, "b")
在一个变异中?
我正在使用echarts,尝试使echarts在x轴上显示一天的时间段时遇到问题。这是我的代码
this.area = {
color: ["#009C95","#21ba45"],
title : {
text: 'Fuel History',
textStyle: {
fontFamily: 'lato'
}
},
tooltip : {
trigger: 'axis'
},
calculable : true,
xAxis : [
{
type: 'time',
boundaryGap:false,
axisLabel: {
formatter: (function(value){
return moment(value).format('HH:mm');
})
},
data : dates
}
],
yAxis : [
{
type : 'value'
}
],
series : [
{
backgroundColor: '#4D86FF',
name:'Refuelling',
type:'line',
smooth:true,
itemStyle: {normal: {areaStyle: {type: 'default'}}},
data: historyRefuelling
},
{
name:'Fuel Theft',
type:'line',
smooth:true,
itemStyle: {normal: …
Run Code Online (Sandbox Code Playgroud)