在flexdashboard上命名部分和浏览应用程序时出现问题

时间:2019-07-17 08:29:57

标签: r r-markdown flexdashboard

我尝试使用flexdashboard为节命名。但是,我做不到。我尝试写HERE DOES NOT APPEAR IN DASHBOARD作为标题,但是不起作用。我的代码:

---
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)
```

结果:

enter image description here

如何命名(HERE DOES NOT APPEAR IN DASHBOARD)部分?

此外,当您单击“在浏览器中打开”时,不会生成带有renderValueBox的框。参见:

enter image description here

什么阻止renderValueBox在浏览器中工作?

1 个答案:

答案 0 :(得分:0)

我认为发生这种情况是因为renderValueBox()剥离了包含节标题的外部div。

如果我们改用不剥离外部div的自定义版本renderValueBox(),则可以实现所需的输出:

---
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