Search code examples
rr-markdownflexdashboard

Problem with section naming and Browse Application on flexdashboard


I try naming a section using flexdashboard. But, I can not do this. I try write HERE DOES NOT APPEAR IN DASHBOARD as a title, but doesn't work. My code:

---
title: "Statistics"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    theme: cerulean
    runtime: shiny
---

```{r}
library(tidyverse)
library(flexdashboard)

set.seed(123)

df_1 <- data.frame(
  x = replicate(n = 6, expr = runif(30, 20, 100)), 
  y = sample(x = 1:3, size = 30, replace = TRUE)
)

reg <- lm(
  x.1 ~ x.2 + x.3, data = df_1
)

saveRDS(object = reg, file = 'regression.rds')

out_1 <- readRDS('regression.rds')
```

Regression{data-navmenu='Forecast'}
======================================================================

Sidebar {.sidebar}
----------------------------------------------------------------------

**Panel**
```{r}

sliderInput(
  inputId = 'x.2', label = 'Advertising expenses:', 
  value = mean(df_1$x.2), min = min(df_1$x.2), max = max(df_1$x.2)
)

sliderInput(
  inputId = 'x.3', label = 'Sellers:', 
  value = mean(df_1$x.3), min = min(df_1$x.3), max = max(df_1$x.3)
)
```

```{r}

reac_1 <- reactive({
  tibble(
  x.2 = input$x.2, 
  x.3 = input$x.3
  )
})

predict_1 <- reactive({
  predict(
  out_1, reac_1()
  )
})
```

Column{}
----------------------------------------------------------------------

### HERE DOES NOT APPEARS IN DASHBOARD
```{r}

renderValueBox({
  valueBox(
  value = scales::dollar(predict_1(), prefix = 'R$ ', big.mark = '.', 
decimal.mark = ','), 
caption = ifelse(test = predict_1() < 33.3, 'Low price', 
                 ifelse(test = predict_1() < 66.6, 'Medium price', 'High price')), 
icon = ifelse(predict_1() < 33.3, 'fa-cc-visa', 
              ifelse(test = predict_1() < 66.6, 'fa-cc-mastercard', 'fa-cc-amex')), 
color = ifelse(predict_1() < 33.3, 'pink', 
               ifelse(test = predict_1() < 66.6, 'orange', 'lightgreen'))
  )
})
```

### HERE APPEARS IN DASHBOARD
```{r}
plot(reg)
```

The result:

enter image description here

How naming the section (HERE DOES NOT APPEAR IN DASHBOARD)?

Also, a box with renderValueBox is not generated when you click "Open in Browser". See:

enter image description here

What is blocking the renderValueBox from working in Browser?


Solution

  • I think this occurs because the renderValueBox() strips the outer div containing your section header.

    If we instead use a custom version of renderValueBox() that does not strip the outer div we can achieve your desired output:

    ---
    title: "Statistics"
    output: 
      flexdashboard::flex_dashboard:
      orientation: columns
    vertical_layout: fill
    theme: cerulean
    runtime: shiny
    ---
    
    ```{r}
    library(tidyverse)
    library(flexdashboard)
    
    # custom rendervaluebox that does not strip outer div
    my_renderValueBox <- function(expr, env = parent.frame(), quoted = FALSE) {
      # Convert the expression to a function
      vbox_fun <- shiny::exprToFunction(expr, env, quoted)
    
      # Wrap that function in another function which strips off the outer div and
      # send it to renderUI.
      shiny::renderUI({
        vbox <- vbox_fun()
        if (promises::is.promising(vbox)) {
          vbox %...>%
            { . }
        } else {
          vbox
        }
      })
    }
    
    set.seed(123)
    
    df_1 <- data.frame(
      x = replicate(n = 6, expr = runif(30, 20, 100)), 
      y = sample(x = 1:3, size = 30, replace = TRUE)
    )
    
    reg <- lm(
      x.1 ~ x.2 + x.3, data = df_1
    )
    
    saveRDS(object = reg, file = 'regression.rds')
    
    out_1 <- readRDS('regression.rds')
    ```
    
    Regression{data-navmenu='Forecast'}
    ======================================================================
    
    Sidebar {.sidebar}
    ----------------------------------------------------------------------
    
    **Panel**
    ```{r}
    
    sliderInput(
      inputId = 'x.2', label = 'Advertising expenses:', 
      value = mean(df_1$x.2), min = min(df_1$x.2), max = max(df_1$x.2)
    )
    
    sliderInput(
      inputId = 'x.3', label = 'Sellers:', 
      value = mean(df_1$x.3), min = min(df_1$x.3), max = max(df_1$x.3)
    )
    ```
    
    ```{r}
    
    reac_1 <- reactive({
      tibble(
        x.2 = input$x.2, 
        x.3 = input$x.3
      )
    })
    
    predict_1 <- reactive({
      predict(
        out_1, reac_1()
      )
    })
    ```
    
    Column{}
    ----------------------------------------------------------------------
    
    ### HERE NOW APPEARS AS DESIRED
    ```{r}
    my_renderValueBox({
      valueBox(
        value = scales::dollar(predict_1(), prefix = 'R$ ', big.mark = '.', 
                               decimal.mark = ','), 
        caption = ifelse(test = predict_1() < 33.3, 'Low price', 
                         ifelse(test = predict_1() < 66.6, 'Medium price', 'High price')), 
        icon = ifelse(predict_1() < 33.3, 'fa-cc-visa', 
                      ifelse(test = predict_1() < 66.6, 'fa-cc-mastercard', 'fa-cc-amex')), 
        color = ifelse(predict_1() < 33.3, 'pink', 
                       ifelse(test = predict_1() < 66.6, 'orange', 'lightgreen'))
      )
    })
    ```
    
    ### HERE APPEARS IN DASHBOARD
    ```{r}
    plot(reg)
    ```
    

    dashboard