如何使用 Rshiny 和 Rmarkdown 使用两个输入的值框? R

How to use a value box of two inputs using Rshiny and Rmarkdown? R

我的目标是使用我的一些输入来更新一个值框。

这是我的代码

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

```{r setup, include=FALSE}
library(flexdashboard)
library(ggplot2)
library(plotly)
library(DT)
library(htmltools)
library(readr)
library(shiny)
library(knitr)

```
  
  Inputs {.sidebar data-width=300}
=====================================


```{r}
selectInput("input_type","Select Cylinder Size: ", c("All", mtcars$cyl))
selectInput("input_type2", "Select # of Gears: ", c("All", mtcars$gear))

mtcars2 <- reactive({
  d <- mtcars
  if(input$input_type !="All")
    d <- subset(d, cyl == input$input_type)
  if(input$input_type2 !="All")
    d <- subset(d, gear == input$input_type2)
  d
  })



```

# Page 1 

Column {data-width=600}
-----------------------------------------------------------------------

### Table
```{r echo=FALSE}

renderDataTable(
  datatable(mtcars2())
  ) 

```


Column {data-width=300}
-----------------------------------------------------------------------
### Value Box

```{r}
valueBox(0)
```

我的目标是得到一个值框,我可以在其中根据过滤后的剩余数据获得 hp 的总和。

例如,在此屏幕截图中,我过滤后只看到 6 缸和 3 个齿轮的汽车。 我的目标是查看值框中填充的 hp 总和。

附加代码


renderValueBox({
    valueBox(
      mtcars2() %>% summarise(Sum=sum(hp)) %>% pull(Sum) ,
      paste("Total HP:", input$input2)
    )
  })

添加

library(dplyr)

到您的 YAML header 然后替换

valueBox(0)

  renderValueBox({
    valueBox(
      "Total HP",
      mtcars2() %>% summarise(Sum=sum(hp)) %>% pull(Sum)
    )
  })

或类似。