【问题标题】:Add a button in full screen mode to reset to normal mode in a shiny app在全屏模式下添加一个按钮以在闪亮的应用程序中重置为正常模式
【发布时间】:2021-07-18 04:30:40
【问题描述】:

在下面的应用程序中,当我按下actionButton() 时,我会看到全屏的情节。在剧情下处于全屏模式时,是否可以有一个返回按钮,而不是在计算机上单击“Esc”?

library(shiny)
library(ggplot2)

js <- "
function openFullscreen(elem) {
  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();
  }
}"

css <- "
#ggplot:-webkit-full-screen {
  height: 100%;
  margin: 0;
}
#ggplot:-ms-fullscreen {
  height: 100%;
}
#ggplot:fullscreen {
  height: 100%;
}"

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('ggplot'));"
      )
    ),
    column(
      width = 9,
      plotOutput("ggplot")
    )
  )
)

server <- function(input, output, session){

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

}


shinyApp(ui, server)

【问题讨论】:

    标签: javascript r shiny


    【解决方案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)
    

    【讨论】:

    • 感谢方向正确,但按钮只能在全屏模式下显示吗?
    • @firmo23 对我来说,它只以全屏模式显示。
    • 你可以在 Rstudio 云环境或浏览器模式下查看它吗?
    • @firmo23 啊不,你是对的。试试onclick = "exitFullscreen(); $('#ggplot').removeClass('fullscreen'); $(this).hide();"
    猜你喜欢
    • 2021-07-18
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2019-02-02
    • 2021-11-19
    • 2014-08-07
    相关资源
    最近更新 更多