ドロップダウンメニュー
shinyAppにドロップダウンメニューを追加する方法を見てきました。これにより、アプリの利用者は限られたスペースで、一覧から1つのオプションを選べます。アプリに機能が多く追加されている場合でも、UIをすっきり保てるよい設計になります。
この演習では、データはsleepとして、プロットはpとして保存されています。shinyとtidyverseライブラリも読み込まれています。コードを完成させたら、HTML ViewerでshinyAppを開いて、全体の表示を確認しましょう。
この演習はコースの一部です
shinydashboard で作るダッシュボード
演習の手順
- 2つの選択肢「Plot」と「Table」を持つ
selectInput()を配置します。 plotOutput()とtableOutput()を、それぞれ"plot"と"table"というIDで配置します。- serverにプロットの出力を追加します。
- serverにテーブルの出力を追加します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
ui <- fluidPage(
titlePanel("Sleeping habits in America"),
# Place a selectInput with two choices, "Plot" and "Table"
___("choice", "Choose an output",
choices = ___),
# Place plot and table outputs called "plot" and "table"
___("plot"), ___)
server <- function(input, output) {
# Add renderPlot
output$plot <- ___({
if(input$choice == "Plot") p
})
# Add renderTable
output$___ <- ___({
if(input$choice == "Table") sleep
}) }
shinyApp(ui, server)