2017-08-16 164 views
1

所以我有這樣的數據幀...DataTable中選擇行

df1 <- read.table(text=" 
    Project  Counts  Inventory 
     A   25   100 
     B   20   120 
     C   10   50 
",header=TRUE,stringsAsFactors = FALSE) 

這個數據幀....

df2 <- read.table(text=" 
    Project  Sub_Project  Counts  Date 
     A   1    5  2017-05-01 
     A   2    10  2017-05-01 
     A   2    10  2017-06-01 
     B   1    40  2017-05-01 
     B   1    20  2017-06-01 
     B   1    20  2017-07-01 
     B   2    40  2017-05-01 
     C   1    15  2017-05-01 
     C   1    35  2017-06-01 
",header=TRUE,stringsAsFactors = FALSE) 

在晴朗的使用library(DT)library(ggplot2)閃亮的表現從另一個表中的數據我在製作一個renderDataTabledf1,它允許你選擇每一行,並且當每一行被選中時,它開始顯示另一個數據表。我想要做的是,當我點擊df1中的一行時,所呈現的新數據表將給我一個df2的子集,僅顯示所選項目的信息,總結計數並仍然按Sub_Project組織它們,像這樣(如果你點擊的df1項目A:

df3 <-read.table(text=" 
    Project  Sub_Project  Counts 
     A   1    5 
     A   2    20 
",header=TRUE,stringsAsFactors = FALSE) 

的什麼,我試圖做作品的實際功能,我只是不知道如何編程邏輯我想要什麼?這裏是我的整個應用程序

library(shiny) 
library(shinydashboard) 
library(ggplot2) 
library(DT) 

df1 <- read.table(text=" 
      Project  Counts  Inventory 
       A   25   100 
       B   20   120 
       C   10   50 
       ",header=TRUE,stringsAsFactors = FALSE) 


df2 <- read.table(text=" 
      Project  Sub_Project  Counts  Date 
       A   1    5  2017-05-01 
       A   2    10  2017-05-01 
       A   2    10  2017-06-01 
       B   1    40  2017-05-01 
       B   1    20  2017-06-01 
       B   1    20  2017-07-01 
       B   2    40  2017-05-01 
       C   1    15  2017-05-01 
       C   1    35  2017-06-01 
       ",header=TRUE,stringsAsFactors = FALSE) 



ui <- dashboardPage(
    dashboardHeader(title = 'Dashboard'), 
    dashboardSidebar(), 

    dashboardBody(

tabsetPanel(
    tabPanel('Sequencing', 
      fluidRow(
      column(12, 
        dataTableOutput('project_table'), 
        dataTableOutput('subproject_table')) 
      ) 
     ) 
) 
) 
) 



server <- function(input, output) { 

    output$project_table <- renderDataTable(df1, options = list(pageLength = 10)) 


    output$subproject_table <- renderDataTable({ 
    s = input$project_table_rows_selected 
     if(length(s)) df1[s, , drop=FALSE]}) 
} 

shinyApp(ui, server) 
+0

在'df3'中,'counts' 20似乎來自'df2'上的'Project'爲'B'的那一行 – akrun

+0

@akrun它沒有做什麼是總結項目中相同Sub_Project的計數答:所以它忽略了日期信息,並保留由項目組織的'df1',然後通過類似的Sub_Projects彙總計數... – Naji

回答

1

我認爲這就是你在找什麼:

library(shiny) 
library(shinydashboard) 
library(ggplot2) 
library(DT) 

df1 <- read.table(text=" 
        Project  Counts  Inventory 
        A   25   100 
        B   20   120 
        C   10   50 
        ",header=TRUE,stringsAsFactors = FALSE) 


df2 <- read.table(text=" 
        Project  Sub_Project  Counts  Date 
        A   1    5  2017-05-01 
        A   2    10  2017-05-01 
        A   2    10  2017-06-01 
        B   1    40  2017-05-01 
        B   1    20  2017-06-01 
        B   1    20  2017-07-01 
        B   2    40  2017-05-01 
        C   1    15  2017-05-01 
        C   1    35  2017-06-01 
        ",header=TRUE,stringsAsFactors = FALSE) 



ui <- dashboardPage(
    dashboardHeader(title = 'Dashboard'), 
    dashboardSidebar(), 

    dashboardBody(

    tabsetPanel(
     tabPanel('Sequencing', 
       fluidRow(
       column(12, 
         dataTableOutput('project_table'), 
         dataTableOutput('subproject_table')) 
       ) 
    ) 
    ) 
) 
) 



server <- function(input, output) { 

    output$project_table <- renderDataTable(df1, options = list(pageLength = 10)) 


    output$subproject_table <- renderDataTable({ 
    s = input$project_table_rows_selected 
    project <- unique(df1[s,c("Project")]) 
    df2[df2$Project %in% project, ]}) 
} 

shinyApp(ui, server) 

我已創建新的對象project,這將是在Project柱唯一變量的向量從df1。然後在它的基礎上,我過濾了df2

+0

真棒!謝謝 – Naji