How to specify the width of a sidebarPanel in pixels? - css

How to specify the width of a sidebarPanelin pixels? The width argument is not accurate enough in my case.
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(width = 5), # This is not accurate enough!
mainPanel()))
server <- function(input, output, session) {}
shinyApp(ui, server)

You can also specify the width as a function of the percentage, 100% = full width instead of hard-coding it to px
library(shiny)
ui <- fluidPage(
sidebarLayout(
div(style="width: 70%;",sidebarPanel(width = 5)), # This is not accurate enough!
mainPanel()))
server <- function(input, output, session) {}
shinyApp(ui, server)

With the help of R shiny - background of sidebar panel I found the following solution:
library(shiny)
ui <- fluidPage(
tags$head(tags$style(HTML('#sidebar {width: 100px;}'))),
sidebarLayout(
sidebarPanel(id = "sidebar"),
mainPanel()))
server <- function(input, output, session) {}
shinyApp(ui, server)
This one does not affect other wellPanels.

Related

Shiny - Go to another section in same page

I am using below code and try to do below action.
Click on action button to go to next table. How can I do this?
library(shiny)
library(shinydashboard)
library(ggplot2)
ui <- fluidPage(
fluidRow(box(
actionButton("btn1", "Go to Next Table"),
tableOutput("tbl1")
)),
fluidRow(box(
tableOutput("tbl2")
))
)
server <- function(input, output, session) {
output$tbl1 <- renderTable(mtcars)
output$tbl2 <- renderTable(mpg)
}
shinyApp(ui, server)
Here's one solution:
library(shiny)
library(shinydashboard)
library(ggplot2)
ui <- fluidPage(
fluidRow(box(
actionButton("btn1", "Go to Next Table",
onclick = "location.href='#table2';"),
tableOutput("tbl1")
)),
fluidRow(id = "table2", box(
tableOutput("tbl2")
))
)
server <- function(input, output, session) {
output$tbl1 <- renderTable(mtcars)
output$tbl2 <- renderTable(mpg)
}
shinyApp(ui, server)
I've added a unique ID to the location in the UI - here the 2nd fluidRow, then added an onclick javascript function to the actionButton also in the UI. No server function means all the work is done by the user's browser which is handy sometimes.
You can add infinite complexity to the Javascript here to customise it to fit your needs.

Vertically align text in reactable cells in shiny app

Is there a way to vertically align (middle) the text in the cells in a reactable rendered within a shiny app?
minimal example below. I tried some CSS options but no luck so far.
library(shiny)
library(reactable)
ui <- fluidPage(
titlePanel("reactable example"),
reactableOutput("table")
)
server <- function(input, output, session) {
output$table <- renderReactable({
reactable(iris)
})
}
shinyApp(ui, server)
thank you
You can use defaultColDef to set default attributes. Below code snippet should vertically align text centrally:
library(shiny)
library(reactable)
ui <- fluidPage(
titlePanel("reactable example"),
reactableOutput("table")
)
server <- function(input, output, session) {
output$table <- renderReactable({
reactable(iris,
defaultColDef = colDef(
align = "center")
)
})
}
shinyApp(ui, server)
Below is the output after running the code. The output is both centrally & horizontally aligned

Remove horizontal scrollbar on Shiny UI's input

This is a minimal app that reproduces my problem:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("input1", "input1", min = as.Date("2020-02-03"), max = as.Date("2020-12-30"),
value = c(as.Date(Sys.Date()), as.Date("2020-12-30"))),
hr(),
splitLayout(checkboxGroupInput("input2", "input2", choices = c("a", "b")),
verticalLayout(checkboxInput("input3", "input3")))),
mainPanel()))
server <- function(input, output, session) {
}
shinyApp(ui, server)
The app generated gives an horizontal scrollbar for input3, even when the screen size allows it to have more than enough space. Lurking on other similar questions, people recommend giving it a css property with overflow:hidden, but I can't find where to put this piece of code. Other approaches are obviously welcome.
You need to include custom CSS instructions at the head of the UI part.
library(shiny)
ui <- fluidPage(
# Include custom CSS
tags$head(
tags$style(HTML('.shiny-split-layout>div {overflow: hidden;}')),
),
sidebarLayout(
sidebarPanel(
sliderInput("input1", "input1", min = as.Date("2020-02-03"), max = as.Date("2020-12-30"),
value = c(as.Date(Sys.Date()), as.Date("2020-12-30"))),
hr(),
splitLayout(checkboxGroupInput("input2", "input2", choices = c("a", "b")),
verticalLayout(checkboxInput("input3", "input3")))),
mainPanel()))
server <- function(input, output, session) {
}
shinyApp(ui, server)

Fix width of a DT in shiny

I'm trying to fix the width of very wide table in shiny but haven't mange to do it. I try the answer here Shrink DT::dataTableOutput Size but it didn't work, I also tried the answer from here https://github.com/rstudio/DT/issues/29 with percentage and pixels for all the columns and it didn't work neither. This is an example of the table and problem I have:
shinyApp(
ui = fluidPage(
DT::dataTableOutput("table")
),
server <- function(input, output) {
x <- cbind(iris,iris,iris,iris)
output$table <- DT::renderDataTable(x)
}
)
I think this does what you want:
library(shiny)
shinyApp(
ui = fluidPage(
DT::dataTableOutput("table",width='500px')
),
server <- function(input, output) {
x <- cbind(iris,iris,iris,iris)
output$table <- DT::renderDataTable(x,options=list(scrollX=T))
}
)

Flexible height of plot in shiny app

I´m searching for a method to change the height parameter in plotOutput() depending on an input$.. value.
Any suggestions?
Samuel
Here you go:
library(shiny)
shinyApp(
ui = fluidPage(
numericInput("height", "height", 300),
plotOutput("plot", height = "auto")
),
server = function(input, output, session) {
output$plot <- renderPlot({
plot(1:10)
},
height = function(x) input$height)
}
)

Resources