[Shiny 30本ノック] 7. Tableを表示させる



uiから説明する

shinyUI(

    navbarPage(

        tabPanel(

            sidebarLayout(

                sidebarPanel(

                    selectInput()

            mainPanel(

                tabsetPanel(

                    tabPanel(

 ))))))))


ui.R

library(shiny)

library(shinythemes)


shinyUI(

  navbarPage("Shinyサンプルアプリケーション",

             tabPanel("可視化", sidebarLayout(

               sidebarPanel(

                 # 下記部分を追加

                 selectInput("selected_data_for_plot", label = h3("データセットを選択してください。"),

                             choices = c("アヤメのデータ" = "iris",

                                         "不妊症の比較データ" = "infert",

                                         "ボストン近郊の不動産価格データ" = "Boston",

                                         "スパムと正常メールのデータ" = "spam",

                                         "ニューヨークの大気状態データ" = "airquality",

                                         "タイタニックの乗客データ" = "titanic"),

                             selected = "iris")

               ),

               mainPanel(

                 tabsetPanel(type = "tabs",

                             tabPanel("Table",

                                      tableOutput("table_for_plot"))

                 )

               )

             ))))

             


server.R
library(shiny)
library(MASS)
library(kernlab)
data(spam)

shinyServer(function(input, output) {
  output$distPlot_shiny <- renderPlot({
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins_shiny + 1)
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })
  
  data_for_plot <- reactive({
    switch(input$selected_data_for_plot,
           "iris" = iris,
           "infert" = infert,
           "Boston" = Boston,
           "spam" = spam,
           "airquality" = airquality,
           "titanic" = data.frame(lapply(data.frame(Titanic), 
                                         function(i){rep(i, data.frame(Titanic)[, 5])}))
    )
  })
  
  output$table_for_plot <- renderTable({
    data_for_plot()
  })
})


アーカイブ

もっと見る