I am trying to build a dashboard using semantic.dashboard library. I see that all the components are aligned at the center. How can I change the layout?
I have taken the following code from examples in the semantic.dashboard repo. It works absolutely fine when I run it. I see that there is a space between the sidebar menu and boxes in the main body. I want the components to be aligned without any space from the side bar menu. Is there any way I could achieve this?
library(shiny)
library(shiny.semantic)
library(semantic.dashboard)
library(ggplot2)
library(plotly)
library(DT)
ui <- dashboardPage(
dashboardHeader(
dropdownMenuOutput("dropdown"),
dropdownMenu(type = "notifications",
taskItem("Project progress...", 50.777, color = "red")),
dropdownMenu(
icon = uiicon("red warning sign"),
notificationItem("This is an important notification!", color = "red")
)
),
dashboardSidebar(side = "left",
sidebarMenu(
menuItem(
tabName = "plot_tab",
text = "My plot",
icon = icon("home")
),
menuItem(
tabName = "table_tab",
text = "My table",
icon = icon("smile")
)
)),
dashboardBody(tabItems(
tabItem(tabName = "plot_tab",
fluidRow(
valueBox(
"Unread Mail",
44,
icon("mail"),
color = "blue",
width = 5
)
),
fluidRow(
box(
title = "Sample box",
color = "blue",
width = 11,
selectInput(
inputId = "variable1",
choices = names(mtcars),
label = "Select first variable",
selected = "mpg"
),
selectInput(
inputId = "variable2",
choices = names(mtcars),
label = "Select second variable",
selected = "cyl"
),
plotlyOutput("mtcars_plot")
),
tabBox(
title = "Sample box",
color = "blue",
width = 5,
collapsible = FALSE,
tabs = list(
list(menu = "First Tab", content = "Some text..."),
list(menu = "Second Tab", content = plotlyOutput("mtcars_plot2"))
)
)
)),
tabItem(
tabName = "table_tab",
fluidRow(
valueBox(
"Unread Mail",
144,
icon("mail"),
color = "blue",
width = 6,
size = "small"
),
valueBox(
"Spam",
20,
icon("mail"),
color = "red",
width = 5,
size = "small"
),
valueBox(
"Readed Mail",
666,
icon("mail"),
color = "green",
width = 5,
size = "small"
)
),
fluidRow(
box(
title = "Classic box",
color = "blue",
ribbon = FALSE,
title_side = "top left",
width = 14,
tags$div(
dataTableOutput("mtcars_table")
,
style = paste0("color:", semantic_palette[["blue"]], ";")
)
)
)
)
)),
theme = "slate"
)
server <- function(input, output) {
output$mtcars_plot <-
renderPlotly(plot_ly(
mtcars,
x = ~ mtcars[, input$variable1],
y = ~ mtcars[, input$variable2],
type = "scatter",
mode = "markers"
))
output$mtcars_plot2 <-
renderPlotly(plot_ly(
mtcars,
x = ~ mtcars[, input$variable1],
y = ~ mtcars[, input$variable2],
type = "scatter",
mode = "markers"
))
output$mtcars_table <-
renderDataTable(mtcars, options = list(dom = 't'))
output$dropdown <- renderDropdownMenu({
dropdownMenu(
messageItem("User", "Test message", color = "teal", style = "min-width: 200px"),
messageItem("Users", "Test message", color = "teal", icon = "users"),
messageItem(
"See this",
"Another test",
icon = "warning",
color = "red"
)
)
})
}
shinyApp(ui, server)
It looks like the issue is behind the dashboardBody function. It uses a ui stackable container grid for each tab which is causing the large amount of padding.
There are two ways around it:
Hack around the dashboardBody function and remove "container" from the class:
dash_body <- function (...) {
shiny::div(
class = "pusher container", style = "min-height: 100vh;",
shiny::div(
class = "ui segment", style = "min-height: 100vh;",
shiny::tags$div(class = "ui stackable grid", ...)
),
semantic.dashboard:::body_js
)
}
Add CSS to remove the padding for this class (add first row in the dashboardBody arguments):
tags$style(".pusher.container .ui.segment .ui.stackable.container.grid {margin:0px!important;}")
Related
I created an application with the shinyMobile package and I would like to know if it is possible to make it available on the Play and Apple Store as a regular app.
My app:
library(shinyMobile)
if(interactive()){
library(shiny)
library(shinyMobile)
library(shinyWidgets)
shinyApp(
ui = f7Page(
title = "Tab layout",
f7TabLayout(
tags$head(
tags$script(
"$(function(){
$('#tapHold').on('taphold', function () {
app.dialog.alert('Tap hold fired!');
});
});
"
)
),
panels = tagList(
f7Panel(title = "Left Panel", side = "left", theme = "light", "Blabla", effect = "cover"),
f7Panel(title = "Right Panel", side = "right", theme = "dark", "Blabla", effect = "cover")
),
navbar = f7Navbar(
title = "Tabs",
hairline = FALSE,
shadow = TRUE,
leftPanel = TRUE,
rightPanel = TRUE
),
f7Tabs(
animated = FALSE,
swipeable = TRUE,
f7Tab(
tabName = "Tab1",
icon = f7Icon("envelope"),
active = TRUE,
f7Shadow(
intensity = 10,
hover = TRUE,
f7Card(
title = "Card header",
f7Stepper(
"obs1",
"Number of observations",
min = 0,
max = 1000,
value = 500,
step = 100
),
plotOutput("distPlot1"),
footer = tagList(
f7Button(inputId = "tapHold", label = "My button"),
f7Badge("Badge", color = "green")
)
)
)
),
f7Tab(
tabName = "Tab2",
icon = f7Icon("today"),
active = FALSE,
f7Shadow(
intensity = 10,
hover = TRUE,
f7Card(
title = "Card header",
f7Select(
inputId = "obs2",
label = "Distribution type:",
choices = c(
"Normal" = "norm",
"Uniform" = "unif",
"Log-normal" = "lnorm",
"Exponential" = "exp"
)
),
plotOutput("distPlot2"),
footer = tagList(
f7Button(label = "My button", href = "https://www.google.com"),
f7Badge("Badge", color = "orange")
)
)
)
),
f7Tab(
tabName = "Tab3",
icon = f7Icon("cloud_upload"),
active = FALSE,
f7Shadow(
intensity = 10,
hover = TRUE,
f7Card(
title = "Card header",
f7SmartSelect(
inputId = "variable",
label = "Variables to show:",
c("Cylinders" = "cyl",
"Transmission" = "am",
"Gears" = "gear"),
multiple = TRUE,
selected = "cyl"
),
tableOutput("data"),
footer = tagList(
f7Button(label = "My button", href = "https://www.google.com"),
f7Badge("Badge", color = "green")
)
)
)
)
)
)
),
server = function(input, output) {
output$distPlot1 <- renderPlot({
dist <- rnorm(input$obs1)
hist(dist)
})
output$distPlot2 <- renderPlot({
dist <- switch(
input$obs2,
norm = rnorm,
unif = runif,
lnorm = rlnorm,
exp = rexp,
rnorm
)
hist(dist(500))
})
output$data <- renderTable({
mtcars[, c("mpg", input$variable), drop = FALSE]
}, rownames = TRUE)
}
)
}
I produce conventional shinyapps, but I would like to know if there is a way to make a shinMobile available on mobile platforms.
I even tried to do something, but nothing that came close to a satisfactory solution.
Help! For the life of me, I can't get values to populate from the server to the infoBox in the UI.
I've tried to define the infoboxes from the server section, but the infoboxes will only appear if I construct them in the UI (as shown below).
The goal is to populate the boxes with filtered data based on user inputs, but I've abandoned this at this stage because I can't even pass a value from the server to the UI infobox here:
infoBox("Participants Trained",
value = renderText("AYval"), # tried every combo here
width = 12,color = "blue", # tried width = NULL
icon = icon("fa-solid fa-people-group"), fill = F)
A value shows when I hardcode a value in "value = ", but none of the render options, renderText, verbatimText, output$AYval, valueTextbox, listen(),react() will get a value that is hard-coded in the server side to show up in this infobox.
To get the dashboard to display boxes, I'm using header = tagList(useShinydashboard()). My guess is this useShinydashboard() is the culprit.
I thought this comment might be relevant:
Your code using lapply and the navbarPage doesn't generate the UI in
the proper namespace, since when using the navbarPage construct your
modules are "one level deeper".
The script:
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)
#library(shinyjs)
side_width <- 5
#completing the ui part with dashboardPage
ui <- navbarPage(fluid = TRUE,
theme = shinythemes::shinytheme("flatly"),
collapsible = TRUE,
header = tagList(
useShinydashboard()
),
tabPanel("START"),
tabPanel("Home Dashboard",
value = "Tab1",
# useShinyjs(),
fluidRow(
column(4,
# Selection Input ---------------------------------------------------------
selectInput(inputId = "AY","Academic Year",
multiple = T,
choices = unique(INDGEN$AcademicYear),
selected = unique(INDGEN$AcademicYear)
)),
column(4,
selectInput(inputId = "State","Select State",
choices = c("State","States"))),
column(4,
selectInput(inputId = "Program","Select Program",
choices = c("Program","Programs")))
),
fluidRow(column(12,
box(width = 4,
infoBox("Who?",
width = 12,color = "blue",
fill = F)
),
box(width = 4,
infoBox("Where?", width = 12,color = "blue",
fill = F)
),
box(width = 4,
infoBox("What?", width = 12,color = "blue",
fill = F))
)),
# UI Box R1 ---------------------------------------------------------------
fluidRow(column(12,
box(width = 4,
# uiOutput(infoBoxOutput("BOX1",width = NULL)),
infoBox("Participants Trained", value =
renderText("AYval"),
width = 12,color = "blue",
icon = icon("fa-solid fa-people-group"), fill = F)
),box(width = 4,
infoBox("Training Sites", nrow(data), width = 12,color = "blue",
icon = icon("fa-solid fa-school"), fill = F)
),box(width = 4,
infoBox("Training Programs Offered", nrow(data), width = 12,color = "blue",
icon = icon("fa-solid fa-book-open-reader"), fill = F))
)),
server <- function(input, output,session) {
output$AYval <- renderText({
textInput(13)
})
output$BOX1 <- renderInfoBox({
infoBox(title = "Participants Trained",
value = 13,
width = NULL,color = "blue",
icon = icon("fa-solid fa-people-group"), fill = T)
})
}#Server End
shinyApp(ui = ui,server = server,options = list(height = 1440))
Notice the "participant trained" box is empty. That's because that value isn't hard-coded. The rest are.
Here's a small reproducible example of how to change the value contents dynamically:
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)
data(iris)
ui <- navbarPage(
fluid = TRUE,
theme = shinythemes::shinytheme("flatly"),
collapsible = TRUE,
header = tagList(
useShinydashboard()
),
tabPanel("START"),
tabPanel(
title = "Home Dashboard",
value = "Tab1",
selectInput("column",
label = "Select a column",
choices = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
),
box(
width = 4,
infoBoxOutput("test")
)
)
)
server <- function(input, output, session) {
iris_sum <- reactive({
sum(iris[input$column])
})
output$test <- shinydashboard::renderInfoBox({
infoBox(
title = "Where?",
value = iris_sum(),
width = 12,
color = "blue",
fill = F
)
})
}
shinyApp(ui, server)
I am trying to add a navlistPanel to a box() in bs4Dash and when I set the column width to 12, the tabItems squeeze next to each other like this:
Adding the widths = c(4,8) argument to navlistPanel also gives me the same result. If I set the column width to 4 or less, the navlistPanel is properly formatted.
My goal is to have a box() with column = 12 and a vertical tab menu inside it with width = 4 and have the tabs look correct.
I've also tried using tabsetPanel with vertical = TRUE, but the content doesn't show up in the right location, it shows up below. See picture #2:
Reprexes for both are below:
library(shiny)
library(bs4Dash)
ui <- dashboardPage(
header = dashboardHeader(
title = dashboardBrand(
title = "My dashboard",
color = "primary",
href = "https://adminlte.io/themes/v3",
image = "https://adminlte.io/themes/v3/dist/img/AdminLTELogo.png"
)),
sidebar = bs4DashSidebar(),
controlbar = dashboardControlbar(),
body = dashboardBody(fluidRow(column(
12,
box(
title = "Title",
status = "primary",
solidHeader = TRUE,
collapsible = FALSE,
maximizable = TRUE,
color = "white",
height = 175,
width = NULL,
navlistPanel(
"Header",
tabPanel("First"),
tabPanel("Second"),
tabPanel("Third")
)
)
))),
help = FALSE,
dark = NULL,
scrollToTop = FALSE
)
server = function(input, output, session) {
}
shinyApp(ui, server)
library(shiny)
library(bs4Dash)
ui <- dashboardPage(
header = dashboardHeader(
title = dashboardBrand(
title = "My dashboard",
color = "primary",
href = "https://adminlte.io/themes/v3",
image = "https://adminlte.io/themes/v3/dist/img/AdminLTELogo.png"
)),
sidebar = bs4DashSidebar(),
controlbar = dashboardControlbar(),
body = dashboardBody(fluidRow(column(
12,
box(
title = "Title",
status = "primary",
solidHeader = TRUE,
collapsible = FALSE,
maximizable = TRUE,
color = "white",
height = 175,
width = NULL,
tabsetPanel(
id = NULL,
vertical = TRUE,
tabPanel("First",
"The content is below and I want it to the right of the tabset"),
tabPanel("Second"),
tabPanel("Third")
)
)
))),
help = FALSE,
dark = NULL,
scrollToTop = FALSE
)
server = function(input, output, session) {
}
shinyApp(ui, server)
I have a shiny app with a sidebar menue and several different tabs. Within each tab, there is a lot of content that is supposed to be seen together, so the tabs are quite lengthy and navigating can be a pain because a lot of scrolling is needed. However, spliting the content into sub-tabs is not an option.
I have thus tried to implement "location markers" as fake sub-tabs to navigate through.
This works fine, except when you are on another tab and you want to switch directly to the bottom of another tab, i.e. from subtab_1_1 directly to subtab_2_2.
In that case, the tab switches over correctly to subtab_2_1 but the scrollposition() afterwards does not actually scroll the full 50000 pixels, but to the maximum distance of the active tab (i.e. the bottom of Tab 1).
As #YBS pointed out, one solution would be to add lines to each Tab so that they all share the same length. However, that would make using the scroll bar to scroll manualy very unintuitive, as some tabs would go on for much longer as the content of the tab.
Is there any way to circumvent this limitation of window.scrollTo()?
Here is a minimal working example:
library(shiny)
library(shinydashboard)
library(shinyjs)
ui <- dashboardPage(
title = "Title",
dashboardHeader(title = "Header", titleWidth = 350),
skin = "blue",
dashboardSidebar(
width = 350,
disable = FALSE,
sidebarMenu(
id = "tabs",
menuItem(
text = "Tab 1",
tabName = "Tab_1",
icon = icon("database"),
hidden(menuSubItem(
text = "Subtab 1.1",
tabName = "Subtab_1_1",
icon = icon("angle-right")
)),
menuSubItem(
text = "Proxy Subtab 1.1",
tabName = "Proxy_Subtab_1_1",
icon = icon("angle-right")
),
menuSubItem(
text = "Subtab 1.2",
tabName = "Subtab_1_2",
icon = icon("angle-right")
)
),
menuItem(
text = "Tab 2",
tabName = "Tab_2",
icon = icon("database"),
hidden(menuSubItem(
text = "Subtab 2.1",
tabName = "Subtab_2_1",
icon = icon("angle-right")
)),
menuSubItem(
text = "Proxy Subtab 2.1",
tabName = "Proxy_Subtab_2_1",
icon = icon("angle-right")
),
menuSubItem(
text = "Subtab 2.2",
tabName = "Subtab_2_2",
icon = icon("angle-right")
)
)
)
),
dashboardBody(
useShinyjs(),
extendShinyjs(
text = "shinyjs.scrollposition = function(y) {window.scrollTo(0, y)};",
functions = c("scrollposition")
),
tags$script(HTML("$('body').addClass('fixed');")),
tabItems(
tabItem(
tabName = "Subtab_1_1",
fluidPage(
h1("This is Subtab 1_1"),
HTML(rep("<br/><br/><br/>↓<br/>", 10)),
h1("This is supposed to be Subtab 1_2")
)
),
tabItem(
tabName = "Subtab_2_1",
fluidPage(
h1("This is Subtab 2_1"),
plotOutput("Plot_1"),
plotOutput("Plot_2"),
plotOutput("Plot_3"),
plotOutput("Plot_4"),
plotOutput("Plot_5"),
plotOutput("Plot_6"),
h1("This is supposed to be Subtab 2_2")
)
)
)
)
)
server <- function(input, output, session) {
observeEvent(input$tabs, {
if(sum(c("Proxy_Subtab_1_1", "Proxy_Subtab_2_1","Subtab_1_2", "Subtab_2_2") %in% input$tabs) > 0) {
updateTabsetPanel(session, "tabs", switch(input$tabs,
"Proxy_Subtab_1_1" = "Subtab_1_1",
"Proxy_Subtab_2_1" = "Subtab_2_1",
"Subtab_1_2" = "Subtab_1_1",
"Subtab_2_2" = "Subtab_2_1")
)
js$scrollposition(case_when(input$tabs == "Proxy_Subtab_1_1" ~ 0,
input$tabs == "Proxy_Subtab_2_1" ~ 0,
input$tabs == "Subtab_1_2" ~ 50000,
input$tabs == "Subtab_2_2" ~ 50000)
)
}
})
output$Plot_1 <- output$Plot_2 <- output$Plot3 <-
output$Plot_4 <- output$Plot_5 <- output$Plot6 <- renderPlot(
ggplot(data.frame(
x = c(1, 2, 3),
y = c(1, 2, 3),
labels = c(
"",
"Some plots",
""
)
)
) +
geom_text(aes(x = x, y = y, label = labels), size = 6)
)
}
shinyApp(ui = ui, server = server)
I have an example shiny app as below. In order to the actionButton with selectInput, I need to add style='margin-top:25px'. Shinywidgets package has actionBttn widgets with some built-in style. For example, I like the one with style='gradient'. But I wonder how I can use css style to add margin on the top to align the actionBttn with other element?
library(shiny)
library(shinydashboard)
library(shinyWidgets)
ui <- dashboardPage(
dashboardHeader(title = "example"),
dashboardSidebar(),
dashboardBody(
box(width=12,
column(width = 3, dateRangeInput("dateRange", "Date Range",
start = "2017-01-01",
end = Sys.Date(),
min = "2001-01-01",
max = Sys.Date(),
format = "mm/dd/yy",
separator = " - ") ),
column(width=3, selectizeInput(inputId = 'var',
label='Select variable',
choices = c('cut', 'color'),
multiple=FALSE,
options = list(
maxItems = 1,
placeholder = '',
onInitialize = I("function() { this.setValue(''); }"))) ),
column(width=1, offset =2, actionButton('Apply', 'Apply', style='margin-top:25px') ),
column(width=3, actionBttn(
inputId = 'clear',
label = "Clear",
style = "gradient",
color = "danger" ) )
)
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
Hard to say with out your .css but You can find a sample in here
To add a style to an existing element created by a package, sometimes you have to wrap that element. Here's three approaches:
Wrap the element itself in a div with the style you want. May not work for all CSS elements.
Write your own custom function using the source from your desired element. Here I used the source from https://github.com/dreamRs/shinyWidgets/blob/ac8134e944f91fdcc4490ace6d839c46e7df02ff/R/actionBttn.R#L63
Add in some external CSS that targets only that element. This is my least favored approach because it moves the logic away from where it's actually being applied, and you have to keep track of it for each element you want to modify.
library(shiny)
library(shinyWidgets)
# new function for approach #2
actionBttn_with_style <- function(inputId, label = NULL, icon = NULL, style = "unite",
color = "default", size = "md", block = FALSE,
no_outline = TRUE, my_additional_style = "") {
value <- shiny::restoreInput(id = inputId, default = NULL)
style <- match.arg(
arg = style,
choices = c("simple", "bordered", "minimal", "stretch", "jelly",
"gradient", "fill", "material-circle", "material-flat",
"pill", "float", "unite")
)
color <- match.arg(
arg = color,
choices = c("default", "primary", "warning", "danger", "success", "royal")
)
size <- match.arg(arg = size, choices = c("xs", "sm", "md", "lg"))
tagBttn <- htmltools::tags$button(
id = inputId, type = "button", class = "action-button bttn", `data-val` = value,
class = paste0("bttn-", style),
class = paste0("bttn-", size),
class = paste0("bttn-", color), list(icon, label),
class = if (block) "bttn-block",
class = if (no_outline) "bttn-no-outline",
style = my_additional_style
)
shinyWidgets:::attachShinyWidgetsDep(tagBttn, "bttn")
}
After you make your custom button function, you can use it just like actionBttn inside your ui.
ui <- dashboardPage(
dashboardHeader(
title = "example"
),
dashboardSidebar(),
dashboardBody(
# for approach #3, but this is far away from the button in the code
htmltools::tags$head(
htmltools::tags$style('button#clear_ext_css { margin-top:25px }')
),
box(
width = 12,
column(
width = 2,
dateRangeInput(
"dateRange",
"Date Range",
start = "2017-01-01",
end = Sys.Date(),
min = "2001-01-01",
max = Sys.Date(),
format = "mm/dd/yy",
separator = " - "
)
),
column(
width = 1,
actionButton('Apply', 'Apply', style = 'margin-top:25px')
),
column(
width = 3,
# approach #1, just wrapping it in a styled div
div(
actionBttn(
inputId = 'clear_div',
label = "Clear with div",
style = "gradient",
color = "danger"
),
style = 'margin-top:25px'
)
),
column(
width = 3,
# approach #2, custom function from above
actionBttn_with_style(
inputId = 'clear_fn',
label = "Clear with custom function",
style = "gradient",
color = "danger",
my_additional_style = 'margin-top:25px'
)
),
column(
width = 3,
# approach #3, but you don't see any custom logic here
actionBttn(
inputId = 'clear_ext_css',
label = "Clear with external CSS",
style = "gradient",
color = "danger"
)
)
)
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)