0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ShinyBSで折りたたみパネルUIを作る&パネルのヘッダーのクリック範囲をヘッダー全体に広げる

0
Last updated at Posted at 2022-01-20

折りたたみパネルUIのためのパッケージshinyBS

RShinyアプリケーションを作る際に便利なパッケージとして、
shinyBSパッケージがあります。
https://ebailey78.github.io/shinyBS/

その中で特に便利なのが、bsCollapse関数です。
https://ebailey78.github.io/shinyBS/docs/Collapses.html
これを利用すると、↓のような折りたたみ式のパネルUIを簡単に作ることができます。

最初は手順1が開いている。手順2と3は折りたたまれている

↓(手順2のヘッダーをクリックすると、手順2が開いて、手順1は折りたたまれる

このように、余計なUIを見せずに手順を進められるので、
ユーザーが混乱しにくいと思われます。

まあShinydashboardを使えば同じことは簡単にできるのですが、
使わなくても折りたたみパネルが作れるよということで。

上記公式サイトにサンプルがあるので、転記しておきます。(動作確認ずみ)

library(shiny)
library(shinyBS)

shinyApp(
  ui =
    fluidPage(
      sidebarLayout(
        sidebarPanel(HTML("This button will open Panel 1 using updateCollapse."),
                     actionButton("p1Button", "Push Me!"),
                     selectInput("styleSelect", "Select style for Panel 1",
                                 c("default", "primary", "danger", "warning", "info", "success"))
        ),
        mainPanel(
          bsCollapse(id = "collapseExample", open = "Panel 2",
                     bsCollapsePanel("Panel 1", "This is a panel with just text ",
                                     "and has the default style. You can change the style in ",
                                     "the sidebar.", style = "info"),
                     bsCollapsePanel("Panel 2", "This panel has a generic plot. ",
                                     "and a 'success' style.", plotOutput("genericPlot"), style = "success")
          )
        )
      )
    ),
  server =
    function(input, output, session) {
      output$genericPlot <- renderPlot(plot(rnorm(100)))
      observeEvent(input$p1Button, ({
        updateCollapse(session, "collapseExample", open = "Panel 1")
      }))
      observeEvent(input$styleSelect, ({
        updateCollapse(session, "collapseExample", style = list("Panel 1" = input$styleSelect))
      }))
    }
)

ヘッダーのクリック範囲をヘッダー全体に広げる

便利なbsCollapseですが、1つだけ欠点があります。
それは、デフォルト設定だとパネルを広げるときにクリックするヘッダーが
「文字のみ」になっているという点です。
つまり下図↓の②のあたりをクリックしても反応せず、①をクリックしなければなりません。
これはちょっと不便です。
image.png

対策を調べてみると、パッケージのgithubで解決策が出ていました。
https://github.com/ebailey78/shinyBS/issues/122
以下のCSSをcuntom.cssに追加するか、Rのコード内に直接記述すればOKとのことです。

.panel.title>a {
  display: block;
  padding: 10px 15px;
  margin: -10px -15px;
}

自分の場合はcssを別ファイルにするのが面倒なので、
shinyjsパッケージを使ってコード内で記述しました。以下、改良したサンプルコードです。

library(shiny)
library(shinyBS)
library(shinyjs)

shinyApp(
  ui =
    fluidPage(
      useShinyjs()
      ,inlineCSS(list(
        ".panel-title>a"="
          display: block;
          padding: 10px 15px;
          margin: -10px -15px;
        "
      ))
      
      ,sidebarLayout(
        sidebarPanel(HTML("This button will open Panel 1 using updateCollapse."),
                     actionButton("p1Button", "Push Me!"),
                     selectInput("styleSelect", "Select style for Panel 1",
                                 c("default", "primary", "danger", "warning", "info", "success"))
        ),
        mainPanel(
          bsCollapse(id = "collapseExample", open = "Panel 2",
                     bsCollapsePanel("Panel 1", "This is a panel with just text ",
                                     "and has the default style. You can change the style in ",
                                     "the sidebar.", style = "info"),
                     bsCollapsePanel("Panel 2", "This panel has a generic plot. ",
                                     "and a 'success' style.", plotOutput("genericPlot"), style = "success")
          )
        )
      )
    ),
  server =
    function(input, output, session) {
      output$genericPlot <- renderPlot(plot(rnorm(100)))
      observeEvent(input$p1Button, ({
        updateCollapse(session, "collapseExample", open = "Panel 1")
      }))
      observeEvent(input$styleSelect, ({
        updateCollapse(session, "collapseExample", style = list("Panel 1" = input$styleSelect))
      }))
    }
)

以上、Shinyで折りたたみパネルUIを作る方法でした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?