2015-08-25 109 views
1

下面的數據集的表中給出(實際上更多的情況下):過濾器閃亮

data_test = data.frame(ID = c ("1","2","3","4","5"), 
       product = c("A","B","C","A","C"), 
       milieu = c("good","medium","bad","medium","bad"), 
       online = c(1,0,1,1,0), 
       ooh = c(0,1,0,1,1), 
       event = c(1,1,0,0,0)) 

現在我想建立一個閃亮的應用程序,其中一個人可以選擇一個環境可以說「好」和產品「A」和所有具有「1」的在線以及具有這些設置的數據表被返回。在實例ID 1

我嘗試以下

UI:

shinyUI(fluidPage(
    titlePanel("product milieu"), 

    sidebarLayout(
    sidebarPanel("select", 
       selectInput("select_milieu", 
          label = "Milieu", 
       choices = list("good", 
           "medium", 
           "bad") 
       ), 
       selectInput("select_product", 
          label = "Product", 
          choices = list("A", 
              "B", 
              "C") 
       ), 
       selectInput("select_online", 
          label = "Online", 
          choices = list(1, 
              0) 
       ), 
       selectInput("select_ooh", 
          label = "ooh", 
          choices = list(1, 
              0) 
       ), 
       selectInput("select_Event", 
          label = "Event", 
          choices = list(1, 
              0) 

       ) 
    ), 
    mainPanel("My table", 
       textOutput("output_milieu"), 
       textOutput("output_product"), 
       textOutput("output_event"), 
       textOutput("output_online"), 
       textOutput("output_ooh"), 
       tableOutput("gapminder_table") 
      ) 
) 
)) 

服務器:

shinyServer(function(input, output) { 

    output$gapminder_table <- renderTable({ 
    subset(data_test, 
      milieu == input$select_milieu & product == input$select_product & 
      online == input$select_online) 

    }) 
    output$output_milieu <- renderText({ 
    paste("milieu", input$select_milieu) 
    }) 
    output$output_product <- renderText({ 
    paste("product", input$select_product) 
    }) 
    output$output_event <- renderText({ 
    paste("Event", input$select_Event) 
    }) 
    output$output_online <- renderText({ 
    paste("Online", input$select_Online) 
    }) 
    output$output_ooh <- renderText({ 
    paste("out of Home", input$select_ooh) 
    }) 

}) 

我現在的問題是如何爲 「事件」 篩選和「嚯」。有人有建議嗎?

在此先感謝!

+1

爲什麼不直接做在同一個'renderTable()'步過濾的? – ulfelder

+0

另外,是否有一個原因,你沒有顯示你正在使用renderTable()步驟創建的表?你的意思是在你的UI中有一個'tableOutput(「gapminder_table」)行? – ulfelder

+0

對不起,我在複製/粘貼過程中忘記了這個... 你會如何在renderTable()中進行過濾? – burton030

回答

5

如果您開始探索使用閃亮數據表的DT軟件包,則可以更簡單。有了這個,你可以在相應的列上面輸入你喜歡的任何過濾標準。

server.R

library(shiny) 
library(DT) 

data_test = data.frame(ID = c ("1","2","3","4","5"), 
         product = c("A","B","C","A","C"), 
         milieu = c("good","medium","bad","medium","bad"), 
         online = c(1,0,1,1,0), 
         ooh = c(0,1,0,1,1), 
         event = c(1,1,0,0,0)) 

shinyServer(function(input, output) { 

    output$gapminder_table <- renderDataTable({ 
     data_test   
    }, 
    filter = 'top', 
    rownames = FALSE)  
}) 

ui.R

library(shiny) 
library(DT) 

shinyUI(fluidPage(
    titlePanel("product milieu"), 

    sidebarLayout(
     sidebarPanel("Place for other criteria" 
     ), 
     mainPanel("My table", 
        dataTableOutput("gapminder_table") 
     ) 
    ) 
)) 
+0

這很簡單! – burton030