R Shiny - 根据输入参数显示 Table

R Shiny - Display Table Based on Input Parameter

我一辈子都无法解决这个问题,我在 R Studio 中使用 Flexdashboard,并且我有两个 table。我想要做的是切换通过 selectInput 显示的 table。我的 selectInput 当前是:

```{r setup, include=FALSE}
library(flexdashboard)
library(DT)
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(png)
library(grid)
library(kableExtra)
library(knitr)
```

```{r, echo = FALSE}
selectInput("platform", label = "Select Platform:",
              choices = c("MB","DS"))
```

MB_Val 和 DS_Val 的 csv 文件可以从这里提取:

DS_Val here

MB_Val here

我的两张图表如下:

MB_Val %>%
  mutate(Val = cell_spec(
    format(round(Val, 2), nsmall = 2), "html", color = "white", bold = T,
    background = "#7FC64F")) %>% 
  mutate(ValFm = cell_spec(
    format(round(ValFm, 2), nsmall = 2), "html", color = "white", bold = T,
    background = "#7FC64F")) %>% 
  kable("html", escape = F, align = c('l',rep('c',ncol(MB_Val)-1))) %>%
  kable_styling(bootstrap_options = c("striped", "condensed","hover")) %>%
  scroll_box()

DS_Val %>%
  mutate(Val = cell_spec(
    format(round(Val, 2), nsmall = 2), "html", color = "white", bold = T,
    background = "#7FC64F")) %>%
  mutate(ValFm = cell_spec(
    format(round(ValFm, 2), nsmall = 2), "html", color = "white", bold = T,
    background = "#7FC64F")) %>%
 kable("html", escape = F,align = c('l',rep('c',ncol(DS_Val)-1))) %>%
 kable_styling(bootstrap_options = c("striped", "condensed","hover")) %>%
 scroll_box(height = "200px")

我尝试了很多东西,最近的就是这个。如果可能的话,我也希望能够保留所有格式。

```{r, echo = FALSE}
div(renderTable({ifelse(input$platform %in% c("MB"),MB_Val,DS_Val)}),
 style = "font-size:80%")
```

如果打印 kable_extra 对象的内容,您可以看到它们的输出是 HTML:

<div style="border: 1px solid #ddd; padding: 5px; overflow-y: scroll; height:200px; "><table class="table table-striped table-condensed table-hover" style="margin-left: auto; margin-right: auto;">
 <thead>
  <tr>
   <th style="text-align:left;"> Player.Name </th>
   <th style="text-align:center;"> Tm </th>
   <th style="text-align:center;"> Pos </th>
   <th style="text-align:center;"> Sal </th>
   <th style="text-align:center;"> Gms </th>
...
...

所以你应该使用 renderUI:

而不是 renderTable
```{r, echo = FALSE}
renderUI( {
  data <- ifelse(input$platform %in% c("MB"), MB_table, DS_table)
  HTML(data)
})
```

我在设置块中将输出对象分配给 MD_tableDS_table,因为您的示例中没有包含分配:

```{r setup, include=FALSE}
library(flexdashboard)
library(DT)
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(png)
library(grid)
library(kableExtra)
library(knitr)
library(shiny)
library(readr)
library(dplyr)

DS_Val <- read_csv("DS_Val.csv")
MB_Val <- read_csv("MB_Val.csv")


MB_table <- MB_Val %>%
  mutate(Val = cell_spec(
    format(round(Val, 2), nsmall = 2), "html", color = "white", bold = T,
    background = "#7FC64F")) %>% 
  mutate(ValFm = cell_spec(
    format(round(ValFm, 2), nsmall = 2), "html", color = "white", bold = T,
    background = "#7FC64F")) %>% 
  kable("html", escape = F, align = c('l',rep('c',ncol(MB_Val)-1))) %>%
  kable_styling(bootstrap_options = c("striped", "condensed","hover")) %>%
  scroll_box()

DS_table <- DS_Val %>%
  mutate(Val = cell_spec(
    format(round(Val, 2), nsmall = 2), "html", color = "white", bold = T,
    background = "#7FC64F")) %>%
  mutate(ValFm = cell_spec(
    format(round(ValFm, 2), nsmall = 2), "html", color = "white", bold = T,
    background = "#7FC64F")) %>%
 kable("html", escape = F,align = c('l',rep('c',ncol(DS_Val)-1))) %>%
 kable_styling(bootstrap_options = c("striped", "condensed","hover")) %>%
 scroll_box(height = "200px")
```

结果: