Windows Task Scheduler R Script Automate

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to cradle to grave r my name is mark gingras today i'm going to talk to you about how to use the windows task scheduler to actually schedule your r script to run on a cadence every hour every minute every week however you want to do it so why do we want to do something like this well reality hits us we get an email every day from the boss with a bunch of data and every single day you're dragging that data to some folder doing some sort of analysis on it create the plot and you send it back now granted you can automate some of this with vba and say hey when this attachment comes in pull it over to this folder do all these things but that's beyond the scope of what we're about to do sorry that reality hits and you can't just get a direct line a direct pipeline to that data so it turns out that a lot of people still have to drag and drop the data update it daily well we're going to make that headache just a little bit easier today by automating the actual r script itself so long story short let's jump right into it right what i have here is a blank brand new project called windows task scheduler and what you want to do is create a project and then do a new r script so control shift n or file new r script and we can just call this whatever we want do control s and save it i'm going to call it my underscore script and it's going to be a dot r file because it's an r script so save that now what i have here is i've got a fake data csv file now you can imagine having an excel file or some other file even an access database file fro i care now every day you're dragging and dropping this to this folder and replacing the data with the new data because the scenario is you replace the data you create the plot you email the plot to your boss in fact there's some email packages for r that can actually automate some of that as well all right so the fake data let's take a quick look at it all it is is just an x and a y and it's got some fake data in here so imagine that gets updated daily right so those are the numbers for yesterday and you want to create a plot et cetera et cetera so it's called fake data let's go ahead and dive right in with a quick example so i'm going to do library ggplot2 i'll zoom in on this and give plot2 so we can get a quick graph and we'll just create my data is equal to again i'm going to do read.csv and then in quotes if i hit tab it should open up my directory structure under the project and it's called fakedata.csv right and i know that i have headers so i'm going to say header equals true and i'm going to do command enter on that but what you'll notice sadly which i'm not sure why i haven't looked it up yet but if i click on see up here if i click on my data that just got imported you'll see that the x and the y header didn't quite line up like for some reason that x is a weird weird character dot dot x we don't want that so let's fix that and just to prove to you what that is let's run down here and just do names of my data and you'll see the x turned into some weird funky thing i don't want that so let's just fix that so we'll do column names and it's a accessor method basically i'm going to access the column names of my data and i'm going to rename them i'm going to just rename them x comma y and that will give us just a little bit easier aesthetic on the eye so run that again x and y so now it's corrected no big deal let's create a quick plot so i've got ggplot we'll just do ggplot and my data will bring in and my aesthetic is going to be x is equal to x and y is equal to y and of course my easiest geometry to add is g on point boom if i run that you should get an error because i didn't put the a on the end of my data so now if i run that after i get out of the way we get a plot so there's the plot that's what it's going to do now that's not doing much yet so let's save that plot as an object i'll say my daily plot is equal to that ggplot so run that and now we'll just save it actually into our file system so we'll say ggsave and we'll just put in my daily plot comma file name is equal to my daily plot.png it's pretty safe so i'm going to run that and you'll see after i run that if i go to my folder structure here i will have a my daily plot png file if i open that up here it is it's saved every day now this is just a hypothetical example scenario obviously you're going to have more complicated and better charts than this but you still have to manually drag this thing over to an outlook or an email and send it every day so that's another thing that you might think about automating next all right but we're going to talk about scheduling this now when we do anything with the scheduler or anything with the command prompt for right now i think it's best to put in the complete absolute paths so let's go ahead and do that so when i said read.csv fake data it knew to look in my project folder but if i'm not opening up my project and running it through rstudio the command prompt doesn't know that so let's give it really explicit directions on where things are at so i'm going to open up where this fake data is and i'm going to do shift right click and do save or copy as path so copy as path and you only get that option if you hold shift when you right click so then i'm going to just go ahead and paste it in it already has quotes actually but i'll paste it right in here i don't need the fake data anymore it's already there so just clean it up a bit get rid of the quote double quote here that i accidentally have so we just want a single quote now backslashes are escape characters so we have to add a backslash backslash now this is a little painful the first time again we will build upon this and create things that are much easier later so we have that full path that's fine let's see if that works actually let's go ahead and you can delete all of the data if you'd like i'm going to just click on this little broom here gone i'm going to rerun my data boom six observations of two variables so i'm going to rerun uh everything down to gg save now again it ggsave only knows to save it in this project path because it's in the rstudio environment we're not opening up our studio to run anything we're going to be running the r script so we need to put the entire path on here again so i'm just going to copy this path up here all the way to fake data and we will put it down here so i hope you understand the importance of why i'm doing it this way okay so that should work let me make it so you could probably see the whole thing i'm going to run it now and it should still be there in fact if i come over here and delete my daily plot come back let's run the whole thing again ctrl a ctrl enter boom go back we have my daily plot again right so all right we've got it all set it's good the r script works as we want it so i'm going to save that it's called my underscore script let's not forget that now what we want to do is go to the task scheduler so in fact i already have it open let me close it we want to go to the task scheduler so click on start menu and type in task space s and you'll see it pop up go ahead and open that up we're going to do action create task create task here and then we're going to give it a name we'll just say my daily plot for boss whatever you want to call it you might have tons of these and you can have the option to run it only when you're logged in or any time that the computer is on but you happen to be logged off it can still run i'm going to keep it the way it is feel free to experiment let's go to triggers next and then we want to click on new we want to say what when does this happen i'm going to say one time now it's 4 47 now on my clock i need to give this a couple minutes so let's just do um let's just do 450 to be safe i'm going to do 450. now of course you can run this daily weekly et cetera so i've got a couple minutes to make this happen all right so i'm going to hit ok i'm only going to do it one time that's my trigger you could set yours the way you like click on actions go to new and now you'll see that i have a what's my action start a program i want to start a program and that program is going to be the r script program itself so click on this little folder if you have it down here if not you know how to get to your file explorer let's go to this pc c drive most likely 99.9 chance you're running off the c drive so c drive program files and then go down to r r whatever version you have bin for binary a binary executable file and then i have this thing called r script.exe so let's go ahead and shift right click on the r script and do copy as path okay let's go back to the scheduler and now we say hey what program do we want i'm going to do control v i want to run the r script script or the r script executable file the binary file now what are my arguments add arguments optional i do want to add arguments i want to say what do i want to run i want to run now i believe the space will be put in there for us but i want to run my r script so let's go back to our project which for me was under documents windows task scheduler and you could see here i have uh well i'm going to delete my daily plot just because i don't need it anymore and then i have this thing called my script underscore r shift right click go to copy as path and come back here and so for my arguments i'm just going to paste that in there right and i'm gonna hit ok and then i'm gonna hit ok again so those are the steps i'm gonna close windows task scheduler out completely now let's draw your attention to here i like to view this as details honestly more than anything so what you see here is i don't have my plot anymore so we're just going to sit here and wait for 450 to occur and then it's going to trigger that r script executable file to go reach out and grab my underscore script.r and run that script and when it runs that script because it doesn't it's not using our studio it's using the r itself the r executable the language and when that happens it doesn't know there's a project directory or this or that so so that's why we're very explicit about where these are located there it is and it looks like we have the plot so my daily plot did you guys see that um so that's it it worked put it on a cadence do whatever you want now there are plenty more things to automate on this you know that there's a package for that you know there's a package to say okay now that i have a a file i can move that file you can move files with r you can attach files to emails and automate sending emails with r you can do all those things so i hope this helps you begin your automation path and i'm sorry that you don't have a direct connection to the data that you want live so as long as you keep getting those emails from some business unit out there or some management or some leader or some boss then you'll have to use something like this to have that workaround now it's not the best solution but it'll definitely get you on the right path and thank you all for joining the discord channel thanks for subscribing and thanks for sharing these videos i meant to say that at the beginning but oh well i'll say it now so please again uh subscribe share like most importantly throw this on twitter throw this on social media facebook everything you can please help me out and i will see you guys in the next one [Music] you
Info
Channel: CradleToGraveR
Views: 2,727
Rating: 5 out of 5
Keywords:
Id: KSe1EHI-Us8
Channel Id: undefined
Length: 12min 40sec (760 seconds)
Published: Tue Jul 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.