Return 数据帧与 renderValueBox 的关联值(同一行)

Return the associated value (same row) of a dataframe with renderValueBox

我正在尝试 return 与另一个关联的值 renderValueBox 例如:

如果John return 4321

如果Louis return 1234

如果Marco return 30

问题出在这部分函数(f_1):

f_1 <- function(x) {
  if (is.character(x)) {
  df[1, 2] # it's wrong!
  } else (NULL)
}

我的代码:

---
title: "Untitled"
runtime: shiny
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

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

```{r}
name = c('John', 'Louis', 'Marco')
value = c(4321, 1234, 30)
df <- data.frame(name, value)

f_1 <- function(x) {
  if (is.character(x)) {
  df[1, 2] # it's wrong!
  } else (NULL)
}

reac <- reactive({
  tibble(
    input$name, 
    input$value
  )
})

pred <- reactive({
  temp <- reac()
  input$name
})
```

side1{.sidebar}
---------------------------------

**Control panel**
```{r}
selectInput(inputId = "name",
            label="Names:",
            choices = unique(df$name),
            selected = "",
            multiple=FALSE
            )
```

calc1{}
---------------------------------

###
```{r}
renderValueBox({
  expr = valueBox(
    value = f_1(x = pred()), 
    caption = "Value", 
    color = "#008bbb", 
    icon = "fa-users"
  )
})
```

有什么想法吗?谢谢。

f_1函数更改为

f_1 <- function(x) df$value[df$name == x]