Knitr doesn't align title and table properly - r-markdown

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")

Related

Using date in Shiny valueBox

How can I show the current date in the subtitle of a Shiny valueBox?
output$approvalBox <- renderValueBox({
valueBox("95",
"Number of School Days as of `r Sys.Date()`
You can use paste()
valueBox(value = "95",
subtitle = paste("Number of School Days as of", Sys.Date()))

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)

Reactive coloring in DT table

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") ))
)
```

Select row to display in Shiny data table with selectInput

I'm new to figuring out reactivity in shiny. I want to use selectInput to choose the name of a row and have the table display just that row and then several columns.
For example, if my rows are people ("Anna","Tim","Larry") and my columns are variables ("A","B","C") I want the selectInput to show "Anna" and the data table to display variables A,B, and C for only Anna.
I'm stuck on how to do this.
ui <- shinyUI(
fluidPage(
fluidRow(
column(2, selectInput("name", "Select a Name:",
c("Anna"= "smith.anna",
"Tim" = "miller.tim"))),
column(6, "People Table", tableOutput("mytable")
))))
server <- function(input, output) {
output$mytable <- renderTable({
mydataset[mydataset, input$name]})
}
I'm pretty sure it's my server functionality that's messed up, but all tips are helpful! Thanks!
See my comment:
mydataset <- data.frame(A = 1:3, B = 4:6, C = 7:9)
row.names(mydataset) <- c("smith.anna", "miller.tim", "page.larry")
ui <- shinyUI(
fluidPage(
fluidRow(
column(2, selectInput("name", "Select a Name:",
c("Anna"= "smith.anna",
"Tim" = "miller.tim"))),
column(6, "People Table", tableOutput("mytable")
))))
server <- function(input, output) {
output$mytable <- renderTable({
mydataset[input$name, ]
})
}
shinyApp(ui, server)

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")
})