6

I am building an application in RMarkdown that relies upon a user-generated password:

library(getPass)

pw <- getPass(msg = "Enter the Password, please!")

When I run all of the code interactively in the R console, the getPass function pauses the code and provides a popup window in which a user can enter the password.

However, when including this code in RMarkdown, the knitting process does not halt for getPass to do its work. As a result, the code runs without the password. In this case, it prevents the application from connecting to the password-protected database.

Do any of you have a recommendation for how to resolve this issue? I have searched for similar questions, but if this is a duplicate, then my apologies. Thank you!

DoubleTap
  • 69
  • 1
  • 3
  • 1
    Rmarkdown rendering is not meant to be interactive: graphics/plots are all captured to be rendered (to html, pdf, etc), so the pop-up window is either (a) just another graphic, or (b) something that is not waited-on or even permitted. An option would be to use `params` feature of Rmarkdown documents, and create a function that prompts for the password and passes it with something like `rmarkdown::render("path/to/file.Rmd", params=list(pass=pw))` (allowing for other arguments to `render`). – r2evans Jun 30 '17 at 19:24
  • Thank you, r2evans! That worked very well. – DoubleTap Jun 30 '17 at 22:21
  • Just curious: when using RStudio, is there a way to get the `rmarkdown::render` to pop up the preview window? – Kalin Dec 15 '17 at 19:12

1 Answers1

6

As stated in comment use parameterized report.

Your rmarkdown should look like:

---
output: html_document
params:
  pwd:
    label: "Enter the Password, please!"
    value: ""
    input: password
---

Your password is `r params$pwd`

Now either run

rmarkdown::render("test.Rmd", params="ask")

or use RStudio button "Knitr with parameters":

where to klik

Marek
  • 49,472
  • 15
  • 99
  • 121