Beyond Static Reports With R Markdown | RStudio Webinar - 2017

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] all right so here we're gonna talk about beyond static reports with our markdown so hopefully everyone is is somewhat familiar with with our markdown but it's a file format that allows us to to create HTML PDF reports Word documents you can knit two presentations but but there's a whole world kind of beyond that of our markdown and it's it's a really flexible format allow allows us to do a lot of different things hmm so we're just going to gonna walk through a couple of those different uses today and with that let's let's go ahead and jump right in so hopefully everyone can see the IDE that I have pulled up here this is kind of a classic are markdown report so I'm going to call it a static report I'll go ahead and admit it so we can can just know what we're talking about and so just grinding through code so this is gonna create a report for Facebook right for FB pulls in the the price history of Facebook it charts it there's this really simple well we're calling a forecast model down here in the bottom and hopefully this looks really familiar to everyone as something you can accomplish with our markdown right this is basically an HTML file we could we could save this and send it to whoever we're wants and they would receive it and be able to read our work the reason I think of this as being kind of the most static I'm almost even hard-coded you can almost think this is a hard-coded our markdown file is that everything is kind of explicitly defined here right this is a report and I'm saying it's for FB I'm passing in this ticker symbol FB there's a few more examples where it's just fbfbfb that's really the guts of what's going on here and that's great come but this is this is not really a document that's easily kind of changed or used for different purposes right so we can imagine if somebody said wow this this report looks really great could you could you set it up for Google or for Amazon for net for Netflix right some other stock and and we could definitely do that right we could we could just do a ctrl F and look for all the instances of FB and replace it with something like Google and we could say replace replace replace that that would work right it's it's a probably a bad idea for a few reasons you know one of which would be that this is a really simple report though we would still almost inevitably make a mistake at some point as you can see right here I have googan I'm actually pulling in data from Google so if I did another find in replace on goo this immediately would mess me up but even if we were really meticulous about that you know for one thing we've we've lost our original report on Facebook and so we could say well no problem we'll just make a copy of this file we could call the first one Facebook port our MD and then excellent Google dot report not our MD etcetera etcetera that would also work but if we ever wanted to make any changes to maybe this this this forecast down here maybe we want to make this maybe a little bit more complicated or a better model instead of just calling this simple one we'd have to change it in every single one of those files so our markdown has a more flexible way of doing this and that's this concept of a parameterised report okay so so this file is going to it's going to produce basically the exact same report we were just looking at for for Facebook I'm just I'm going to knit it here just so we can look at it and confirm that I'm not lying so here's the exact same report the difference is that up in the yam while I created a parameter and I named that parameter symbol because these are ticker symbols and I gave it a default you have FB right so if we go back to our original document you can see that the animals look slightly different but now if you search through this document you won't see FB appearing anywhere else you'll just see params dollar symbol and here's params dollar symbol so now if I want to create what we call a variant of this report from myself as the the creator of this report I don't have to do a ctrl F and and replace FB with with Google I could just do it right here right then I could say agree okay I'm just gonna pass in this ticker so everywhere that originally there was FB now there's this goog this is one way you could do it you could just change the default value that's perfectly valid way and okay it works you could also when you knit this instead of just clicking on the net if you click on this downward arrow you say I want to knit it with parameters because what we've created here is as a parameter we've given it the name symbol okay so maybe looks a little familiar from from the shiny world and actually we get this little interface and it's gonna say okay well what what ticker do you want you see I put in I can put in Amazon I could go back to FB I could go away from the tech world and say hey well well Goldman but in any event to me this this report has taken one step away from its original static hard-coded nature right because it's a lot more flexible now for me to pop in different stock tickers and indeed any parameter that you wanted to pass in to save this model you could parameterize so this is kind of the first step right towards towards to me making this a more dynamic document again I think two valuable things one less likely to make a mistake - now I can I can change this underlying code without having to worry about doing it for all these - all these different variants in this report so I'm going to just kind of kind of head-on here because that that very simple case probably doesn't doesn't really jump out as being all that useful so let's look at this slightly more complicated case so here I've almost intentionally created a lot of parameters that they don't even kind of gonna fit on one screen so this is a script that's going to take gonna take three stocks these default values to Google Amazon and Facebook there's the parameters or stock one stock two stock three each of them gets a weight we're gonna create a portfolio stocks so they they need a way to eat in that portfolio we're gonna assign an interest rate and we're gonna have a starting date and one of the things so we're still up here in the ammo and one of the things that this is going to show I hope is that the parameters don't just have to be text they can be numeric alright so the weights are all going to be numerix this this risk-free interest rate is going to be a slider input this is going to be a date let's just knit this and it's kind of default format hopefully it all it'll help show how we're moving towards the things being a little bit more dynamic right and some of it depends on the point of view from which we're defining dynamic versus static this point to me this document has become much more dynamic from our perspective as the creators of this document from the end user or the end consumers perspective this is still static they're still getting an HTML or a PDF report from us right from our perspective it's more dynamic we can change it faster so here's with that that document knits - it's gonna take our three stock tickers combine them with the waiting's that we gave them it's going to tell us how the dollar would have grown it calculates the Sharpe ratio which measures the risk/reward trade-off of this portfolio on page two here just shows us the monthly percentage returns of these individually if that was important to us right but there's a lot of there's a lot of parameters here right so hopefully this demonstrates a little bit more how if we wanted to create a variant of this if this had been our original hard-coded example where we were going to go through and do a control F on every single parameter that we needed to change that would get a little bit complicated here and we would become I think more likely to make an error right so we probably don't want to be doing a control F here and trying to change things throughout the code right so and this also I think shows of why it's it's going to get difficult to just change things directly in the am alright here was easy to change the different tickers there's only one so we can pretty easily change the symbols now when we have to have more and more we want to actually really make use of this knit with parameters button so let's see what that looks like with these different formats so one we can we can change the title how about energy stocks BP Chevron is another one exxon so these are the these are all text inputs as we had before but now we have these numeric inputs right so we can change the weighting this when we do a 40 30 30 sure how about the risk-free rate we're assuming we could take it up that's gonna make our Sharpe ratio go down the start date oh sure 2013 so hopefully this is showing that that this is at least my opinion greatly decreasing the likelihood we're gonna make an error and miss one of these parameter somewhere and it's making it a lot easier to kind of kind of create variance in this report and I'm gonna walk through the code a little bit once we're done here but I just wanted to show what the end result is gonna look like so the we're not just walking through code sort of blind alright so here's a here's a new report write growth of a dollar invested in I mean energy has not been a great place to be for the last four years it actually has a negative Sharpe ratio but you can see them you know by changing all those parameters we change the title now it's called energy stocks right here's the individual returns so it's pretty fast to iterate through different variants of reports for us as the are coders again what what the end-user would be receiving here would still be quite static so we'll just quickly look at how these parameters get get passed down into the code that that portion of it is actually you know it's pretty that part's pretty intuitive you create your parameters up here and then you pass them into the code the same way you would anything else right so so this is just a function it's not being affected at all by any of the parameters right so kind of this the same as the way we load it up an unpromising report start with your start with your packages and create whatever sort of function that you want and then here's where we start passing in parameters right we have a portfolio name that was one times our first parameter stock one that's our second parameter started here with with a date graham dollar date so we're just passing these parameters the same way as we would if we were quote-unquote hard-coding them but this just makes it a lot easier to to create variants of this so again this is all sort of from the our coders perspective this has become a more dynamic document and a lot easier to to change and we if we did want to update this model in some way you know maybe we maybe we want to change this function to daily stock returns or weekly stock returns you know or indeed maybe if we wanted to have that be parameterised you know we can make this we could add a parameter here make this attacks and call it something like my params dollar period or something like that right and then we could create a parameter up here and then all of a sudden we'd be able to toggle between monthly weekly daily two different types of period returns all right so that's that's kind of another at least to me another nice thing about parameterizing these is that is that it's a little bit of a mindset shift because now all of a sudden this to me anyway this feels like a more dynamic document and I can start to think well what can I promote our eyes what would be interesting to parameterize well here I think this period might be interesting to parameterize we're getting along returns with a parameter in our parameter as that maybe we just want if we don't want log returns for whatever reason if we want continuous returns the the opposite side of that coin of course is starting to think about well what what should I not be parameterizing right maybe we want to enforce a risk-free rate so maybe we might want to obliterate this and just quote unquote hard-code the risk-free rate and say this is one thing we're not going to toggle it's an assumption it's an unchanging assumption let's change everything else and see how the different reports come out so a lot of flexibility again this is for us as the as the creators of the reports right all right so to me the ability to secrete these multiple parameters takes us another step towards towards being more dynamic right so this is the original hard-coded right then we just had had one parameter and then we have a lot of parameters and because maybe eight total parameters right we could keep going you could put ten there's really really no limit to the number of parameters we can put in here and then just you know we knit with parameters it's going to give us a nice interface for changing the mom all right so now you know hopefully I've convinced you that this parameter is a ssin is a valuable thing for us as the creators of these reports but let's take the next step to to to shiny and this is where things are just they're always going to be dynamic in the sense that even the end user is going to receive a dynamic document and again a lot of the important stuff is happening up here in the amyl which to me is kind of the good news even though as our coders maybe the Emel isn't the most intuitive place for us to be you know it's it's nice that we can just make these changes up here and not have to touch our code a lot there are some changes in the code but this is really where a lot of the important changes in a format are happening I'm just again gonna gonna quickly knit this and then we're gonna we're actually gonna go way back to the beginning of shiny apps but I just want to show where we're headed with this shiny world so this is you know the things you could say the most sort of dynamic iteration of this in the sense that here we would be delivering this app to an end consumer we'd be saying you can choose your own stocks and your own weights right and you can see in you know real time how they change so this is kind of inherently dynamic it's it's never gonna take a static state and just stay there so both for us as the the creators and for the end consumers this is always going to be a dynamic document and it looks very similar to those reports right I'm gonna I'm gonna come back to to the code here I just wanted to quickly show where we're headed so that we can kind of get a picture of it so this is this is the code that was used to create this it's very similar to the code that was used to create those reports that the main difference is up here in the yamo we have this runtime shiny instead of having all these parameters right so we include this runtime shiny line our machine our local our desktop when we're running this or whatever server this is sitting on dosed okay this is this is a shiny app and when our machine sees these parameters it knows okay this is a this is a parameter edge report or if we had this sitting on a connect server somewhere right but just in case there's maybe some people out there who are more familiar with the traditional way to create a shiny app I'm gonna take a brief departure here and just just start at the beginning with shiny and then we'll come back to this and we'll go through the code so we came to a five minute departure here okay so we here's kind of a classic shiny app it's sitting in an app dot our file you know this is the the hello world example right one of the hello world examples where it can change the number of bends and see how it affects the histogram right so we have our inputs reactive sitting between these and then and then our output switches in this histogram alright so here it is in the appetite of our file and then here is just a Flex dashboard skeleton with runtime shiny so if you want to just get started and and began kind of playing around with with Flex dashboard and the ability to create shiny apps there what you can do is you can take your entire app dot our file and you can copy it alright so I just need control a control see there this is just if you want to get started playing around and you can can you can copy the entire app dot our file into that first coat chunk right library shiny UI fluid page and you can run this document within the RMD and it will will basically reproduce that shiny app in a Flex dashboard format so I just copied the app that are over 2 flex dashboard and here's pretty much that exact same map right the reason I think is useful just to do this in the beginning if you're just getting started and you're more used to app dot R than using Flex dashboard and our markdown for creating shiny apps you can kind of deconstruct the shiny app and put it into its component pieces and see what what is needed and what is not needed and I our markdown base shiny app versus the more classic app dot our shiny app so the the first thing that's not needed is just library shiny and get rid of that because we've already called runtime shiny so we don't need that there's really no concept of UI and server and in the Flex dashboard in our markdown so we can we can get rid of that you can see we we had to do a lot of work here to tell the UI this is a sidebar layout it's a sidebar panel a fluid page title panel that's not really how it's handled in in our markdown file we just tell it up here this is a sidebar right and then we we drop in a line of dashes and that's how Flex dashboard and our markdown knows there's a there's an element coming here and we thought it that it's a sidebar now we can actually we can get rid of this fluid page we can get rid of sidebar layout in sidebar panel because I just told it it's a sidebar but to me this is gonna be a just a much simpler app right now that's gone numb okay so this is our sidebar I've called it inputs you can call it whatever you want you call it sidebar you could you really just give it the name any name you want this is really what's what's telling our markdown that this is a sidebar for inputs and I think we just want that slider there for inputs right we don't really want our outputs sitting in the in the sidebar we're gonna make that its own code chunk yeah we'll come down here we'll create a new coat chunk for our outputs okay now we have all these kind of extraneous parentheses we can get rid of those main panel we don't need that main panel and the reason why is that we've indicated here that this is a sidebar we want to let let our markdown know that we're done with the sidebar by putting in another another line of these dashes so now our markdown knows okay we're done with this sidebar let's go to the main panel we don't need this anywhere alright we said there's no server or UI logic way to deal with function input output I want to deal with that okay getting pretty close I promise shiny app UI well we got rid of UI in server take this out this is an orphan now take that okay what's nicer so we have our our inputs we have our outputs know you I know server logic really need it here you can get rid of some of this white space does really matter let's see what this looks like when we run it always an adventure to do these live never quite know what's gonna happen when we deconstruct and reconstruct okay so this looks like a pretty close approximation of what we had before in our app dot our file it's probably a matter of personal preference or just aesthetics which one we prefer there's not really a big substantive difference between what you can do an app dot are and what you can do in in our markdown shiny file I personally like this format better I think this is simpler I think that you could probably show this to someone who wasn't all that familiar with what we're doing here today and say this is this is the sidebar it has a slider input and these are where the outputs go the output is going to be at that histogram so to me this is just a little bit more intuitive and a simpler way to layout your shiny apps there's nothing wrong of course with using the classic method of F that are but today we're just talking about our markdown so so this is the way we're going with today it is actually you could make this even more parsimonious you could take out this you don't need plant output just pot output dollar this plot you can take that out we can knit this it would be would be fine just prove I'm not lying hopefully this is going to be fine okay so it looks the exact same I like to keep it in there though and again this really is just a matter of preference I just find kind of mentally having this at the top it makes it easy to just glance up and see that this is this is where I'm putting it output it's not necessary to have it in there so just just to kind of complete this and show what I think is kind of the power of using our markdown for creating shiny APS's let's say we wanted to add another plot that was responsive to another slider or really any input it's it's really fast and easy to do that or markdown so this is how you add a second slider just copy/paste it I'm gonna call it pins to distinguish it from input pins one and I think we want another histogram it kind of be responsive to that one and one this to be responsive to pins two we'll call this upper two and this output two so let's just knit this up all right so now we have a histogram on the left is responsive to our first slider histogram on the right is responsive to our second slider we could put these on different pages we could reorient them we could have a sidebar that just has the first slider and then on the second page slider that has that just has the second one the point hopefully that that I'm demonstrating but it is really easy to add new elements and drop things into an our markdown flex dashboard when you're building your shiny apps for me I just find it a little bit easier than dropping elements into the kind of classic app dot our file where I would be dealing with you know these three calls if I wanted to put in a new input and then I need to deal with my server logic I just think it's a lot easier to kind of drop things and there's a whole website devoted to flex dashboard called our markdown there's two slash flex dashboard and then there's a whole section on SHINee so I'm not going to go through this but this is all out here the different different layouts over here right some different examples of shiny or here so anyway I highly encourage if you'll want to get into building SHINee with with our markdown this website is a good place okay so that was a little bit of a have a detour just to to make sure we're all on the same page in terms of how to port over SHINee apps that were constructed using the app that our format over to a Flex dashboard so now let's look let's go back to a tool the Flex dashboard or the shiny up we want to look at it's basically going to be built on the exact same components as our example right so here's our example the key line here is runtime shiny um same here runtime shiny we're gonna we're gonna load up our packages here in the setup chunk and we're going to define our functions right so very similar to to any other shiny app you can define whatever functions you want up here in the set up and then they're accessible to you a question though that we get a lot about SHINee in general is well what functions can I drop into a shiny app the answer is any of them any function you can create here can be called in this app so then just to kind of refresh everyone what this app looked like because I know we took a detour over there to look at the hello world example well we have this this sidebar and our hello world example of this was just a slider input but here we have the stocks I throw weights starting date the mystery rate this is sitting in our sidebar and the actual chart is sitting over here in our in our row so that's happening right here at our sidebar okay the reason is double line instead of single line is that you'll notice when we toggle back to it that this sidebar only appears on the first page of this app doesn't appear in the second page okay so here's how we constructed the sidebar for this shiny app and I actually did use the the fluid row format here and there's a reason for that which is that this shiny app is trying to put a lot of elements into this sidebar alright so we have 8 inputs if we didn't go with that fluid road they would just be stacked so we would have 8 different boxes 1 2 3 and it would start to you I think kind of hard to see it on one screen so the reason that I went with that fluid row is just to be able to put them next to each other it's also a little bit more intuitive to have the weights there so the the point is that you're not going to lose the ability to put to put you know nuanced layouts into your shiny app by going with our markdown the same things that were available to us and an app that are available to us within the sidebar I still think it's a little bit easier just to house them within just this sidebar call so we we have our reactive our inputs and stock 1 weight 1 the basically we're mirroring the the parameters that we had here and I'm parameter eyes report right stock - wait - and I'll stop toggling in a second because I'm sure everyone kind of gets it stocked - wait - the code is is very similar it has a different layout because in this case we're you want the user to have the chance to actually do it themselves and then we come down to where we're actually gonna going to be passing those reactive inputs to the function that we returned that we defined up in the setup code junk so we pass in a year and one two three and we calculate portfolio growth we're gonna calculate the Sharpe ratio which is what's sitting in those value boxes these are knowledge calculations we haven't actually done any charting and graphing yet and this is this is an interesting place to look and a shiny app so here's three lines that are layout oriented they're not substantive we're telling that with this double lines we're telling this shiny app this is page one and we're saying this is this is the first row of page one and this is the name of it so remember that portfolio growth row group one time maybe we want to call it something like portfolio performance let's see how that affected the end result of the app so now this is one portfolio performance when we talk about page two you're gonna notice the sidebar is gonna go away I know it actually I'm sorry reason the sidebar doesn't go away is that we defined it as a universal sidebar if you come back up here to the top see these double lines are telling this app this sidebar is universal normally this is how you define a page but since we called sidebar first flex dashboard and our markdown now okay we're calling a universal sidebar though we can keep keep toggling no matter what page we go to so that's kind of a substance of choice do we want each page to give the user the ability to change inputs and outputs some sometimes yes sometimes nose and yells we won't want that so back to page one which was portfolio performance we said this is going to be our first row we're gonna call it growth of $1 and then very similar to what we did in our hello world example we have in this case it's a dy graph output and our hello world example we just called it plot output because it's kind of dozen besar we're using a simple plot of the histogram here we're using dy graph so we're going to tell you call GUI graphs I'll put in dollar graph rendered I do my graph and we're gonna we're gonna pass in our reactive to it here so very similar to actually our hello world example the only difference here is we have a couple of functions more complicated functions sitting in between our inputs and our outputs right that's really the only difference is that instead of just passing in these tickers straight to our output we're running them through through these functions here other than that this is really just the exact same as just hello world example so so hopefully I'm convincing you that you can take your app thought our shiny apps in and and kind of parse them into an R markdown file and have them do the same thing and what I think is a nice format then let's let's take one more look at this we're not just looking at code you might have noticed that in addition to the chart that's showing the growth of a dollar which is here which is here we also have these two value boxes it's down here and those are gonna be responsible to any any change that we make there to activating Sharpe ratio all right so so let's look at look at the Flex dashboard code and how we actually put this into the file well we had to tell our markdown there's a new row coming because the first row had that chart that was showing the growth of a dollar so we had to tell it okay there's a new row and first piece of that row is gonna be called the Sharpe ratio of our portfolio and it's a value box Ken and it's gonna again depend on that reactive Sharpe ratio that we defined up above which again is really just the inputs passed through some functions right so we said okay new row first value box is here and then with these three hashes we said there's a second there's a second element coming in that second element is also going to be write a value box okay so that's how we told that's how we told our markdown that there's a new row it's going to be these value boxes and we don't want them to be very high because we're just showing a couple of numbers so we you said data height 200 right which is really just an aesthetic that we're setting and it's a relative aesthetic in the sense that it's not an absolute to 200 it's it's going to say well if this is 600 down below was 200 it's going to set these heights relatively which is why down here we said okay let's make this data height 350 this is just some background on the Sharpe ratio it's it's really just here to kind of show that you can also include static content not everything has to be reactive when you're when you're building a shiny app in our markdown you can still put in just like you always can in our markdown you can still put in your your text right so if you want to have some some tech in here and you want to write something up about your shiny app you can still drop that in to your to your shiny application right so I just said this is going to be a new row and I said let's give it a title background but then this is not in a coat chunk right this is just text the same as we would always be doing with our with our our markdown files and and then we said okay let's drop in a second page where we can just show the monthly returns this is this I think of is almost a way of just telling people you know we're doing all this these calculations we're passing all this data through these functions we're calculating the growth of a dollar or showing the Sharpe ratio but if you want to look at the we could think of as almost like raw data just the monthly returns for each of the stocks that you selected here it is right so this is probably just a personal preference of mine I almost always like to have a page to where there's some sort of way to look at raw data the data that I haven't really passed through a function here we're changing it into monthly returns it's not just prices but it's a place for someone if they are so inclined to to check out that the raw data and you're lying it so this is page to individual stocks we could call it on maybe do a monthly returns I'll just change it just so we can and see how that's going to change the final output we have a page called individual monthly returns with monthly misspelled but but that's okay right and again so this is going to be responsive or to our sidebar of course because it's pulling in the percentage returns of each ticker so if we start changing these to MPP and IBM we're gonna get different monthly returns because these are different ticker symbols right okay all right so I'm gonna I'm going to pause here for just a second and I'm gonna I'm going to take a look to see if we have we had any questions come through and one is can you enforce constraints in their relationships yes you can you could you could define them in that way but you could also yeah you can't enforce the constraints you you would really just need to to code to put it in the code but I I can send you an example on that if you if you send over an email how they do it how can you use flex dashboard with SHINee server and the answer is the exact same way as you load your app dot our file is you would load up this RMD file in the exact same way and when the server quote-unquote sees this file and the server notices this runtime shiny it's going to know this is a shiny app so it's going to be the same way as you were loading up your app thought our files but I would really just encourage you if you want to try it out just you know grab that grab that hello world example put it into a test a test our MD and then put it on your shyness or currency but but but trust me it'll it'll see this runtime shiny and it'll know it's a shiny AB and the next question is is a similar question can you run through the deployment options for shiny our MD it is the same as a shiny app so if you have shiny amp yo account it's the exact same you'll publish it by pushing a button if you have a shiny server setup you'll you'll move the are markdown file the same way as you would your app dot our file if you have a connect server it's the same process you'll you'll push your button and you'll zip it up to connect so there's not going to really be a difference in publishing the I guess the only difference is if you're used to using a UI and a server individual file that's not really going to be part of an arm markdown based shiny app it's almost it's going to be gonna be one file but yeah those are those are very good questions because obviously we need to to deploy these in some way and those are the only questions I see ok great thank you for those questions and I'll just if anyone has others please put them in the chat there was a couple of other things that I thought might be interesting to cover so I'll just kind of plow ahead here and I'm gonna I'm gonna toggle over to to a different set of our markdown reports slash apps I'm similar setup in the sense that we start with a none parameterize document we move on to a parameterised document and then we move on to to a shiny app right so this is this is kind of my workflow for almost anything I'm creating start with something uncommon Erised the simplest case move on to parameterize and then the shiny one thing that having that workflow enables is that is that if you want to kind of stuff your functions into a helpers file right so so what we've talking to over here is a slightly different use case this use case is gonna kind of build a portfolio of stocks going to calculate their volatility so we're leaving the world of stock returns to calculate their volatility let me calculate the contributions of each asset to the portfolio's volatility right but each of these three documents under a motorized parameterised and the shiny app if you look at their setups I'll toggle down to you right now their setups are all exactly the same we start out by loading up these packages right this is all three files and then second thing we do in each of them is we load up this dot our file and that dot our file has a set of functions that that I defined earlier and one to calculate component returns which is here's the first one right the second function in here calculates contribution volatility of each asset that's chosen and then the last one calculates the portfolio volatility alright it doesn't matter what the substance is the point is that one of the things that I think makes building shiny apps in our markdown quite efficient is that if you're building a report using our markdown and you're building a parameterize report using on lockdown and then you're building a shiny app using our markdown they can all use the same this same helpers function right so to me that that's nice because I don't have to worry about changing all three of these right if I want to I want to mess around or test out functions over here I can just do it in this in this one file and then all three of these are pulling them in right kind of same with formatting there's a lot of efficiencies to be gained if you know if you spend a lot of time formatting a report the way you want it so this is our under eyes report if you spend time kind of formatting this and getting it to look and feel the way you want and then you decide I want to turn this into a shiny app it's not a lot of work to take this structure and and put it over here in fact it's going to be really really similar you know once you have the look and feel the way you want it and your unpromising static report you can take to parameterize land a less static reporter then you can take it to shiny and all the trial and error that went into making this report look good almost all of that learning is going to translate into your shiny app it's going to be a little bit different because the look and feel will vary because in the shiny app of course you're giving the user those that sidebar inputs where they can actually put stuff now change stuff so you need to kind of think about that but other than that these formats are going to mirror each other so I kind of find that there's a lot of efficiencies especially for someone like myself I don't really want to spend a lot of time thinking about formatting I want to spend as little time there as possible I wanna spend as much time as I can here and helpers dot are working on these functions and I want flex dashboard and our markdown to him really handle that that formatting for me so that's just kind of another little what I think of as an efficiency gain it's not really substantive I mean you know you can you could very easily take any of this work and poured it over to a classic act on our you know shiny app and if you're very familiar with formatting those shiny apps then that that'll be easy for you for me I'm just much more comfortable formatting our markdown documents and the fact that I don't have to leave the world of our markdown to build a shiny app just saves a lot of trouble having to reformat things so I'm just gonna go ahead and knit this file because there's one thing that that I want to show you all that's it's not specific to an arm markdown shiny app but hopefully you'll find it helpful so then this happens a little bit more analytically I'd say well at least time consuming than the other one right it's it's building a portfolio calculating standard deviations and then when the contributions over timing that's having to do that for three different stocks so I'll push I'll push go here you'll see it's gonna take a second to run and I'm kind of doing that on purpose all right so all right so you can see that this app didn't start running until I pushed and go which is which is different from from this app from the Sharpe ratio app this this was calculating the new Sharpe ratio every time we changed a parameter which really isn't that efficient it isn't that big of a deal if your app was gonna run extremely quickly meaning it isn't doing a lot of heavy computations but for this volatility app I don't really want that because every single time anything here gets changed if I didn't have this go button it would start trying to recalculate recalculate recalculate and that would eventually drive our end users insane so apologies if this is kind of old hat for people but but the way that we handled that was here with this action button go right so clearly the what that's doing is it's waiting for someone to push go before anything happens and then the the link between this button and the rest of our app is right here so instead of creating a straight-up reactive which is what we were doing here in our original app our Sharpe ratio app right these are just reactives right they're not waiting for anything that they're just waiting for inputs reactive reactive reactive here we're using an event reactive and this event reactive is saying wait for input dollar go and don't do anything until you get that input dollar go so I just it's not specific at all to our markdown shiny apps this is the same you would you would have in a normal app that are but I I like to just kind of point it out whatever whatever I'm going through shiny-ass because it can save your end-users a lot of grief and frustration from having things run every single time they change an input I mean it's it's really it's not hard to get this set up you just add an action button this doesn't have to say go we can say like submit or something whatever you do whatever you kind of want to tell people to do it's just waiting for input go to get to get fired off right so that that's gonna it's not only going to change the performance of your app and since we're doing two calculations we need it twice here so I'm calling two of reactives input dollar go this probably isn't the greatest construction in the world but what it's saying is so now render hi chart knows when you're high chart is waiting to get this value portfolio rolling standard deviation right so with waiting for that input dollar go to get fired off because then that's when these values we're going to get passed down to it so that's just going to make you know life a little bit easier on your end users and just just for completeness if you don't want to see what this looks like in the parameterize setting it looks like this right so similar to what we were just showing our end users right different different options here baking stocks I don't know those are pirating banking stocks but you can see Wells sure we'll see what happened before the financial crisis I thought did it let's see how long this is gonna take to MIT this this app takes a little bit longer the the functions and helpers are they're not you're dependent on a built-in function but they're not completely built-in so they're doing some slicing and dicing but okay here so here's that that parameterised report right it was pretty easy to create a variant of this and I wouldn't have had I wouldn't have wanted to create a variant if somebody had asked me to so this is showing the volatility of a banking portfolio it really spiked here for the financial crisis Jevon driven almost completely by Wells Fargo had to get bailed out by that by the federal government and then the Wells of the Purple Line here slowly starts to take down as they announced they were gonna pay back their their bailout kind of plumbing it even had a negative volatility they're paying back their their tarp money but in any event you know this relies on that exact same structure the only difference here is that you know one it's calling on this helpers file so again I just find that takes away a lot of a lot of having to update things in different places if you can have your files to pin them the same thing there might be a good reason not to do that and then something someone else is working on but for this one it worked out well and then you know key you know just purely thinking about our end user experience which is not a natural thing at least I can only speak for myself as someone who users are typically we're just thinking about delivering our results we never probably have really had to think about what's it gonna be like when someone interacts with this you know shiny web application what's their experience gonna be like but this section but will make their experience a lot better so hopefully we don't you know especially if you're doing work that you can do a lot of a number crunching it'll save them some grief and also let them know like okay I push to go something's happening now so things aren't just kind of happening on the screen without their control hey Jonathan we're coming up just to the top of the hour and it looks like you have one more question you see watch it okay so do you ever see our plush Ani functioning in the browser without a server requirement like say J s um not really so you need a so I I haven't seen that that's not to say that that it's that it's impossible the the main reason that you kind of need a what we call a shiny server sitting underneath here is that it's going to be running your R code right so so that's that's a big thing with that shining server is is doing when you're serving this up it's running the R code and then bringing back the calculations to the end user that's not to say that this is impossible I never say things are impossible because there's probably someone out there who can figure out a way to do it but but not something that we see [Music]
Info
Channel: RStudio
Views: 12,628
Rating: 4.8823528 out of 5
Keywords: RStudio, R Programming, R Markdown, Reporting with R, Reporting with R Markdown
Id: sQjnvaf072M
Channel Id: undefined
Length: 54min 16sec (3256 seconds)
Published: Wed Nov 29 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.