【问题标题】:The flexdashboard reactive global profile parameters problemflexdashboard 反应式全局配置文件参数问题
【发布时间】:2020-03-12 00:20:46
【问题描述】:

几周以来,我一直在尝试为似乎与此discussion 相关的常见用例提出解决方案,但没有成功。这段对话最接近于解释如何在 Shiny 应用程序中创建和使用全局变量——但我很难理解所提供代码的细微差别。 这篇 SO 帖子很相似,但并不完全是我感到困惑的地方:

全局配置文件问题

我们有一个应用程序可以引导用户完成“向导设置” 介绍,允许他们配置某些参数,这些参数会影响多个应用程序 flexdashboard 中单个 Shiny 应用程序模块的功能。这些需要全局存储,传递到子应用程序中,并且需要在“配置文件”应用程序中进行修改在全局级别,以便对全局配置文件所做的更改会修改其他模块的功能因此。

我正在尝试理解这篇文章中提供的代码并使其适应这个用例,但到目前为止我还无法确定如何去做。任何帮助将不胜感激!

---
title: "Global Profile Reprex"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
runtime: shiny
---

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

Global <- function(input, output, session) {
  profile  <- reactiveValues(goal = 5)
  return(profile)
}
Module1 <- function(input, output, session, globals) {
  goal = globals$profile
}

```

Column {data-width=500} 
-----------------------------------------------------------------------

### Access
```{r 'Access'}
# This is just intented to access the global reactive values
ui <- renderUI(fluidPage(
    shiny::htmlOutput("viewProfile")
))



server <- function(input, output, session) {
  globalProfile <- callModule(Global, "global")
    output$viewProfile <- shiny::renderText({
        paste0("Goal:",globalProfile$profile$goal)
    })
}

# Run the application 
shinyApp(ui = ui, server = server)
```

Column {data-width=500} 
-----------------------------------------------------------------------

### Update
```{r 'Update'}
# This is intended to allow a user to update the global reactive values.
ui <- renderUI(fluidPage(

    fluidRow(width = 12,
             column(12,
                    textInput(inputId = "goal",
                              label = "Goals (time per week)",
                              placeholder = "ex 4h42m 4:42"))),
    fluidRow(width = 12,
             column(5), column(2, submitButton("Save Changes", icon("save", lib = "font-awesome"))), 
column(5)) , 
    shiny::htmlOutput("test.text")
))



# Define server logic required to draw a histogram
server <- function(input, output, session) {
  globalProfile <- callModule(Global, "global")

    reactive({
        print("profile Saved")
      mod1 <- callModule(Module1, "mod1", globalProfile)
        mod1$profile$goal <- input$goal
    })
    output$test.text <- shiny::renderText({
        c("Goal",globalProfile$profile$goal) %>% paste0(collapse = " ")

    })
}

# Run the application 
shinyApp(ui = ui, server = server)
```


R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] magrittr_1.5         shiny_1.4.0.9000     RevoUtils_11.0.3    
[4] RevoUtilsMath_11.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.3.1             pillar_1.4.2            
 [3] compiler_3.5.3           later_1.0.0             
 [5] plyr_1.8.4               tools_3.5.3             
 [7] digest_0.6.22.3          packrat_0.4.9-3         
 [9] lubridate_1.7.4.9000     jsonlite_1.6.9000       
[11] evaluate_0.14            tibble_2.1.3            
[13] gtable_0.2.0             pkgconfig_2.0.3         
[15] rlang_0.4.1              rstudioapi_0.10.0-9000  
[17] yaml_2.2.0               xfun_0.11.1             
[19] fastmap_1.0.1            withr_2.1.2             
[21] dplyr_0.8.3              httr_1.4.1.9000         
[23] knitr_1.26               grid_3.5.3              
[25] tidyselect_0.2.5         flexdashboard_0.5.1.9000
[27] glue_1.3.1.9000          R6_2.4.1                
[29] rmarkdown_1.17           ggplot2_3.1.0           
[31] purrr_0.3.3.9000         promises_1.1.0.9000     
[33] scales_1.0.0.9000        htmltools_0.4.0.9000    
[35] rsconnect_0.8.13         assertthat_0.2.1        
[37] xtable_1.8-4             mime_0.7                
[39] colorspace_1.4-0         httpuv_1.5.2.9000       
[41] lazyeval_0.2.1           munsell_0.5.0           
[43] crayon_1.3.4 
更新

访问函数适用于这个简化的代码,但我仍然无法弄清楚如何从更新应用程序内部更新全局反应值。

---
title: "Global Profile Reprex"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
runtime: shiny
---

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


  profile  <- reactiveValues(goal = 5)

Module1 <- function(input, output, session, globals) {
  goal = globals$profile
}

```

