简体   繁体   中英

How to pass multiple list of lists as arguments to mapply function in R

Given three lists of lists ( avg_errors , year_months , dfs ) as follows:

avg_errors <- list(0.7, 0.92, 2.35)

Out:

[[1]]
[1] 0.7

[[2]]
[1] 0.92

[[3]]
[1] 2.35

and:

year_months <- list(list('2021-12'), list('2021-11'), list('2021-10'))

and data 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"))

How could I pass these variables as arguments to the function Plotting() as follows:

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)

The current code gives me: Error in (function (data, errors): argument no use (dots[[3]][[1]]) .

How could call the function and pass arguments? Thanks for your help at advance.

I'm not sure if this is your desired outcome, but my guess is, that you don't need the for-loop's inside the Plotting function, since your are already looping over your input with 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)

Created on 2022-01-23 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM