My aim is to build menu which content is dynamically generated and user sees only menuItems/menuSubItems starting from the top to the selected one ("next" button will be used to reach next not yet displayed menuItems/menuSubItems; in case of selection of any available menuItems/menuSubItems menu should be regenerated so the last available menuItems/menuSubItems is selected one). So I will have to play with selected and startExpanded arguments, but this is not the part of this question.
The issue I faced is mutual connection of renderMenu and observeEvent (that checks selected menuItem/menuSubItem). Here is the code:
library(shiny)
library(shinydashboard)
menu_generator <- function(selected = NULL, expanded = NULL){
print("menu_generator")
output <- sidebarMenu(
menuItem("Charts1", icon = icon("bar-chart-o"),
menuSubItem("AAdashboard", tabName = "AAdashboard"),
menuSubItem("BBdashboard", tabName = "BBdashboard")
),
menuItem("Charts2", icon = icon("bar-chart-o"),
menuSubItem("DDdashboard", tabName = "DDdashboard"),
menuSubItem("EWidgets", tabName = "EWidgets")
)
)
return(output)
}
ui <- dashboardPage(
dashboardHeader(title = "Test"),
dashboardSidebar(sidebarMenu(
sidebarMenuOutput("menu_output")
)),
dashboardBody(
tabItems(
tabItem(tabName = "AAdashboard",
h2("ADashboard tab content")
),
tabItem(tabName = "BBdashboard",
h2("BWidgets tab content")
),
tabItem(tabName = "DDdashboard",
h2("DWidgets tab content")
),
tabItem(tabName = "EWidgets",
h2("EWidgets tab content")
)
))
)
server <- function(input, output) {
output$menu_output <- renderMenu({
print("output$menu_output")
sidebarMenu(menu_generator(),
id = "my_menu")
})
observeEvent(input$my_menu, {
print("observer")
print(input$my_menu)
print(input$sidebarItemExpanded)
output$menu_output <- renderMenu({
sidebarMenu(menu_generator(),
id = "my_menu")
})
})
}
shinyApp(ui, server)
Scenario: expand Charts2 then click "DDdashboard".
In the console:
[1] "observer"
[1] "DDdashboard"
[1] "Charts2"
[1] "menu_generator"
[1] "observer"
[1] "AAdashboard"
NULL
[1] "menu_generator"
"observer" is called twice (as consequence "menu_generator" also), so this creates unexpected behaviour. My understanding is that reason of that is nature of renderMenu. The question is - how to prevent Shiny from calling "observer" in this case twice? Also notice that in the second call input$my_menu = "AAdashboard".
observeEvent(input$sidebarItemExpanded == 'DDdashboard',{
print("DDdashboard_selected")
})
Related
My goal is to create a dashboard that has in one of the tabs a table based on date inputs from the user. I want this to be specifically in the tab called Overview in the dashboard.
I have 3 r scripts, one called data cleaning thats basically a table loaded into a data frame called trans. The other 2 are called ui and server. They are the following:
ui.R
library(shiny)
library(shinydashboard)
library(data.table)#For fread.
library(tidyverse)
library(DT)#For the interactive table.
# Header -----------------------------------------------------------------------|
header<-dashboardHeader( title = "Marketing Dashboard"
)
# Sidebar ----------------------------------------------------------------------|
sidebar<-dashboardSidebar(
sidebarMenu(
menuItem("Overview", tabname ="overview", icon = icon("dashboard")),
menuItem("Weather", tabname ="weather", icon = icon("bolt"))
)
)
# Body -------------------------------------------------------------------------|
body<-dashboardBody(
tabItems(
tabItem(tabName = 'Overview',
fluidRow(
dateRangeInput("date",
label = 'Date range input',
start = Sys.Date() - 7, end = Sys.Date()
),
dataTableOutput("overviewtable")
)
),
tabItem(tabName = 'weather',
fluidRow(
)
)
)
)
# UI ---------------------------------------------------------------------------|
ui = dashboardPage(
header,
sidebar,
body
)
shinyApp(ui,server)
server.R
server <- function(input,output){
#Reactive for dates in overview
overviewdata<- reactive({
trans %>% filter(ymd_hms(start_time) >= input$date[1] & ymd_hms(end_time)<= inpute$date[2])
})
#Table for overview
output$overviewtable<- renderDataTable(
datatable({
overviewdata
})
)
}
My problem is when I run the app I see only a blank dashboard with two tabs. One for Overview and one for Weather. I don't see any table or any place where it gives me the option to input the dates. This is my first time working with shiny and I am trying to learn on the go. It is also my first time working with multiple r scripts.
I have looked at other examples online from r gallery and from stack overflow but I am not sure what I am doing wrong.
You should place the dateRangeInput inside the menuItem if you want to see it inside the tab. Here's what it would look like under the "Overview" tab.
library(shiny)
library(shinydashboard)
library(data.table)#For fread.
library(tidyverse)
library(DT)#For the interactive table.
# Header -----------------------------------------------------------------------|
header<-dashboardHeader( title = "Marketing Dashboard"
)
# Sidebar ----------------------------------------------------------------------|
sidebar<-dashboardSidebar(
sidebarMenu(
menuItem("Overview", tabname ="overview", icon = icon("dashboard"),
dateRangeInput("date",
label = 'Date range input',
start = Sys.Date() - 7, end = Sys.Date()
)),
menuItem("Weather", tabname ="weather", icon = icon("bolt"))
)
)
# Body -------------------------------------------------------------------------|
body<-dashboardBody(
tabItems(
tabItem(tabName = 'Overview',
fluidRow(
dataTableOutput("overviewtable")
)
),
tabItem(tabName = 'weather',
fluidRow(
)
)
)
)
# UI ---------------------------------------------------------------------------|
ui = dashboardPage(
header,
sidebar,
body
)
shinyApp(ui,server)
I want to create a function/a module to update tabset items.
I'm aware of this and this question, but my issue relates to how the button input is handled in updateTabItems.
Here you can find an example:
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
mod_updateTabset <- function(
input, output, session, triggerId, dashboardId, tab, parent
) {
observeEvent(triggerId, {
updateTabItems(parent, dashboardId, selected = tab)
})
}
ui <- dashboardPagePlus(
header = dashboardHeaderPlus(),
sidebar = dashboardSidebar(
sidebarMenu(
id = 'Tabs',
menuItem("Tab 01", tabName = "tab01", icon = icon("dice-one")),
menuItem("Tab 02", tabName = "tab02", icon = icon("dice-two"))
)
),
body = dashboardBody(
tabItems(
tabItem(
tabName = "tab01",
actionButton("updateButton", label = "To Tab02")
),
tabItem(
tabName = "tab02",
h4("New Tab")
)
)
)
)
server <- function(input, output, session) {
callModule(
mod_updateTabset,
"updateLink",
triggerId = input$updateButton,
dashboardId = "Tabs",
tab = "tab02",
parent = session
)
}
shinyApp(ui = ui, server = server)
I know it's working when creating mod_updateTabset_UI, shifting the actionButton to the module. That's why I suppose the issue lies in the button handling.
Still, I'd like to have a function/module which can handle all kind of links, buttons etc. to update Tab items, not just one button
For anyone who runs into the same problem: You need to use reactive() for triggerId when calling the module
callModule(
mod_updateTabset,
"updateLink",
triggerId = reactive(input$updateButton),
dashboardId = "Tabs",
tab = "tab02",
parent = session
)
The module then needs to handle a reactive value:
mod_updateTabset <- function(
input, output, session, triggerId, dashboardId, tab, parent
) {
observeEvent(triggerId(), {
updateTabItems(parent, dashboardId, selected = tab)
})
}
Background
Within a modular1 Shiny application, I would like to load module only when menu item on shinydashboard is clicked. If the menu item is not accessed I wouldn't like to load the module.
Basic application
app.R
# Libs
library(shiny)
library(shinydashboard)
# Source module
source("sample_module.R")
ui <- dashboardPage(
dashboardHeader(title = "Dynamic sidebar"),
dashboardSidebar(sidebarMenuOutput("menu")),
dashboardBody(tabItems(
tabItem(tabName = "tab_one", h1("Tab One")),
tabItem(tabName = "tab_two", sampleModuleUI("sampleModule"))
))
)
server <- function(input, output) {
callModule(sampleModuleServer, "sampleModule")
output$menu <- renderMenu({
sidebarMenu(
menuItem(
"Menu item 1",
icon = icon("calendar"),
tabName = "tab_one"
),
menuItem(
"Menu item 2",
icon = icon("globe"),
tabName = "tab_two"
)
)
})
}
shinyApp(ui, server)
sample_module.R
sampleModuleServer <- function(input, output, session) {
output$plot1 <- renderPlot({
plot(mtcars)
})
}
sampleModuleUI <- function(id) {
ns <- NS(id)
plotOutput(ns("plot1"))
}
Desired implementation
The desired implementation would load sample_module only when the relevant menu item is clicked. On the lines of 2:
Don't call callModule from inside observeEvent; keep it at the top level. Take the reactive expression that's returned, and use eventReactive to wrap it in the button click. And use the eventReactive from your outputs, etc.
x <- callModule(...)
y <- eventReactive(input$go, x())
output$tbl <- DT::renderDataTable(y())
Attempt
app.R (modified)
# Libs
library(shiny)
library(shinydashboard)
# Source module
source("sample_module.R")
ui <- dashboardPage(
dashboardHeader(title = "Dynamic sidebar"),
dashboardSidebar(sidebarMenuOutput("menu")),
dashboardBody(tabItems(
tabItem(tabName = "tab_one", h1("Tab One")),
tabItem(tabName = "tab_two", sampleModuleUI("sampleModule"))
))
)
server <- function(input, output) {
eventReactive(eventExpr = input$tab_two,
valueExpr = callModule(sampleModuleServer, "sampleModule")
)
output$menu <- renderMenu({
sidebarMenu(
menuItem(
"Menu item 1",
icon = icon("calendar"),
tabName = "tab_one"
),
menuItem(
"Menu item 2",
icon = icon("globe"),
tabName = "tab_two"
)
)
})
}
shinyApp(ui, server)
Problem
Application runs but the module does not load. Questions:
How to correctly call eventReactive on dashboard menu item? The tab_item does not seem to have id parameter is tabName equivalent in that context?
The linked discussion refers to refreshing one table. I'm trying to figure out example that will work with modules containing numerous interface element and elaborate server calls.
Clicking on Menu item 2 should display the content from the sample_module.R file.
1 Modularizing Shiny app code
2 Google groups: activate module with actionButton
Update
I've tried explicitly forcing module into application environment load using the following syntax:
eventReactive(eventExpr = input$tab_two,
valueExpr = callModule(sampleModuleServer, "sampleModule"),
domain = MainAppDomain
)
where
MainAppDomain <- getDefaultReactiveDomain()
Edit: Dropping Joe Cheng's top level statement:
# Libs
library(shiny)
library(shinydashboard)
# Source module
source("sample_module.R")
ui <- dashboardPage(
dashboardHeader(title = "Dynamic sidebar"),
dashboardSidebar(sidebarMenuOutput("menu")),
dashboardBody(tabItems(
tabItem(tabName = "tab_one", h1("Tab One")),
tabItem(tabName = "tab_two", sampleModuleUI("sampleModule"))
))
)
server <- function(input, output) {
observeEvent(input$tabs,{
if(input$tabs=="tab_two"){
callModule(sampleModuleServer, "sampleModule")
}
}, ignoreNULL = TRUE, ignoreInit = TRUE)
output$menu <- renderMenu({
sidebarMenu(id = "tabs",
menuItem(
"Menu item 1",
icon = icon("calendar"),
tabName = "tab_one"
),
menuItem(
"Menu item 2",
icon = icon("globe"),
tabName = "tab_two"
)
)
})
}
shinyApp(ui, server)
Furthermore, your sidebarMenu needs an id to access the selected tabs; please see the shinydashboard documentation.
I am having a following problem:
I want a sidebar change when I switch between tabItem(s).
dashboardPage(
dasboardHeader(title = ""),
dashboardSidebar(
sidebarMenu(
menuItem("1", tabName = "1"),
menuItem("2", tabName = "2")
),
#I want this to be displayed when menuItem "1" is clicked
tabsetPanel(
tabPanel("t1", val="t1",
.... some inputs),
tabPanel("t2", val="t2",
.... some inputs)
),
# This to be displayed when menuItem "2" is clicked
selectInput("s1", label="Select"....),
selectInput("s2", label="Select2"...)
)
dashboardBody(
tabItem(tabName="1",
.......
),
tabItem(tabName="2",
........
)
)
)
I have the dashboardBody changing when switching between the tabs but don't know how to change the dashboardSidebar values. Tried this inside the dashboardSidebar:
conditionalPanel(
condition="tabName='1'",
#displaying first version of DashboardSidebar
),
conditionalPanel(
condition="tabName='2'",
#displaying second version of DashboardSidebar
)
But it didn't work for me.
Any ideas?
Thank you for your help.
First of all, you have to add sidebarMenu an ID which will be used for a Shiny input value, and it will report which tab is selected.
After that, add uiOutput to dashboardSidebar function.
uiOutput is going to receive, depending on a selected tab, either tabsetPanel or two selectInputs.
Finally, within renderUI, which you define on the server side, you just need to create conditional statements. That's the easy part.
The tricky part is that you have to wrap selectInputs into a list - otherwise only the second widget would be sent to the UI. Even more tricky part is that you have to specify the ID of tabsetPanel when you want to send it to the UI via renderUI. (That's very tricky because normally you don't have to specify its ID!)
Full example:
library(shiny)
library(shinydashboard)
rm(ui)
rm(server)
ui <- dashboardPage(
dashboardHeader(title = ""),
dashboardSidebar(
# added ID which will be used for a Shiny input value,
# and it will report which tab is selected.
sidebarMenu(id = "tab",
menuItem("1", tabName = "1"),
menuItem("2", tabName = "2")
),
uiOutput("out1")
),
dashboardBody(
tabItem(tabName = "1"),
tabItem(tabName = "2")
)
)
server <- function(input, output) {
output$out1 <- renderUI({
if (input$tab == "1") {
dyn_ui <- tabsetPanel(id = "tabset_id", selected = "t1",
tabPanel("t1", value = "t1"),
tabPanel("t2", value = "t2"))
}
if (input$tab == "2") {
dyn_ui <- list(selectInput("s1", label = "Select", choices = letters[1:3]),
selectInput("s2", label = "Select2", choices = letters[4:6]))
}
return(dyn_ui)
})
}
shinyApp(ui, server)
I have initial loading of data from the DB in the server.R which takes a few seconds. Until this is done, the page displayed is distorted (wrong data in selection box, and weird placing of the boxes, see below).
I want to display a different page (or at least different content in my first-displayed tab) until the data is completely loaded.
I thought about doing some kind of conditionalPanel using a condition based on a dedicated global variable (initial_loading_done), but wherever I tried placing the conditionalPanel it didn't work.
This is the structure of my UI.R:
shinyUI(
dashboardPage(
dashboardHeader(title = "Title"),
dashboardSidebar(
sidebarMenu(
menuItem("Tab1", tabName = "Tab1",icon = icon("dashboard")),
menuItem("Tab2", tabName = "Tab2", icon = icon("bar-chart-o"))
)
),
dashboardBody(
includeCSS("custom_css.css"),
tabItems(
tabItem(tabName = "Tab1",
fluidRow(<content>),
mainPanel(
fluidRow(<content>)
)
),
tabItem(tabName = "Tab2",
fluidRow(<content>),
mainPanel(
dataTableOutput('my_data_table')
)
)
)
)
)
)
Here's a very simple example using shinyjs package
The idea is to create the loading "page" and the content "page" under different IDs, have the content page initially hidden, and use show() and hide() after the app is ready
library(shiny)
library(shinyjs)
load_data <- function() {
Sys.sleep(2)
hide("loading_page")
show("main_content")
}
ui <- fluidPage(
useShinyjs(),
div(
id = "loading_page",
h1("Loading...")
),
hidden(
div(
id = "main_content",
"Data loaded, content goes here"
)
)
)
server <- function(input, output, session) {
load_data()
}
shinyApp(ui = ui, server = server)
In server I like to use reactiveValues() to store a setupComplete condition. Then, when the data is loaded my setupComplete is set to TRUE.
In the ui we can then assess this setupComplete condition in a conditionalPanel, and only display the content (in my example the three box() widgets).
Here's a working example
## app.R ##
library(shiny)
library(shinydashboard)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
actionButton(inputId = "btn_data", label = "Download"),
conditionalPanel(condition = "output.setupComplete",
box( title = "box1" ),
box( title = "box2" ),
box( title = "boc3" )
),
conditionalPanel(condition = "!output.setupComplete",
box( title = "loading"))
)
)
server <- function(input, output) {
rv <- reactiveValues()
rv$setupComplete <- FALSE
## simulate data load
observe({
if(input$btn_data){
df <- data.frame(id = seq(1,200),
val = rnorm(200, 0, 1))
## Simulate the data load
Sys.sleep(5)
## set my condition to TRUE
rv$setupComplete <- TRUE
}
## the conditional panel reads this output
output$setupComplete <- reactive({
return(rv$setupComplete)
})
outputOptions(output, 'setupComplete', suspendWhenHidden=FALSE)
})
}
shinyApp(ui, server)
The code
hidden(
div(
id = "main_content",
"Data loaded, content goes here"
)
doesn't work with tabsetPanel. But if you move the id to the div level it works beautifully. Thanks to shinyjs author Dean Attali for this tip. https://stackoverflow.com/users/4432127/keshete
hidden(
div(id = "mainTabsetPanel",
tabsetPanel(
....