Created
May 13, 2017 20:16
-
-
Save daattali/0afc37f0fafe5938cdecf47a9ec2f87b to your computer and use it in GitHub Desktop.
Change the width of the main panel depending on what tab is open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
ui <- fluidPage( | |
shinyjs::useShinyjs(), | |
# You have to include this one-liner CSS | |
tags$style(".fullwidth { width: 100% !important; }"), | |
# You need to wrap the sidebarLayout() by a div() and give it an ID | |
div(id = "SOMEID", | |
sidebarLayout( | |
sidebarPanel( | |
"side panel" | |
), | |
mainPanel( | |
tabsetPanel( | |
id = "maintabs", | |
tabPanel( | |
"tab1", | |
"Tab 1 show" | |
), | |
tabPanel( | |
"tab2", | |
"Tab 2 hide" | |
), | |
tabPanel( | |
"tab3", | |
"Tab 3 show" | |
), | |
tabPanel( | |
"tab4", | |
"Tab 4 hide" | |
) | |
) | |
) | |
) | |
) | |
) | |
server <- function(input, output) { | |
observe({ | |
if (input$maintabs %in% c("tab1", "tab3")) { | |
# In all these shinyjs functions, you need to use the same ID as you used in the div in the UI | |
shinyjs::show(select = "#SOMEID > div > div:nth-child(1)") | |
shinyjs::removeClass(class = "fullwidth", | |
select = "#SOMEID > div > div:nth-child(2)") | |
} else { | |
shinyjs::hide(select = "#SOMEID > div > div:nth-child(1)") | |
shinyjs::addClass(class = "fullwidth", | |
select = "#SOMEID > div > div:nth-child(2)") | |
} | |
}) | |
} | |
shinyApp(ui = ui, server = server) |
Awesome! I forgot I ever made this :)
How did you find this?!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this script exemple. It helps me a lot to manage the fullwidth option for the div class. 👍