Scheduling with taskscheduleR setting environment variables

699 Views Asked by At

Having some trouble scheduling tasks using taskscheduleR. Specifically I have had no issues scheduling the tasks however, when I try to set environment variables, the code will fail.

I've tried explicitly calling Sys.getenv() at the begginning of my scheduled task to no avail.

I think I could do something like

out <- readLines("path to my .Renviorn")

but that doesn't seem like the right way to do it.

Here is what I would like to schedule which runs fine (even when scheduled) when I pass full argument rather than the Sys.getenv().

library(RODBC)
library(slackr)
library(tidyverse)

Sys.getenv()

dbhandle <- odbcDriverConnect(connection = Sys.getenv("CWDSN"))

proj <- sqlQuery(dbhandle, "SELECT * FROM v_rpt_Project WHERE Date_required > '2018'", 
stringsAsFactors = FALSE) %>% 
  as_tibble()

RODBC::odbcCloseAll()

slackr(proj, channel = "test-r-1", api_token = Sys.getenv("SLACK_API_TOKEN"))

write.csv("C:/Users/bill/Desktop/output.csv")

I would like to avoid passing login information that could be pushed to GitHub.

If there is an alternative method, like setting up a .config file to draw login information from, I am open to that. Kind of just looking for best practices here.

This is how I am scheduling:

library(taskscheduleR)


taskscheduler_create(taskname = "DELETE-TEST", 
                     rscript = "C:/Users/bill/Desktop/simple-test.R", 
                     schedule = "MINUTE", 
                     startdate = "01/01/2019", 
                     modifier = 1)

taskscheduler_delete("DELETE-TEST")

sessionInfo()

R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] forcats_0.4.0   stringr_1.4.0   dplyr_0.8.3     purrr_0.3.2     readr_1.3.1     tidyr_0.8.3     tibble_2.1.3   
 [8] ggplot2_3.2.0   tidyverse_1.2.1 slackr_1.4.2    RODBC_1.3-16   

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2       cellranger_1.1.0 pillar_1.4.2     compiler_3.6.1   tools_3.6.1      zeallot_0.1.0   
 [7] jsonlite_1.6     lubridate_1.7.4  gtable_0.3.0     nlme_3.1-140     lattice_0.20-38  pkgconfig_2.0.3 
[13] rlang_0.4.0      cli_1.1.0        rstudioapi_0.10  curl_4.2         haven_2.1.1      withr_2.1.2     
[19] xml2_1.2.0       httr_1.4.1       generics_0.0.2   vctrs_0.2.0      hms_0.5.0        grid_3.6.1      
[25] tidyselect_0.2.5 glue_1.3.1       R6_2.4.0         fansi_0.4.0      readxl_1.3.1     modelr_0.1.4    
[31] magrittr_1.5     scales_1.0.0     backports_1.1.5  rvest_0.3.4      assertthat_0.2.1 colorspace_1.4-1
[37] utf8_1.1.4       stringi_1.4.3    lazyeval_0.2.2   munsell_0.5.0    broom_0.5.2      crayon_1.3.4    

1

There are 1 best solutions below

1
On

There is a readRenviron() function.

readRenviron("path to environment variables")

Sys.getenv("access token")

You can now schedule tasks without calling sensitive information.

Credit: How can I make R read my environmental variables?