Column {data-width=500} 
-----------------------------------------------------------------------

### Access
```{r 'Access'}
# This is just intented to access the global reactive values
ui <- renderUI(fluidPage(
    shiny::htmlOutput("viewProfile")
))



server <- function(input, output, session) {

    output$viewProfile <- shiny::renderText({
        paste0("Goal:", profile$goal)
    })
}

# Run the application 
shinyApp(ui = ui, server = server)
```

Column {data-width=500} 
-----------------------------------------------------------------------

### Update

```{r 'Update'}
# This is intended to allow a user to update the global reactive values.
ui <- renderUI(fluidPage(

    fluidRow(width = 12,
             column(12,
                    textInput(inputId = "goal",
                              label = "Goals (time per week)",
                              placeholder = "ex 4h42m 4:42"))),
    fluidRow(width = 12,
             column(5), column(2, submitButton("Save Changes", icon("save", lib = "font-awesome"))), 
column(5)) , 
    shiny::htmlOutput("test.text")
))



# Define server logic required to draw a histogram
server <- function(input, output, session) {

    reactive({
        print("profile Saved")
      profile$goal <<- input$goal
    })
    output$test.text <- shiny::renderText({
        c("Goal: ",profile$goal) %>% paste0(collapse = " ")

    })
}

# Run the application 
shinyApp(ui = ui, server = server)
```

【问题讨论】:

    标签: r shiny scoping flexdashboard shiny-reactivity


    【解决方案1】:

    好的,经过一番修改后解决了这个问题!使用observeEvent 和保存actionButton 可以正确更新反应配置文件值。

    ---
    title: "Global Profile Reprex"
    output: 
      flexdashboard::flex_dashboard:
        orientation: columns
        vertical_layout: fill
    runtime: shiny
    ---
    
    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(shiny)
    library(magrittr)
    
    
      profile  <- reactiveValues(goal = 5)
    
    Module1 <- function(input, output, session, globals) {
      goal = globals$profile
    }
    
    ```
    
    Column {data-width=500} 
    -----------------------------------------------------------------------
    
    ### Access
    ```{r 'Access'}
    # This is just intented to access the global reactive values
    ui <- renderUI(fluidPage(
        shiny::htmlOutput("viewProfile")
    ))
    
    
    
    server <- function(input, output, session) {
    
        output$viewProfile <- shiny::renderText({
            paste0("Goal:", profile$goal)
        })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    ```
    
    Column {data-width=500} 
    -----------------------------------------------------------------------
    
    ### Update
    ```{r 'Update'}
    # This is intended to allow a user to update the global reactive values.
    profile  <- reactiveValues(goal = 5)
    uiUpdate <- fluidPage(
    
        fluidRow(width = 12,
                 column(12,
                        textInput(inputId = "goal",
                                  label = "Goals (time per week)",
                                  placeholder = "ex 4h42m 4:42")), actionButton("save","Save Changes", icon("save", lib = "font-awesome"))), 
      shiny::htmlOutput("test.text")
    )
    
    
    
    # Define server logic required to draw a histogram
    serverUpdate <- function(input, output, session) {
    
        observeEvent(input$save,{
           # print("profile Saved")
          message("Profile Saved")
          profile$goal <<-  input$goal
        })
        output$test.text <- shiny::renderText({
            c("Goal: ",profile$goal) %>% paste0(collapse = " ")
    
        })
    }
    
    # Run the application 
    shinyApp(ui = uiUpdate, server = serverUpdate)
    ```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 2019-06-14
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      相关资源
      最近更新 更多