Running a javascript library in RMarkdown - how to obtain variable?

Hello there!

I am currently trying to use the MSAL javascript library in a R markdown report to obtain an oauth token to authenticate a db connection in a code chunk. Currently my script is like this:

---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
```

<script src="https://alcdn.msauth.net/browser/2.35.0/js/msal-browser.js"></script>

```{js}
async function wrapperFunc() {
  const msalConfig = {
    auth: {
      clientId: "XXX",
      authority: "XXX"
    }
  };

  const msalInstance = new msal.PublicClientApplication(msalConfig);

  const silentRequest = {
    scopes: ["XXX"]
  };

  const callLogin = async function (silentRequest, msalInstance) {
    try {
      const loginResponse = await msalInstance.loginPopup(silentRequest);
      return loginResponse;
    } catch (err) {
      console.log(err);
    }
  };

  response = callLogin(silentRequest, msalInstance);
  return response;
}
wrapperFunc().then((result) => {
  console.log(result["accessToken"]);
});
```

But I am getting this error below in the console when deploying to r studio connect. All I need at this stage is for the oauth token to be printed in the console (or ideally obtained as an R variable).

How do I obtain this oauth token?! Or at least load the msal library correctly?

Is this happening only on RStudio Connect ?
If so maybe some blocking from the url you are trying to reach (https://alcdn.msauth.net/browser/2.35.0/js/msal-browser.js)

Try setting this in a <head> using a include script to be in include header (7.10 Include the content of an existing HTML file (*) | R Markdown Cookbook)

Have you consider building a R HTML dependency for this ? Usually this is a way to package some JS lib and feature to be used in R within Shiny Apps or Rmd document
You can find some example on those type of things at Chapter 4 Handle HTML dependencies with {htmltools} | Outstanding User Interfaces with Shiny but also in the doc: Define an HTML dependency — htmlDependency • htmltools

I don't know more about msal, so here the issue seems to be your JS lib not loading. Try solve this and hoepfully the token will be retrieved

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.