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

问题描述 投票:0回答:2

我有几个 R 脚本,我计划通过

taskscheduleR
包运行。如果它们运行的话,我会在它们运行时不断收到错误。

第一个使用

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

# 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

这个甚至没有运行,但任务调度程序说它已完成 - 我从任务调度程序中得到的信息不多,所以我知道它没有运行(当我在 R-Studio 中点击运行时运行良好):

# 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())

这个运行数据库部分,但最后不会写入文件(但当我从 R-Studio 中点击运行时会写入文件):

# 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())

有什么想法吗?也许是权限问题?

r automation zip scheduled-tasks
2个回答
2
投票

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

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

当在路径中放置反斜杠并在路径中的每个空格之前放置普通斜杠时,它对我有用,例如:

G:/桌面\工作\文件/FridayFile.xlsx


0
投票

感谢您的帮助!

由于路径问题,通过 Windows 任务计划程序写入文件失败。我一上午都找不到正确的答案。最后我找到了我的问题的正确答案。所以我重新发布了正确的答案(而且我还不允许喜欢它)。也许会更快找到。

在 R 脚本中,我在文件路径中的每个空格之前放置了一个反斜杠(如下面示例中的“测试文件”)。这解决了问题。

自己尝试一下,R 脚本:

library(openxlsx)

wbc <- createWorkbook()

addWorksheet(wbc, "MTcars")

writeData(wbc, sheet = 1, mtcars)

saveWorkbook(wbc, file = "G:/RStudio/test\ files/result/MTcars.xlsx", overwrite = TRUE)
© www.soinside.com 2019 - 2024. All rights reserved.