Search code examples
rshinyflexdashboard

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


I'm trying to return the values associated with another one with renderValueBox For example:

If John return 4321

If Louis return 1234

If Marco return 30

The problem lies in this part of the function (f_1):

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

My code:

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

Any idea? Thanks.


Solution

  • Change the f_1 function to

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