Use SharePoint to Host an R Markdown Web App!
Jorge Miranda, SHRM-SCP
Data Scientist specializing in integrating and making sense of HR data.
To state the obvious, the world has mountains of data. As a people analytics pro, I have seen and used many tools to ingest, clean, transform, analyze, visualize, and share a big chunk of that mountain. My favorite tool for these jobs is typically R. I particularly love the range of options it comes with vis-a-vis reporting.
R Markdown is an extremely powerful and easy way to build web apps that feel like an enterprise reporting tool. It does this by leveraging markdown, which is an easy-to-learn syntax that gets converted to HTML (don't believe me? check out the syntax here). R Markdown comes pre-loaded with CSS, but a little knowledge of CSS can completely customize the design. In an R Markdown document, the markdown syntax is combined with R code to produce a web page (or multi-page website) with visualizations, tables, and R calculations. The best part is you don't even need a server to host it. You can easily share a single web page as a file.
But what if you want to share it with others in your organization, especially a multi-page site? You certainly can't email someone pages and pages of HTML to open up on their end. Let's say you have a SharePoint site with permissions and content that you control. Just follow these steps to take your multi-page analysis or dashboard to the cloud.
Create a batch script to rename HTML extensions to ASPX
When you create an R Markdown site, the files are automatically placed in a sub-folder called _site. Create a .bat file containing the file path of your _site folder and the?rename *?keyword.
cd "C:\Users\YOUR_USER_NAME\PROJECT FOLDER\_site"
rename *.html *.aspx?
Run this .bat file every time you re-render your R Markdown site and publish.
Multi-page Websites
The above code that renames a single page works fine, but creating a website with a navigation bar and numerous hyperlinks embedded in the body can be slightly more complex. Luckily, you can apply the same concept here with a few minor adjustments.
领英推荐
YAML Configuration
For starters, you have to make sure all hyperlinks point to a?.aspx?file when routing to internal pages. Next, make sure to reference?.aspx?files in the?href?argument of your YAML file. The YAML file should be in your root project folder and always be named _site.yml.
Here is an example.
name: "A Multipage Webiste"
navbar:
? title: Company Dashboard
? type: inverse
? right:
? ? - text: "Home"
? ? ? href: index.aspx
? ? - text: "Products"
? ? ? href: index.aspx
? ? ? menu:
? ? ? ? - text: "Contact Us"
? ? ? ? ? href: index.aspx"
Create a SharePoint folder to hold the?site folder
Run the batch file to rename the outputted HTML files. Copy and paste these?.aspx?files and any subdirectories such as?styles, images, and?site_libs?to the?_site?folder on SharePoint. Open the?index.aspx?on SharePoint and test out the navigation links to ensure they lead to the?.aspx?routes defined in your YAML configuration.
Take it further by automating the copy/paste step so that the files are automatically updated in SharePoint when you re-publish. This can be accomplished with Microsoft's Power Automate or a shell/bash script.
Check out the R Markdown website to learn how to build your own. I know what you're thinking and yes, that website is made with R Markdown.