Can I use javascript or the RStudio Connect API to get the URL of the current page in Rmarkdown

In brief: We are trying to do the following, with the aim to include a (variable) link to a parameterized Rmarkdown report in an email generated in RStudio Connect.

In connect each time that we create a new version of a Parameterized report and save it, connect creates a sub-id. The url will be something like /connect/#/apps/102/access/55 where 102 is the id of the original report and 55 of the sub-report that we have saved with different variables. The id 102 does not change when we create a new version of the report. But the sub-id does, and is different with every new parameterized version that we save.

What we would like to do is to send an email based on the parameterized version (in this case with sub id 55), but not including the full report, but with a button that brings the user to the report if they decide to read it.

Including the button is not the issue. The issue is including the correct URL. Since the email is defined in the main Rmarkdown file (published with id 102), we need to identify the current url (with sub id, in this case 55) on a case by case basis.

We have tried to get the URL using the Connect API, but cannot get details of the sub-pages when we us the get_content endpoint. For instance.

__api__/v1/experimental/content/89299538-713b-4f38-abca-322b48e9e1ea

So we tried to include javascript, for instance

```{js}
var jstest = window.location;
jstest.toString();

This may be the most promising path, but how do we get the output of window.location back a follow up R chunk in the Rmarkdown file?

Has you solved this use case somehow? Or could you give us a pointer to look for a solution?

1 Like

This information is available to the child report in an enviroment at render-time: https://docs.rstudio.com/connect/1.7.8/user/r-markdown.html#r-markdown-including-urls

So the code you're using to customize the email would use that environment variable to create the correct link.

Does that solve your problem?

I'd also highly recommend checking out the new release of the blastula package which makes it much easier to create these custom emails. For example:

install.packages('blastula')
blastula::prepare_rsc_examples()
5 Likes

Hello Sean,

Yes it works. (I have been working on this with Frans). The solution was to include:

report_url <- Sys.getenv("RSC_REPORT_URL")

And now the button redirects to the latest published report!

3 Likes