Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过Windows任务计划程序运行的R脚本失败_R_Automation_Zip_Scheduled Tasks - Fatal编程技术网

通过Windows任务计划程序运行的R脚本失败

通过Windows任务计划程序运行的R脚本失败,r,automation,zip,scheduled-tasks,R,Automation,Zip,Scheduled Tasks,我有几个R脚本,我已经计划通过taskscheduleR包运行它们。当它们运行时,如果它们运行的话,我会不断在它们身上发现错误 第一个使用zip包来简单地压缩几个目录(这一个我甚至尝试将所有文件移动到C驱动器,但仍然失败,出现相同的错误): 只有在通过任务计划程序运行时,我才会出现以下错误(当我点击run时工作正常): 这一个甚至没有运行,但任务调度器说它完成了-我没有从taskscheduler得到长时间,所以我知道它没有运行(当我在R-Studio中点击run时运行良好): #库加载----

我有几个R脚本,我已经计划通过
taskscheduleR
包运行它们。当它们运行时,如果它们运行的话,我会不断在它们身上发现错误

第一个使用
zip
包来简单地压缩几个目录(这一个我甚至尝试将所有文件移动到C驱动器,但仍然失败,出现相同的错误):

只有在通过任务计划程序运行时,我才会出现以下错误(当我点击run时工作正常):

这一个甚至没有运行,但任务调度器说它完成了-我没有从taskscheduler得到长时间,所以我知道它没有运行(当我在R-Studio中点击run时运行良好):

#库加载----
如果(!require(pacman))安装程序包(“pacman”)
pacman::p_装载(
“tidyverse”
,“dbplyr”
,“DBI”
,“odbc”
,“readxl”
)
#加载Excel文件----

df我也有同样的问题:当我在R studio中运行脚本时,一切都很好。但当我使用任务计划程序(Windows而不是taskscheduleR软件包)时,我遇到了一个错误:

ifelse(附加“a”、“w”),encoding=fileEncoding):无法打开连接

当在路径中的每个空格前放置反斜杠和正斜杠时,它对我很有效,例如:

G:/Desktop\Working\Files/FridayFile.xlsx

# Lib Load ----
if(!require(pacman)) install.packages("pacman")
pacman::p_load(
    "zip"
    , "RDCOMClient"
)

# Zip Files ----
zipr(
    zipfile = "C:\\path_to_desktop\\Code.zip"
    , files = c(
        "S:\\path_to_files\\R"
        ,"S:\\path_to_files\\SQL"
        ,"S:\\path_to_files\\VB"
        )
    , include_directories = TRUE
    )
Loading required package: pacman
Error in zip_internal(zipfile, files, recurse, compression_level, append = FALSE,  : 
  Some files do not exist
Calls: zipr -> zip_internal
Execution halted
# Lib Load ----
if(!require(pacman)) install.packages("pacman")
pacman::p_load(
    "tidyverse"
    , "dbplyr"
    , "DBI"
    , "odbc"
    , "readxl"
)

# Load Excel File ----
df <- read_excel(
        path = "G:\\Desktop Working Files\\FridayFile.xlsx"
        , sheet = "Sheet1"
    )

# Make sure records are distinct
df <- df %>% 
    distinct()

# DB Connection ----
db_con <- dbConnect(
    odbc(),
    Driver = "SQL Server",
    Server = "server_name",
    Database = "db_name",
    Trusted_Connection = T
)

# Insert Records ----
dbWriteTable(
    db_con
    , Id(
        schema = "smsdss"
        , table = "c_friday_file"
    )
    , df
    , overwrite = TRUE
)

# DB Disconnect
dbDisconnect(db_con)

# Clean Env
rm(list = ls())
# Lib Load ----
if(!require(pacman)) install.packages("pacman")

pacman::p_load(
    # DB Packages
    "DBI",
    "odbc",

    # Tidy
    "tidyverse",
    "dbplyr",
    "writexl",

    # Mapping Tools
    "tmaptools"
)

# Connection Obj ----
con <- dbConnect(
    odbc(),
    Driver = "SQL Server",
    Server = "server_name",
    Database = "db_name",
    Trusted_Connection = "TRUE"
)

# Tables ----
pav <- ...

pdv <- ...

# Query ----
geo_add <- ...

a <- pdv ...

add_geo <- a %>%...

# Make df ----
df <- add_geo ...

# Geocode File ####
# Initialize the data frame
geocoded <- data.frame(stringsAsFactors = FALSE)

# First Loop ----
for(i in 1:nrow(origAddress)) {
    print(paste("Working on geocoding: ", origAddress$FullAddress[i]))
    … code
}

# Get Non Found Records ----
# Get all records that were not found and geocode on city/town, state, zip
for(i in 1:nrow(origAddress)) {
    … code
}
# Clean up Records ----
geocoded <- origAddress %>%
    ...

# Insert into tbl ----
dbWriteTable(
    con
    , Id(
        schema = "smsdss"
        , table = "c_geocoded_address"
    )
    , geocoded
    , append = T
)

# Delete Dupes ----
dbGetQuery(
  conn = con
  , paste0(
    "
    DELETE X ...
    "
  )
)

# DB Disconnect ----
dbDisconnect(conn = con)

# Save missing ---- this is what fails
origAddress %>%
  filter(is.na(lat)) %>%
  select(Encounter, FullAddress, ZipCode, PartialAddress) %>%
  write_xlsx(
    path = "S:\\path_to_file\\daily_geocode_file.xlsx"
    , col_names = T
    )

# Clean env ----
rm(list = ls())