如何使用null参数创建函数?

时间:2017-09-20 23:57:59

标签: r function apply lapply mapply

我有三个向量,我需要将其应用于模板,替换内容并创建新文件。这是功能:

multx<-function(){
  readLines(Template) %>%
    gsub(pattern = "stx", replace = stimearray) %>% #Replaces the start time
    gsub(pattern = "etx", replace = etimearray) %>% #Replaces the end time
    write.table(., paste0("ds/", iNames), #Writes out a file for every batch
              row.names=F, col.names=F, quote = F)
}

x <- mapply(multx)

这是创建全局变量的部分,也用于其他函数:

runStart <- lubridate::ymd_hm(startDate) #Start date 
stimearray <- runStart + months(0:(nMonths-1)) 
etimearray <- runStart + months(1:nMonths) - lubridate::dhours(1)

但在这种情况下,stimearrayetimearrayiNamesvectors,这些c("", "\"! ***********************************************************************************************************************\"", "\"simulStart stx ! (01) simulation start time -- must be in single quotes\"", "\"simulFinsh etx ! (02) simulation end time -- must be in single quotes\"", "\"\"", "\"! ***********************************************************************************************************************\"" ) 已在先前计算的全局环境中可用。

如何创建一个空的参数函数以创建一批文件?

或者,还有其他办法吗?

数据

模板

structure(c(1475280000, 1477958400, 1480550400, 1483228800, 1485907200, 
1488326400, 1491004800, 1493596800, 1496275200, 1498867200, 1501545600, 
1504224000, 1506816000, 1509494400, 1512086400, 1514764800, 1517443200, 
1519862400, 1522540800, 1525132800), class = c("POSIXct", "POSIXt"
), tzone = "UTC")

dput(stimearray)

structure(c(1477954800, 1480546800, 1483225200, 1485903600, 1488322800, 
1491001200, 1493593200, 1496271600, 1498863600, 1501542000, 1504220400, 
1506812400, 1509490800, 1512082800, 1514761200, 1517439600, 1519858800, 
1522537200, 1525129200, 1527807600), tzone = "UTC", class = c("POSIXct", 
"POSIXt"))

dput(etimearray)

<?php
  require("../config/config.php");
  require("../lib/db.php");
  $_POST['id'] = $uid;
  $_POST['pw'] = $upwunenc;
  $conn = db_init($config["host"],$config["duser"],$config["dpw"],$config["dname"]);
  $result = mysqli_query($conn, "SELECT * FROM userdata");
  $sql = "SELECT id,pw,nickname,pid FROM userdata WHERE id LIKE '$uid'";
  $result = mysqli_query($conn, $sql);
  $row = mysqli_fetch_assoc($result);
  $row['id'] = $sqlid;
  $row['pw'] = $sqlpw;
  $row['nickname'] = $sqlni;
  $row['pid'] = $sqlpid;
  $upw = password_hash($upwunenc, PASSWORD_DEFAULT);
  if ($uid === $sqlid) {
    if ($upw === $sqlpw) {
      $resulti = mysqli_query($conn, 'SELECT * FROM sitestatements');
      $sqli = 'SELECT link,pid FROM sitestatements WHERE upid = '.$_GET['pid'];
      $resulti = mysqli_query($conn, $sqli);
      $rowi = mysqli_fetch_assoc($resulti);
      $rowi['link'] = $link;
      session_start();
      $_SESSION['pid'] = $sqlpid;
      $_SESSION['nickname'] = $sqlni;
      header('Location: ./index.php');
    } else {
      echo "<script>window.alert('아이디나 비밀번호가 틀렸습니다. 회원가입하거나 다시 로그인해주세요.');</script>";
      echo "<script>window.location=('../login/login.php');</script>";
    }
  } else {
    echo "<script>window.alert('아이디나 비밀번호가 틀렸습니다. 회원가입하거나 다시 로그인해주세요.');</script>";
    echo "<script>window.location=('../login/login.php');</script>";
  }
?>

1 个答案:

答案 0 :(得分:1)

您可以使用多变量Map lapply来执行此操作:

template <- c("", "\"! ***********************************************************************************************************************\"", 
              "\"simulStart              stx     ! (01) simulation start time -- must be in single quotes\"", 
              "\"simulFinsh              etx      ! (02) simulation end time -- must be in single quotes\"", 
              "\"\"", "\"! ***********************************************************************************************************************\"")

stime <- structure(c(1475280000, 1477958400, 1480550400, 1483228800, 1485907200, 1488326400, 1491004800, 1493596800, 1496275200, 1498867200, 1501545600, 1504224000, 1506816000, 1509494400, 1512086400, 1514764800, 1517443200, 1519862400, 1522540800, 1525132800), 
                   class = c("POSIXct", "POSIXt"), tzone = "UTC")

etime <- structure(c(1477954800, 1480546800, 1483225200, 1485903600, 1488322800, 1491001200, 1493593200, 1496271600, 1498863600, 1501542000, 1504220400, 1506812400, 1509490800, 1512082800, 1514761200, 1517439600, 1519858800, 1522537200, 1525129200, 1527807600), 
                   tzone = "UTC", class = c("POSIXct", "POSIXt"))

dir.create("ds")

file_data <- Map(function(start, end, name){
        filled_template <- gsub("stx", start, template, fixed = TRUE)
        filled_template <- gsub("etx", end, filled_template, fixed = TRUE)
        writeLines(filled_template, file.path("ds", name))
        filled_template    # return vector written to file
    }, 
    start = stime,
    end = etime,
    name = paste0("file-", as.integer(stime), ".txt")
)

file_data[1:2]
#> [[1]]
#> [1] ""                                                                                                                             
#> [2] "\"! ***********************************************************************************************************************\""
#> [3] "\"simulStart              2016-10-01     ! (01) simulation start time -- must be in single quotes\""                          
#> [4] "\"simulFinsh              2016-10-31 23:00:00      ! (02) simulation end time -- must be in single quotes\""                  
#> [5] "\"\""                                                                                                                         
#> [6] "\"! ***********************************************************************************************************************\""
#> 
#> [[2]]
#> [1] ""                                                                                                                             
#> [2] "\"! ***********************************************************************************************************************\""
#> [3] "\"simulStart              2016-11-01     ! (01) simulation start time -- must be in single quotes\""                          
#> [4] "\"simulFinsh              2016-11-30 23:00:00      ! (02) simulation end time -- must be in single quotes\""                  
#> [5] "\"\""                                                                                                                         
#> [6] "\"! ***********************************************************************************************************************\""

请注意,午夜的POSIXct时间的默认打印方法省略了时间部分。如果您想要打印,请在时间上致电format,例如format(start, "%F %T")