1

I'm trying to run a daily taskscheduleR script that pulls data into R from an API. It works when I run it as a one time task but for some reason it won't work as a daily task. I keep getting the following error in the log file:

<HEAD><TITLE>Authorization Required</TITLE></HEAD>
<BODY BGCOLOR=white FGCOLOR=black>
    <H1>Authorization Required</H1><HR>
    <FONT FACE=Helvetica,Arial>
        <B>Description: Authorization is required for access to this proxy</B>
    </FONT>
    <HR>
    <!-- default Authorization Required response (401) -->

Here's the code:

library(httr)
library(jsonlite)
library(tidyverse)
library(taskscheduleR)

# Url to feed into GET function
url<-"https://urldefense.com/v3/__http://files.airnowtech.org/airnow/yesterday/daily_data_v2.dat__;!!J30X0ZrnC1oQtbA!Yh5wIss-mzbpMRXugALJoWEKLKcg1-7VmERQwcx2ESK0PZpM5NWNml5s9MVgwHr5LD1i5w$ "
# Sends request to AirNow API to get access to data
my_raw_result<-httr::GET(url)
# Retrieve contents of a request
my_content<-httr::content(my_raw_result,as="text")
# Parse content into a dataframe 
my_content_from_delim <- my_content %>% textConnection %>% readLines %>% read.delim(text = ., sep = "|",header = FALSE)
head(my_content_from_delim)

I have been using the Rstudio add-in to create the task.

TylerH
  • 20,799
  • 66
  • 75
  • 101
NBE
  • 641
  • 2
  • 11
  • 33
  • 1
    Not sure why your side appears to want authorization, but is there a reason you don't just start with `readLines(url)` and skip most of `httr::`? This worked without error: `readLines(url) %>% read.delim(text = ., sep = "|", header = FALSE)`. – r2evans Jun 18 '20 at 04:38
  • @r2evans Yeah I don't get why it says that. But when I run it as a one time task or even outside taskscheduleR it works... but no reason thats just how someone told me to do it. Thanks though! – NBE Jun 18 '20 at 12:54
  • Even doing it with `Rscript` works, so there's something going on with your environment. Sorry, no idea yet. – r2evans Jun 18 '20 at 14:47
  • @r2evans its ok. Thanks for trying. Do you think it can have something to do with getting certain admin rights? This is for my work computer – NBE Jun 19 '20 at 03:57
  • If this is a work computer, it seems reasonable to think that there might be a firewall/proxy on your network (even if not explicitly configured by you). I know many devices keep track of individual users, and as such might filter differently based on the user and/or the device trying to get out to the internet. (My CheckPoint firewall, for instance, can do SSL-interception for "security" purposes, and it is aware of the user/computer making the request.) – r2evans Jun 19 '20 at 21:41
  • @r2evans Thanks for your insight. Is there anything I can do about this? The thing that really confuses me is that it works as a one time task but not a daily.. I have also tested another script to run daily and it works .. it just doesn't work as a daily task for this specific script. – NBE Jun 22 '20 at 13:18
  • 1
    Another thought (and I really am going out of my element here): it's entirely possible that the "environment" that Windows allows for its scheduled tasks is not fully network-able, meaning for security reasons it has different rules. And since it is dependent on the security posture of your computer (e.g., [GPO](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/policy/group-policy-objects), positive tests on another computer may not apply. I don't know, sorry, I think somebody with experience with `taskscheduleR` and window's scheduling should pipe in ... – r2evans Jun 22 '20 at 15:24
  • @r2evans It's alright, thanks for trying and giving me things to think about. Hopefully, I can figure it out, it would significantly reduce me doing annoying data input haha – NBE Jun 23 '20 at 02:42

2 Answers2

0

If you are trying to access this on a work computer, you may need to allow downloads from the url link. Open a browser, paste that url, click 'allow downloads', run the script.

Anna Nevison
  • 2,709
  • 6
  • 21
  • Where do I find the "allow downloads" button? – NBE Jun 22 '20 at 13:16
  • @NBE copy that url into a browser on the computer you are having issues with, the url should start a download. if it doesn't start to download- it will normally prompt you to allow for downloads to the site. If it auto begins download or doesn't prompt you- let me know – Anna Nevison Jun 22 '20 at 14:47
  • I did what you said and it auto-downloads – NBE Jun 23 '20 at 02:39
0

I am not sure whether the solution I will offer will work for you, but it won't harm to try. If the problem related to the task scheduler, the following solution might work. However, if the problem of authorization issues, you may need to get some IT help from your workplace.

For the task scheduler issue, you can directly send your script to the windows task scheduler with a batch file and create a schedule for it.

To make it easy, you can use the following code. First, open a new folder and copy-paste your R script there. To run the following code, you should call you R script as My Script.r.

Then, in the same folder, create a batch file with the following codes. To create a batch file, you should copy the following code into a Notepad and save it as Run R Script.bat in the same folder.

cd %~dp0
"C:\PROGRA~1\R\R-40~1.0\bin\R.exe" -e "setwd(%~dp0)" CMD BATCH --vanilla --slave "%~dp0My Script.r" Log.txt

Here, cd %~dp0 will set the directory for the windows batch to the folder you run this batch. "C:\PROGRA~1\R\R-40~1.0\bin\R.exe" will specify your R.exe. You may need to change the path based on your system files.

-e "setwd(%~dp0)" will set the directory of R to the same folder in which the batch and script will be run.

"%~dp0My Script.r" Log.txt will define R script pathname and the log file for the batch.

Second, to create a daily schedule, we are going to create another batch file. To do so, copy and paste the following codes into a notepad and save as Daily Schedule.bat. When you click the Daily Schedule.bat, it will create a daily task and run for the first time in one minute, and every day it will repeat itself at the same time when you first run this batch.

@echo off

for /F "tokens=1*" %%A in ('
  powershell -NoP -C "(Get-Date).AddMinutes(1).ToString('MM/dd/yyyy HH:mm:ss')"
') do (
  Set "MyDate=%%A"
  set "MyTime=%%B"
)

::Execute path to bat path
cd %~dp0

::Create Task
SchTasks /Create /SC DAILY /TN "MY R TASK" /TR "%~dp0Run R Script.bat" /sd %MyDate% /st %MyTime% 

This code will create a task called as "MY R TASK". To see whether it is scheduled, you can run the following codes on the windows prompt: taskschd.msc. This will open your task scheduler, and you can find your task there. If you want to modify or delete, you can use this task scheduler program; it has a nice GUI and easy to navigate.

For more details about the Task scheduler syntax, see the following link

If you have any questions, let me know.

mustafaakben
  • 501
  • 2
  • 5
  • thanks for your answer but I don't believe it is a problem with the task scheduler because it works when its a one time task just not daily. But other scripts I have tested work daily. – NBE Jun 26 '20 at 13:37