【问题标题】:Return the associated value (same row) of a dataframe with renderValueBox使用 renderValueBox 返回数据框的关联值(同一行)
【发布时间】:2021-11-05 18:24:49
【问题描述】:

我正在尝试使用renderValueBox 返回与另一个关联的值例如:

如果John返回4321

如果Louis 返回1234

如果Marco 返回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"
  )
})
```

有什么想法吗?谢谢。

【问题讨论】:

    标签: r shiny flexdashboard


    【解决方案1】:

    f_1函数改为

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      • 2014-08-20
      • 1970-01-01
      • 2013-11-02
      • 2018-10-13
      相关资源
      最近更新 更多