 Hello everyone and welcome to the R-Medicine conference. In my presentation, I'm going to talk to you about using R-markdown and Office Down to automate clinical trial reporting. After a small introduction, I will talk to you about the challenges that we see different companies face when it comes to reporting for clinical trials. Then I will talk about using R-markdown to automate the reporting part and we will dive a little bit deeper into what are the key features of Office Down that you can use on your daily work. By means of introduction, my name is Damian Rojewicz and I'm one of the founders of Apsilon. Personally before founding the company, I have worked in different companies and in different programming languages. In the end, we started working with R&Shiny because it's amazing technology that allows you to build applications rapidly and make sure that they are exactly as your users want it to be. As a company, we have been established over eight years ago and we work mostly with 4,500 companies in different time zones and in different industries. We are a team of 40 plus individuals across the whole world because we are a remote first company. So when it comes to the clinical trials, the common pattern that we see is actually that reporting, it is not easy. Of course, the analytics part is always a big challenge. However once you have the results, you need to actually put them all into a report, you need to make sure that it obeys all the standards and that it can be easily reproduced. So it has to contain all the data and the sources that were used to produce the outcome and conclusions. It is prone to errors because when you do it manually and you use copy-pasting from Excel for example or copy-pasting some graphs, charts, it is a cumbersome work that takes a lot of time and effort. And of course whenever you create a report, you need to make sure that you are able to reproduce this report when asked because this is what regulations require you to do. And I'm jumping into GXP which is probably very well known to most of you. It is about ensuring a safe and reliable product. Usually in the whole Europe, GCP which is good clinical practice is even a legal obligation for all trials of investigational medicinal products. So what is GXP? It just stands for good X practices and in X you can put anything. So it could be C for clinical, it can be L for laboratory or R for review. For us the most important ones are GCP and GLP, so the good clinical practice and good laboratory practice. When you think about GXP and technology, you can think about tricky parts that your products have to obey. So the first one is traceability. You need to be able to reconstruct the whole development history of a drug or medical device, so you need to have some kind of a trace of what has been changing when it comes to reports, when it comes to the data, when it comes to the code that was produced for the application. Then you need to make sure that you have the accountability. So you need to make sure that you can tell who changed what and when. And the next one is data integrity. So the reliability of data generated by the system. In short, it usually means that the underlying data source has to be also as a GXP, but it also means that in your report you need to point to the right sources that you used and make sure that these sources are also reliable. So how R Markdown can help you there? R Markdown is a very powerful tool because it allows you to make the whole reporting part automated. You can avoid spending time on repeatable tasks and very time-consuming manual report creation and you focus on the fact that this R Markdown is reproducible. You can keep regenerating it as often as many times as you want and you do it with the same data or the new data whenever it is available. What is more, it is testable. So you can have some chunks of code that you put into your application or the R Markdown report to be more precise and you can test those chunks separately to ensure that they are correct. R Markdown will allow you to create Word documents, PowerPoint documents, PDF or HTML, whatever you need. It will help you and it allows you to follow the GXP regulations easily because some parts are fully automated like making sure that there is an author on the first page. Just as simple as that but there are plenty, plenty more. So this is an example of what an R Markdown document looks like. Just before my presentation there was another one about creating PowerPoints presentations. So I'm not going to dive too much into details of how to work with R Markdown but just to give you a very quick glimpse of it. There is a simple syntax to select the title, the output, there's plenty of more other options that you can set here, especially some arguments that can be passed from the outside. And if you write a very simple code that is translated or this is basically a typical Markdown but all the other chunks that are in R are going to be translated into Markdown that then is going to be compiled to the final document. So with this simple setup and only 33 or a little bit more lines of code you're able to create a normal, editable Word document that you can then use. So just to give you an overview what R Markdown actually does, it's under the hood, through Knitter, it generates a Markdown report, it's not only through Knitter but also some other libraries but the resulting file, Markdown file that is intermediate file that is used, this is something that is later processed by a so-called PANDOC, a system library and PANDOC renders the final document. This is important to know that Markdown and PANDOC these are fully independent from the R environment so you may have come across them in a very different setup and it is also important because this is a standard. So whenever you talk to the IT you can tell them that okay, R is maybe a little bit new in the company but in fact underneath you are using very well known old and trusted libraries to generate the files. So in the end it is much easier to actually talk to the IT team and make sure that they understand that this is a very good tool to use and say it is already approved. And talking about the big picture, the great thing about RStudio Connect is that when you have an R Markdown file with a single click of a button you can actually deploy it to RStudio Connect and it is going to be automatically rendered and as many users as you want can actually go ahead and read the document, they can download it and at the same time you have the whole history of whenever the document was rendered. You can also track the users that are accessing this file. Whenever you want the document to be regenerated you just click through the interface in RStudio Connect and you have a new file available for your users. So I highly recommend connecting your R Markdowns with RStudio Connect because there is also one more very useful feature which allows you to set up to periodically automatically regenerate the report. So if you want to create a daily report and that is downloadable in the world for example or HTML, you just set a few parameters and RStudio Connect automatically will regenerate the report for you and also it can send the report to some other users. So even for us as a technical company we use RStudio Connect to generate some of our internal reports and we get emails from RStudio that describe what is going on under the hood of our company. So let's talk about OfficeDown, why we need it and what it actually gives us. First of all, R Markdown is generic and it translates to many different file types. So it will translate to PDF, some other formats like Word, PowerPoint or HTML and it is not built specifically for Word or PowerPoint. At some point, and these are some life examples, you may be requested to change your reports because there are some specific GXP regulations and I would like to show you a very practical way of solving those issues because you can spend a lot of time trying to adjust the Markdown and trying to search online how to do it. You will try through CSS, it will not work, you will try through different hacks and it will get ugly at some point but OfficeDown is going to help you a lot with that even though the tricks are very simple and the package itself is not very complex. So the things that you can be asked about is for example please create a page with information about the author, the date, all the sources, they need to be a space for a review. It has to be the first page and then there will be a table of contents and it seems like a simple task but at the same time it is not that easy achievable through Markdown itself and with OfficeDown you will be able to do it right away. You can be asked to present one of the pages out of 50 pages, one of the pages should be in a landscape mode and this is also not that easily achievable through Markdown itself. And for example he wants to make specific formatting just to parts of the data inside of the table that you are presenting. So let me jump into OfficeDown itself. OfficeDown adds much more functionalities to generating OfficeDocuments. Under the hood there is like a whole Officeverse that contains OfficeReflexTable, MSChart, OfficeDown and some other packages. I highly encourage you to take a look into this. There is a lot of great documentation and it will help you build Word documents a lot. So OfficeDown, the key features that you will find there is there is a fine control of the document structure. It will allow you to format text and tables to style documents using the templates to combine multiple documents into one and a simple one but super, super helpful believe me is a landscape page, the one that you select it to be. So what about fine control of the document structure? The Vanilla Markdown allows you to add a table of contents but as I mentioned the control over where it is exactly located is quite limited. With PageDown you can add not only the table of contents but also for example a list of tables or a list of figures but order of those sections is also limited. Now OfficeDown is allowing you to explicitly mark where you want which parts to be generated and you will get exactly what you want. So jumping into the example, on the left hand side you can see the code that was used to create the resulting Word document on the right. So first you can see that after the new page, after the title page that is automatically generated from the metadata. There is a list of figures and you created by using an HTML comment tag that says block TOC and the sequence ID that we are going to use to display is figure. And you can see that the first page is containing only the list of figures here. Then you can create a new page and ask for a block for the table of contents and there on the third page you actually have the full table of contents. Then on the next page you can jump into showing only the tables and it is fully up to you what kind of IDs you set for your figures, anything that you set you can then filter out and create a table of contents from it and it will always regenerate automatically for you whenever the whole document structure changes. Very easy and very, very helpful. Now about formatting text and tables. Actually just the editable document may not be enough. You may need to adjust the styling based on some features of the data and this is actually quite limited in a normal markdown package but it is fully available through Officeverse and through a flexible package. And in order to create this I'm going to show you a very small example. First about formatting text. The great thing about the Officedown package is that it's using the same structure of how Microsoft Office is using the styling. So we actually create different sections of text and you specify the styles to them. So you can see that you can specify as a variable the shortcut for italic paragraph with a font size of 11 and you can create a separate variable that is going to say italic blue and it will use the italic style and add color blue to it. Then whenever you are writing text as you can see here you create a paragraph and inside you use a function to display text with added style that is set here as a variable. And the result you can see on the right there is a blue italic text then it is followed by a normal text and at the end there is just italic text. Extracting the styles to separate variables or functions is a very, very useful tool especially if you are building 50 or 100 pages of report and you want it to be well organized. The next one is the table. The example of what you would like to achieve is when you have a data and then you want to for example color few columns that like price X, Y and Z on an orange color whenever the price is lower than 330 and this is easily achievable. You can see here you create a flex table based on the data and you apply a color function that says whenever price is less than 330 color price X, Y and Z with orange color just that simple but very, very powerful in practice. And you can see some other examples on the styling as well so for example whenever cart is bigger than 0.24 you can color another column so for example the cart. So when it comes to styling documents you can also use templates. This is available in Markdown itself, our Markdown. Office Down allows you to do that as well. You can define the whole style in a separate template file and then just add this file as a template to use in the resulting file that you will create. So this could be a file that you created separately in words. You can color different headings. You can add different styles. And what is also additionally very important you can create your own styles. So we can create a new style that is not in the document, you can name it and then you can create separate paragraphs that will use that particular style and you can do it with Office Down. But in a very simple way if you want to make the style of your document the same as the template document you just reference this document in one line at the top of your our Markdown file and all of the styles are going to be applied to the newly generated document. Now another very useful feature that you may happen to use in your work is combining multiple documents. So sometimes you may want to create small separate reports. This could be like five different reports and you may want to combine them into one big report that also has a table of contents that actually points to different parts and the numbering is updated. And this is an example how you can achieve that. As you can see it's super simple because you just use on the line 34 you can see that you just use a function which is called block or docs and it just takes as an argument the file name that is in the same workspace in the same path. And you can see that this is report one file. This one it starts with number one when it comes to numbering there is report two that also starts with one. But the combined document the result of this is going to give you report one which already is a second in the list of items and the third item is report three. All of the numbering is updated and the table of content is created automatically for you. So very convenient feature very easy to use and actually something that I do recommend doing especially that if you have a lot of different chunks and they may be reusable at this single simple functionality will like give you a lot of a lot of less work to do. And the last but not least using landscape portraits orientation with office down you can very simply select the pages that you want to have different orientation and you do it with a single comment. You can see here online 49. The only thing you need to do is to set a comment that says block landscape start. Then whatever is inside of this landscape block will be in a landscape page. And then when you want to go back to the vertical to the portrait mode you just write block landscape stop. And as you can see the resulting file has three three pages and the middle page is actually a landscape one. Small functionality believe me you're going to use it a lot. So just to sum up the office down package and as you saw it allows you to have a very good control of the document layout and structure. There is plenty of formatting options. I encourage you to take a look into documentation because you can do almost anything you want with your document and I've seen a lot of very difficult requests that we had to go through and everything is achievable. Almost everything of course is achievable there and it actually allows you to easily apply the document style a white style compile multiple documents have a landscape page and many many more. So I encourage you to look more into this and use it in your daily work. Definitely is going to speed up your work and make it much easier. So that's all from me. Thank you very much. I hope that this has been useful for you and thank you for joining the conference. Please let me know if you have any questions and would like to get some code snippets from me. You can just send me an email and I will be very happy to talk to you during the conference as well. Thank you everyone and have a great day.