• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: R shiny custom tabsets using javascript – second tab not rendering

Resolved: R shiny custom tabsets using javascript – second tab not rendering

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I am trying to build my own tabsets functionality in shiny using plain javascript. This is partly an exercise for me to learn javascript and the inner workings of shiny as well as to create functions which give the user greater flexibility when it comes to styling of tabsets.R
So far I have come up with the following (based on this answer but adapted to shiny etc), which works in jsfiddle (see https://jsfiddle.net/qavoeyju/) but the shiny example does not work.
Shiny does not recognize that the second tab is activated and therefore does not trigger the reactive elements and no plot is created.
How can I tell shiny to react to the second page as well, or how can I make these custom tabs in such a way that shiny works with them?

js <- shiny::HTML('
‘)
css <- shiny::HTML( '

‘
)

ui <- div( tags$ul( tags$li(class = "tab", id = "tab1", "data-value" = "Tab 1 CAP", "Tab 1 CAP"), tags$li(class = "tab", id = "tab2", "data-value" = "Tab 2 CAP", "Tab 2 CAP") ), div(class="tab-content", id="tab1-content", "data-value" = "Tab 1 CAP", p("1 first tab"), plotOutput("plot1") ), div(class="tab-content", id="tab2-content", "data-value" = "Tab 2 CAP", p("2 second tab"), plotOutput("plot2")), css, js ) server <- function(input, output, session) { output$plot1 <- renderPlot({ plot(1:10, rnorm(10)) }) output$plot2 <- renderPlot({ plot(1:100, rnorm(100)) }) } shinyApp(ui, server) [/code]

Tab 1 – Good enter image description here
Tab 2 – Bad (plot not rendered) enter image description here

Answer:

Trigger the shown event on your tab content.

library(shiny)
js <- shiny::HTML('
‘)
css <- shiny::HTML( '

‘
)

ui <- div( tags$ul( tags$li(class = "tab", id = "tab1", "data-value" = "Tab 1 CAP", "Tab 1 CAP"), tags$li(class = "tab", id = "tab2", "data-value" = "Tab 2 CAP", "Tab 2 CAP") ), div(class="tab-content", id="tab1-content", "data-value" = "Tab 1 CAP", p("1 first tab"), plotOutput("plot1") ), div(class="tab-content", id="tab2-content", "data-value" = "Tab 2 CAP", p("2 second tab"), plotOutput("plot2")), css, js ) server <- function(input, output, session) { output$plot1 <- renderPlot({ plot(1:10, rnorm(10)) }) output$plot2 <- renderPlot({ plot(1:100, rnorm(100)) }) } shinyApp(ui, server) [/code]

Shiny plotting is bound to the shown event.

If you have better answer, please add a comment about this, thank you!

javascript r shiny
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: I am facing ERR_HTTP2_PROTOCOL_ERROR on my website

27/03/2023

Resolved: TypeScript does not recognize properties when instantiating interface array

27/03/2023

Resolved: How to make statement case insensitive when uploading a dta file with specified columns?

27/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.