Reactive coloring in DT table - shiny

Need help. Below is the application. ColB is to be colored based on numbers in ColA. Below is the condition table
Just to brief : if ColB is 2, So it is lesser than 6 (12/2), it should be red and similarly for others. I tried to build the code myself and came up with below . But looks like there is some issue in the code . I have attached the output also below and the logic is not working properly.
---
title: "Untitled"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(DT)
```
```{r}
tab1 <- data.frame(ColA = c(12,34,45,56), ColB = c(2,32,30,56))
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
DT::DTOutput("table1")
output$table1 <- DT::renderDT(
datatable(tab1))
```
Below is the output I got
So As per the code, the highlighted arrows are showing color.
First Arrow (Supposed to be red but it is showing as Yellow)
Second Arrow (Supposed to be Yellow but it is showing as Green)
Note : ColB is randomly generated and you may not see these numbers when you run it. But you observe randomly, this issue you will find for sure when you run as well. Not sure what is wrong in the code . Below is the code for your reference

You could use formatStyle from DT (link):
---
title: "Untitled"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(DT)
```
```{r}
tab1 <- data.frame(ColA = c(12,34,45,56), ColB = c(2,32,30,56)) %>%
dplyr::mutate(backgroundColB = case_when(
ColA==ColB ~ 1,
ColA/2>ColB ~ -1,
ColA/2<ColB ~ 0
))
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
DT::DTOutput("table1")
output$table1 <- DT::renderDT(
datatable(tab1, options=list(columnDefs = list(list(visible=FALSE, targets=2)))) %>%
formatStyle('ColB', "backgroundColB",
backgroundColor = styleInterval(c(-.5,.5), c("red","yellow","green") ))
)
```

Related

checkboxGroupInput in Shiny doesn't work when input data is data table not data frame

I have a case where I need to view a data table in Shiny with dynamic user-selected columns to view. This demo in Shiny Gallery was very insightful as a start. But when I applied it to my specific code where I use a data table rather than a data frame the main panel throws an error of "'data' must be 2-dimensional (e.g. data frame or matrix)". The only reason I got figure out for this error is that the input$show_vars does not work when the input data is a data table.
I present here two samples of codes to show the problem. The first one works well when the diamonds data is a data frame. The other one is the same code but the diamonds table is converted in data table in server section.
Appreciate any assistance to fix the code such that it works well when the input data is data table class.
Scenario1:
library(shiny)
library(ggplot2) # for the diamonds dataset
ui <- fluidPage(
title = "Examples of DataTables",
sidebarLayout(
sidebarPanel(
checkboxGroupInput("show_vars", "Columns in diamonds to show:",
names(diamonds), selected = names(diamonds))
),
mainPanel(
tabsetPanel(
tabPanel("diamonds", DT::dataTableOutput("mytable1")),
)
)
)
)
server <- function(input, output) {
# choose columns to display
diamonds2 = diamonds[sample(nrow(diamonds), 1000), ]
output$mytable1 <- renderDataTable({
diamonds2[, input$show_vars]
})
}
shinyApp(ui, server)
`
Scenario2:
library(shiny)
library(ggplot2) # for the diamonds dataset
ui <- fluidPage(
title = "Examples of DataTables",
sidebarLayout(
sidebarPanel(
checkboxGroupInput("show_vars", "Columns in diamonds to show:",
names(diamonds), selected = names(diamonds))
),
mainPanel(
tabsetPanel(
tabPanel("diamonds", DT::dataTableOutput("mytable1")),
)
)
)
)
server <- function(input, output) {
# choose columns to display
diamonds2 = as.data.table(diamonds[sample(nrow(diamonds), 1000), ])
output$mytable1 <- renderDataTable({
diamonds2[, input$show_vars]
})
}
shinyApp(ui, server)

Always Visible Scrollbar on Shiny Reactable Table

I have built a Shiny app and one page has a very wide reactable table. Some users are having trouble navigating the width of the table. How can I add horizontal and vertical scroll bars they can click and drag? Here is a minimal example.
library(shiny)
library(dplyr)
library(reactable)
ui <- fluidPage(
titlePanel("reactable example"),
reactableOutput("table")
)
iris_wide <- iris %>% bind_cols(iris) %>% bind_cols(iris) %>% bind_cols(iris) %>% bind_cols(iris) %>% bind_cols(iris) %>% bind_cols(iris)
server <- function(input, output, session) {
output$table <- renderReactable({
reactable(iris_wide)
})
}
shinyApp(ui, server)

Group_by, summarise reactivity not working in R Shiny

Reactivity is working everywhere else in my app, but when I try to create a summary statistic table the table returns the name of the variable selected instead of the summary statistic:
ui <- selectInput('summary_metric', 'Select Metric', choices = c('height', 'weight'))
tableOuput('summary_table')
server <- function(input, output) {
output$summary_table <- renderTable({ data %>% group_by(Age_Group) %>%
summarise(min = min(input$summary_metric),
median = median(weight))})
Returns a table that reads:
Age_Group
min
median
18-29
weight
170
30-39
weight
180
40-49
weight
190
when 'weight' is selected in the UI and toggles to 'height' in the min column when 'height' is selected in the UI. So instead of calculating the minimum, it is just returning the variable name. Any thoughts as to what I'm missing?

Knitr doesn't align title and table properly

Consider the following reprex in rmarkdown
---
title: "Test"
author: "TestUser"
date: "19/05/2020"
output: pdf_document
---
#### **test table**
```{r, eval=TRUE, warning=FALSE, message=FALSE}
library(kableExtra)
library(tidyverse)
head(mtcars) %>%
kable(format = "latex")
```
How can do I prevent the title above the code chunk to be displayed next to the table?
Figured it out
Just needed to put kable_styling(latex_options = "hold_position")

Toggle ggvis-layer with checkbox in a Shiny application

I want to add a checkbox that toggles the layers shown in a ggvis plot in a Shiny application.
library(shiny)
library(ggvis)
ui <- shinyUI(fluidPage(sidebarLayout(
sidebarPanel(
checkboxInput('loess','loess',TRUE)
),
mainPanel(
ggvisOutput("plot")
)
)))
server <- shinyServer(function(input, output) {
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points() %>%
# if(input$loess) layer_smooths() %>%
bind_shiny("plot", "plot_ui")
})
shinyApp(ui = ui, server = server)
Is this possible to do in the ggvis pipeline using shiny the same gist as the commented line in the code above?
I don't think you can use the pipe directly for this. You also need to be in a reactive environment to access input$loess. You could do:
observe({
plot <- mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points()
if(input$loess) plot <- plot %>% layer_smooths()
plot %>% bind_shiny("plot", "plot_ui")
})