給定三個串列串列 ( avg_errors, year_months, dfs),如下所示:
avg_errors <- list(0.7, 0.92, 2.35)
出去:
[[1]]
[1] 0.7
[[2]]
[1] 0.92
[[3]]
[1] 2.35
和:
year_months <- list(list('2021-12'), list('2021-11'), list('2021-10'))
和資料dfs:
dfs <- list(structure(list(id = c("M01", "M02", "S01"), `2021-10(actual)` = c(8.9,
15.7, 5.3), `2021-11(actual)` = c(7.3, 14.8, 3.1), `2021-12(actual)` = c(6.1,
14.2, 3.5), `2021-12(pred)` = c(6.113631596, 14.16243166, 3.288372517
), `2021-12(error)` = c(0.719846116, 0.154463985, 1.225992602
), mean_col = c(7.43333333333333, 14.9, 3.96666666666667), act_direction = c(-1.2,
-0.600000000000001, 0.4), pred_direction = c(-1.186368404, -0.637568340000001,
0.188372517)), row.names = c(NA, -3L), class = "data.frame"),
structure(list(id = c("M01", "M02", "S01"), `2021-09(actual)` = c(10.3,
17.3, 6.4), `2021-10(actual)` = c(8.9, 15.7, 5.3), `2021-11(actual)` = c(7.3,
14.8, 3.1), `2021-11(pred)` = c(8.352097939, 13.97318204,
3.164682627), `2021-11(error)` = c(1.998109138, 0.414373304,
0.342072615), mean_col = c(8.83333333333333, 15.9333333333333,
4.93333333333333), act_direction = c(-1.6, -0.899999999999999,
-2.2), pred_direction = c(-0.547902061, -1.72681796, -2.135317373
)), row.names = c(NA, -3L), class = "data.frame"), structure(list(
id = c("M01", "M02", "S01"), `2021-08(actual)` = c(12.6,
19.2, 8.3), `2021-09(actual)` = c(10.3, 17.3, 6.4), `2021-10(actual)` = c(8.9,
15.7, 5.3), `2021-10(pred)` = c(9.619846116, 15.54553601,
6.525992602), `2021-10(error)` = c(0.945567783, 4.883250027,
1.215819585), mean_col = c(10.6, 17.4, 6.66666666666667
), act_direction = c(-1.4, -1.6, -1.1), pred_direction = c(-0.680153884000001,
-1.75446399, 0.125992601999999)), row.names = c(NA, -3L
), class = "data.frame"))
如何將這些變數作為引數傳遞給函式Plotting(),如下所示:
library(gt)
library(tidyverse)
Plotting <- function(data, year_months, errors){
for (current_month in year_months){
for (avg_error in errors){
p <- data %>%
gt() %>%
tab_header(
title = gt::html(glue("<span style='color:red'>data for {current_month}</span>")),
subtitle = md(paste0("The average error for this month is: <em>", avg_error, '</em>.'))
)
return (p)
}
}
}
mapply(Plotting, dfs, year_months, errors)
當前代碼給了我:Error in (function (data, errors) : argument no use (dots[[3]][[1]]).
如何呼叫函式并傳遞引數?提前感謝您的幫助。
uj5u.com熱心網友回復:
我不確定這是否是您想要的結果,但我的猜測是,您不需要Plotting函式內部的 for 回圈,因為您已經使用mapply.
library(gt)
library(tidyverse)
library(glue)
Plotting <- function(data, year_months, errors){
p <- data %>%
gt() %>%
tab_header(
title = gt::html(glue("<span style='color:red'>data for {year_months}</span>")),
subtitle = md(paste0("The average error for this month is: <em>", errors, '</em>.'))
)
print(p)
}
mapply(Plotting, dfs, year_months, avg_errors)
由reprex 包(v0.3.0)于 2022-01-23 創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/420130.html
標籤:
下一篇:如何計算R中的三分位數?
