Skip to content Skip to sidebar Skip to footer

Add A Button In Full Screen Mode To Reset To Normal Mode In A Shiny App

In the app below when I press the actionButton() I get a full screen of the plot. Is it possible to have a button to return, when in full screen mode under the plot, instead of cli

Solution 1:

library(shiny)
library(ggplot2)

js <- "
function openFullscreen(elem, plotselector) {
  $('#exitbtn').show();
  $(plotselector).addClass('fullscreen');
  if (elem.requestFullscreen) {
    elem.requestFullscreen();
  } else if (elem.mozRequestFullScreen) { /* Firefox */
    elem.mozRequestFullScreen();
  } else if (elem.webkitRequestFullscreen) { /* Chrome, Safari and Opera */
    elem.webkitRequestFullscreen();
  } else if (elem.msRequestFullscreen) { /* IE/Edge */
    elem.msRequestFullscreen();
  }
}
function exitFullscreen() {
  $('#exitbtn').hide();
  if (document.exitFullscreen) {
    document.exitFullscreen();
  } else if (document.webkitExitFullscreen) { /* Safari */
    document.webkitExitFullscreen();
  } else if (document.msExitFullscreen) { /* IE11 */
    document.msExitFullscreen();
  }
}
"

css <- "
#exitbtn {
  display: none;
}
.fullscreen {
  height: 90% !important;
  margin: 0 !important;
}
"

ui <- fluidPage(
  tags$head(
    tags$script(HTML(js)),
    tags$style(HTML(css))
  ),
  br(),
  fluidRow(
    column(
      width = 3,
      actionButton(
        "fs", "Full screen", 
        onclick = "openFullscreen(document.getElementById('frame'), '#ggplot');"
      )
    ),
    column(
      width = 9,
      div(
        id = "frame", 
        plotOutput("ggplot"),
        actionButton(
          "exitbtn", "Exit",
          onclick = "exitFullscreen(); $('#ggplot').removeClass('fullscreen');"
        )
      )
    )
  )
)

server <- function(input, output, session){
  
  output[["ggplot"]] <- renderPlot({
    ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point()
  })
  
}


shinyApp(ui, server)

Post a Comment for "Add A Button In Full Screen Mode To Reset To Normal Mode In A Shiny App"