0

For the life of me I can't work this out, I'm using Flexdashboard in R Studio and I have two tables. What I want to be able to do is to switch the table being being shown via a selectInput. My selectInput is currently:

```{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:

DS_Val here

MB_Val here

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%")
```
2
  • please provide a reproducible example. stackoverflow.com/questions/5963269/… Commented Jan 29, 2018 at 13:17
  • Have added links to csv files to allow re-production. Commented Jan 29, 2018 at 13:33

1 Answer 1

4

If you print the contents of your kable_extra objects, you can see that their output is 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:

```{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:

```{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:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.