对于我的生活我无法解决这个问题,我在R Studio中使用Flexdashboard,我有两张桌子。我想要做的是通过selectInput切换正在显示的表。我的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%")
```
答案 0 :(得分:1)
如果您打印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>
...
...
因此,您应使用renderTable
代替renderUI
:
```{r, echo = FALSE}
renderUI( {
data <- ifelse(input$platform %in% c("MB"), MB_table, DS_table)
HTML(data)
})
```
我已将输出对象分配到设置块中的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")
```
结果: