bnosac / taskscheduleR

Schedule R scripts/processes with the Windows task scheduler.
330 stars 72 forks source link

Set working directory for writing files from script to local disk in taskscheduler #60

Closed wgruber closed 5 years ago

wgruber commented 5 years ago

Hi,

I'm using taskscheduler to run a script which reads and analyzes data from a questionaire server and writes the result in different formats (xlsx, RDatat, csv) back to my local disk. Everything works fine, the only thing I can't change is the directory in which the data is written. If I create a directory in the ../System32 folder (e.g. ..System32/Daten) and change the permissions for this folder to read/write, the scheduler does its job. But I don't actually want to write to the systems directory. If I run the script directly from the windows task scheduler and if I set in the scheduler the optional parameter for the output directory the way I choose, everything works fine. So far I haven't found out the correct settings for the taskscheduler_create function to make it work directly from R (i.e. without setting the desired directory manualy in the windows scheduler). Do you have any suggestions?

Code used to create the task: S2S <- paste0(here("RProgs"), "/", "LimeSurvey_Data_SG.R") taskscheduler_create(taskname = basename(S2S), rscript = S2S, schedule = "ONCE", starttime = format(Sys.time() + 60, "%H:%M"), debug = FALSE)

Thanks for your efforts, Walter

jwijffels commented 5 years ago

Use setwd in your script? Or provide the full path to where you want to write files to instead of a relative path. Learn to use environment variables that stores paths and use this in your r script. And don’t use here in your script as here only works for rstudio projects and does not find the location back if your script is launched as the system user. Use environment variables

wgruber commented 5 years ago

Thank you very much for your quick response! So far I've tried to avoid setwd() and environment variables - both caused more troubles than they did help. But in the current case I can see your point - I'll try setting the environment variables and/or using setwd() right away.

jwijffels commented 5 years ago

Just replace all your calls to here with Sys.getenv("YOUR_PROJECT_FOLDER") and set YOUR_PROJECT_FOLDERin the environment variables of your computer. No need to use setwd(), using environment variables are the way to automate thing.

wgruber commented 5 years ago

Great, just tried it out and it worked PERFECTLY! Many, many thanks - helped me a lot!!!!

wgruber commented 5 years ago

Hi,

Thank you so much, your advices really helped!!! I’ve got it all running now and my Updated for the files is now running automatically every day at 08:00☺.

All the best

Walter


Ing. Dipl.-Ing. Dr. Walter Gruber Universität Salzburg Fachbereich Psychologie Centre for Cognitive Neuroscience Salzburg (CCNS) Tel.: +43 (0662) 8044 – 5115 Fax: +43 (0662) 8044 – 5126 Email: walter.r.gruber@sbg.ac.atmailto:walter.r.gruber@sbg.ac.at Web: https://ccns.sbg.ac.at/people/gruber/

Von: jwijffels [mailto:notifications@github.com] Gesendet: Samstag, 15. Dezember 2018 18:27 An: bnosac/taskscheduleR Cc: Gruber Walter Roland; Author Betreff: Re: [bnosac/taskscheduleR] Set working directory for writing files from script to local disk in taskscheduler (#60)

Just replace all your calls to here with Sys.getenv("YOUR_PROJECT_FOLDER") and set YOUR_PROJECT_FOLDER in the environment variables of your computer. No need to use setwd(), using environment variables are the way to automate thing.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/bnosac/taskscheduleR/issues/60#issuecomment-447584201, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AIcyVA7XqRoWsUQbcCyEBHS8nMdmdrD7ks5u5TDWgaJpZM4ZUR6Z.