如何将登录模块与 Flexdashboards 一起使用?

问题描述 投票:0回答:1

我有一个

r-flexdashboard
shiny
元素。这很好用。

现在我必须实现一个登录页面来启动仪表板。

通过软件包

shinymanager
,我能够集成登录窗口。感谢这篇文章的更新:为flexdashboard修改shinyauthr

我的想法是在正确登录后显示(Flex-)仪表板元素。我能够读出登录是否正确。这是一个无功值 (

TRUE
/
FALSE
)。

我要解决的问题/问题是:

是否可以在

r chunks
中使用这个无功值,即
eval
选项?

您可以在 github 上找到一个完全可重现的示例: FlexDB-登录

要正确登录,请使用:

  • 用户:=闪亮
  • 密码 := vh

对于快速跑步者我在这里记录了主Rmd文件的代码:

--
title: "Log-In Flexdashboard"
author: 
output: 
  flexdashboard::flex_dashboard:
    css: inst/assets/styles-auth.css
    orientation: columns
    vertical_layout: scroll
runtime: shiny
---

```{r global, include=FALSE}
# load data in 'global' chunk so it can be shared by all users of the dashboard
if(!require(shinymanager)){
  install.packages('shinymanager', dependencies = TRUE, repos = "http://cran.us.r-project.org")
  library(shinymanager)
}
if(!require(shiny)){
  install.packages('shiny', dependencies = TRUE, repos = "http://cran.us.r-project.org")
  library(shiny)
}
if(!require(tidyverse)){
  install.packages('tidyverse', dependencies = TRUE, repos = "http://cran.us.r-project.org")
  library(tidyverse)
}
if(!require(lubridate)){
  install.packages('lubridate', dependencies = TRUE, repos = "http://cran.us.r-project.org")
  library(lubridate)
}

# define some credentials (you can also use sqlite database)
credentials <- data.frame(
 user = c("shiny", "shinymanager"),
 password = c("vh", "12345"),
 stringsAsFactors = FALSE
)
```

```{r}

auth_ui(id = "auth")

# Q1: How to transform to 'moduleServer'?
auth <- callModule(
   module = auth_server,
   id = "auth" ,
    check_credentials = check_credentials(credentials) # data.frame
   # check_credentials = check_credentials("path/to/credentials.sqlite", passphrase = "supersecret") # sqlite
)


```

```{r}
# Store result of authentification in a reactive variable 
eval_login <- reactive({
     auth$result
     })

# Some Checks (printed in the Dashboard!)
# FALSE if not looged in or login failiure
# TRUE if correct login used
reactive(auth$result)
eval_login

# isolate() the reactive variable is always FALSE
isolate(eval_login())

```



<!-- does not work: 'isolate(eval_login())' alsway FALSE -->
```{r, child="Child_test.Rmd", eval = isolate(eval_login())}
```

<!-- does not work: 
Error in : Operation not allowed without an active reactive context.
* You tried to do something that can only be done from inside a reactive consumer. -->
<!-- ```{r, child="Child_test.Rmd", eval = eval_login()}
``` -->

<!-- does not work: 
Error:  ungültiger Argumenttyp -->
<!-- ```{r, child="Child_test.Rmd", eval = reactive(auth$result)}
``` -->

不幸的是,这个解决方案失败了:在 Flexdashboard Rmarkdown 上添加身份验证

非常感谢您的帮助!

r shiny r-markdown flexdashboard shinyauthr
1个回答
0
投票

请在下面找到最接近我能想到的解决方案。我不确定为什么子文档没有正确呈现 - 也许你可以在此基础上构建:

---
title: "Log-In Flexdashboard"
author: 
output: 
  flexdashboard::flex_dashboard:
    css: inst/assets/styles-auth.css
    orientation: columns
    vertical_layout: scroll
runtime: shiny
---

```{r global, include=FALSE}
# load data in 'global' chunk so it can be shared by all users of the dashboard
if(!require(shinymanager)){
  install.packages('shinymanager', dependencies = TRUE, repos = "http://cran.us.r-project.org")
  library(shinymanager)
}
if(!require(shiny)){
  install.packages('shiny', dependencies = TRUE, repos = "http://cran.us.r-project.org")
  library(shiny)
}
if(!require(tidyverse)){
  install.packages('tidyverse', dependencies = TRUE, repos = "http://cran.us.r-project.org")
  library(tidyverse)
}
if(!require(lubridate)){
  install.packages('lubridate', dependencies = TRUE, repos = "http://cran.us.r-project.org")
  library(lubridate)
}

# define some credentials (you can also use sqlite database)
credentials <- data.frame(
 user = c("shiny", "shinymanager"),
 password = c("vh", "12345"),
 stringsAsFactors = FALSE
)
```

```{r}
auth_ui(id = "auth")
# Q1: How to transform to 'moduleServer'?
auth <- callModule(
   module = auth_server,
   id = "auth" ,
    check_credentials = check_credentials(credentials) # data.frame
   # check_credentials = check_credentials("path/to/credentials.sqlite", passphrase = "supersecret") # sqlite
)
```

```{r, echo=FALSE, results='asis'}
# https://yihui.org/knitr/options/#chunk-options
# https://bookdown.org/yihui/rmarkdown-cookbook/child-document.html
# https://stackoverflow.com/questions/13327258/is-it-possible-to-share-variables-between-knitr-chunks-using-engine-bash
# https://stackoverflow.com/questions/16827788/knitr-child-throws-error-after-upgrade-to-r-3-0

res <- knitr::knit_child(input= 'Child_test.Rmd', quiet = TRUE)
reactive({
  if (auth$result) {
    cat(res, sep = '\n')
  }
})
```
© www.soinside.com 2019 - 2024. All rights reserved.