今天我profvis
从识别需要更多时间的命令中了解了使我的代码更快的包。包提供的示例(https://rstudio.github.io/profvis/)我可以正常运行,但是当我在我的代码中尝试时,没有任何反应,它出现:(Sources not available)
。所以我想知道,我做错了什么?
library(dplyr)
library(tidyverse)
library(lubridate)
library(profvis)
df1 <- structure(
list(
Id = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1),
date1 = c(
"2022-01-12 00:00:00 UTC",
"2022-01-12 00:00:00 UTC",
"2022-01-12 00:00:00 UTC",
"2022-01-12 00:00:00 UTC",
"2022-01-12 00:00:00 UTC",
"2022-01-12 00:00:00 UTC",
"2022-01-12 00:00:00 UTC",
"2022-01-12 00:00:00 UTC",
"2022-01-12 00:00:00 UTC",
"2022-01-12 00:00:00 UTC",
"2022-01-12 00:00:00 UTC",
"2022-01-12 00:00:00 UTC",
"2022-01-12 00:00:00 UTC",
"2022-01-12 00:00:00 UTC",
"2022-01-12 00:00:00 UTC"
),
date2 = c(
"2022-01-05 00:00:00 UTC",
"2022-01-05 00:00:00 UTC",
"2022-01-06 00:00:00 UTC",
"2022-01-06 00:00:00 UTC",
"2022-01-07 00:00:00 UTC",
"2022-01-07 00:00:00 UTC",
"2022-01-08 00:00:00 UTC",
"2022-01-08 00:00:00 UTC",
"2022-01-09 00:00:00 UTC",
"2022-01-09 00:00:00 UTC",
"2022-01-10 00:00:00 UTC",
"2022-01-10 00:00:00 UTC",
"2022-01-11 00:00:00 UTC",
"2022-01-11 00:00:00 UTC",
"2022-01-12 00:00:00 UTC"
),
Week = c(
"Wednesday",
"Wednesday",
"Thursday",
"Thursday",
"Friday",
"Friday",
"Saturday",
"Saturday",
"Sunday",
"Sunday",
"Monday",
"Monday",
"Tuesday",
"Tuesday",
"Wednesday"
),
Category = c(
"ABC",
"EFG",
"ABC",
"EFG",
"ABC",
"EFG",
"ABC",
"EFG",
"ABC",
"EFG",
"ABC",
"EFG",
"ABC",
"EFG",
"ABC"
),
DR1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0),
DRM0 = c(300, 300, 300, 300, 300, 300, 300, 300, 300,
300, 300, 300, 300, 300, 0),
DRM01 = c(300, 300, 300, 300, 300,
300, 300, 300, 300, 300, 300, 300, 300, 300, 0),
DRM02 = c(
300,
300,
300,
300,
300,
300,
300,
300,
300,
300,
300,
300,
300,
300,
300
),
DRM03 = c(
300,
300,
300,
300,
300,
300,
300,
300,
300,
300,
300,
300,
300,
300,
300
),
DRM04 = c(
300,
250,
250,
250,
250,
250,
250,
250,
250,
250,
300,
300,
300,
300,
300
)
),
row.names = c(NA,-15L),
class = c("tbl_df", "tbl", "data.frame"))
profvis({
x<-df1 %>% select(starts_with("DRM"))
x<-cbind(df1, setNames(df1$DR1 - x, paste0(names(x), "_PV")))
PV<-select(x,Id, date2,Week, Category, DR1, ends_with("PV"))
med<-PV %>%
group_by(Id,Category,Week) %>%
dplyr::summarize(dplyr::across(ends_with("PV"), median),.groups = 'drop')
SPV<-df1%>%
inner_join(med, by = c('Id','Category', 'Week')) %>%
mutate(across(matches("^DRM\\d+$"), ~.x +
get(paste0(cur_column(), '_PV')),
.names = '{col}_{col}_PV')) %>%
select(Id:Category, DRM01_DRM01_PV:last_col())
})
#Larger database
df1 <- data.frame( Id = rep(1:5, length=10000),
date1 = as.Date( "2021-12-01"),
date2= rep(seq( as.Date("2021-01-01"), length.out=5000, by=1), each = 2),
Category = rep(c("ABC", "EFG"), length.out = 10000),
Week = rep(c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday"), length.out = 10000),
DR1 = sample( 200:250, 10000, repl=TRUE),
setNames( replicate(365, { sample(0:10000, 10000)}, simplify=FALSE),
paste0("DRM", formatC(1:365, width = 2, format = "d", flag = "0"))))