选择dataTable中的行以显示Shiny

时间:2017-08-16 04:03:24

标签: r dataframe ggplot2 shiny

所以我有这个数据框...

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)进行闪亮我正在renderDataTable df1允许您选择每一行,并且当每行被选中时,它会开始显示另一个数据表。我想要做的是,当我点击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)

1 个答案:

答案 0 :(得分: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,它将是Projectdf1中唯一变量的向量。然后在它的基础上我过滤了df2