[英]R Shiny - Display Table Based on Input Parameter
For the life of me I can't work this out, I'm using Flexdashboard in R Studio and I have two tables. 为了我的一生,我无法解决这个问题,因为我在R Studio中使用Flexdashboard,但我有两个表。 What I want to be able to do is to switch the table being being shown via a selectInput. 我想要做的是通过selectInput切换正在显示的表。 My selectInput is currently: 我的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"))
```
The csv files for MB_Val and DS_Val can be extracted from here: 可以从此处提取MB_Val和DS_Val的csv文件:
My two charts are as follows: 我的两个图表如下:
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()
and 和
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")
I've tried a number of things, the most recent being this. 我已经尝试了很多事情,最近的就是这个。 I'd love to be able to keep all the formatting too if possible. 如果可能,我也希望能够保留所有格式。
```{r, echo = FALSE}
div(renderTable({ifelse(input$platform %in% c("MB"),MB_Val,DS_Val)}),
style = "font-size:80%")
```
If you print the contents of your kable_extra
objects, you can see that their output is HTML: 如果打印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>
...
...
So instead of renderTable
you should use renderUI
: 因此,而不是renderTable
你应该使用renderUI
:
```{r, echo = FALSE}
renderUI( {
data <- ifelse(input$platform %in% c("MB"), MB_table, DS_table)
HTML(data)
})
```
I assigned the output objects to MD_table
and DS_table
in the setup chunk, since you haven't included an assignment in your example: 我将输出对象分配给设置块中的MD_table
和DS_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")
```
Result: 结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.