R 中调整 renderValueBox 函数

Adjustment renderValueBox function in R

我创建了一个函数来根据3个变量计算预测值。

但是我无法调整 renderValueBox 函数来进行计算。

代码在这里(write in rmarkdown):

---
title: "Untitled"
runtime: shiny
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    theme: cosmo
---

```{r}
library(tidyverse)

df_2 <- data.frame(
  m = seq(from = .05, to = .95, by = .05), 
  d = seq(from = .04, to = .94, by = .05), 
  q = c(1:18, 2000)
)

desconto <- function(mc, desc, qtde) {
  ((((mc) / (mc - desc)) - 1) * qtde + qtde)
}

price <- desconto(
  mc = df_2$m, 
  desc = df_2$d, 
  qtde = df_2$q
)
```

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

Predict values
=================================
Sidebar{.sidebar data-width=300}
---------------------------------

```{r}
# Widgets
sliderInput(
  inputId = 'm', label = 'mc', 
  value = mean(round(x = df_2$m, digits = 0)) * 100, 
  min = min(round(x = df_2$m, digits = 0)) * 100, 
  max = max(round(x = df_2$m, digits = 0)) * 100
)

sliderInput(
  inputId = 'd', label = 'dc', 
  value = mean(round(x = df_2$d, digits = 0)) * 100, 
  min = min(round(x = df_2$d, digits = 0)) * 100, 
  max = max(round(x = df_2$d, digits = 0)) * 100
)

numericInput(
  inputId = 'q', label = 'qtde', 
  value = mean(round(x = df_2$q, digits = 0)), 
  min = min(round(x = df_2$q, digits = 0)), 
  max = max(round(x = df_2$q, digits = 0))
)
```

```{r}
# Reactive object
reac_1 <- reactive({
  tibble(
    m = input$m, 
    d = input$d, 
    q = input$q
  )
})

# Reactive output
pred_4 <- reactive({
  x = reac_1()$price
})
```

Calculator{}
----------------------
```{r}
renderValueBox(
  valueBox(
    value = pred_4()
  )
)
```

例如,对于 mc = 50dc = 20qtde = 1500,预期结果将是 2500

非常感谢。

问题出在 reac_1()$price 行。看你怎么定义reac_1,没有price列。 (您也永远不会使用您在第一个代码块中定义的 price 对象,为了清楚起见,我将其删除)

一个解决方案修改 pred_4 如下:

pred_4 <- reactive({
  temp <- reac_1()
  desconto(
  mc = temp$m,
  desc = temp$d,
  qtde = temp$q
)
})