当应用在闪亮的应用程序中时,按列名对数据框进行子集设置不会响应

时间:2018-11-27 18:22:09

标签: r shiny

我有一个简单的闪亮应用程序,其中我通过行选择和列名选择来子集数据框。在按行进行子集化之后,我尝试了3种不同的方法对列进行子集化,但数据框保持不变。更具体地说,我使用过:

keeps <- c("Database","Organism",input$sta)
    #a[, (colnames(a) %in% input$sta)]
    #a[keeps]
    a[ , which(names(a) %in% keeps)]

但似乎没有任何作用

#data
Database<-c("Composite","DB","TC","RH","DGI","DCH","DCH","DCH","LDP")
Organism<-c("Human","Human","Human","Human","Human","Human","Mouse","Rat","Human")
Unique_Drugs<-c(12672,5130,1425,3090,6100,2019,250,736,1182)
Unique_Targets<-c(3987,2175,842,2308,2413,1441,198,327,702)
Mean_S.D.Targets_per_Drug<-c("5.87 ± 6.72","2.60 ± 6.87","2.28 ± 3.76","3.29 ± 5.03","3.60 ± 5.21","6.28 ± 14.29"
                             ,"1.92 ± 1.83"
                             ,"4.11 ± 5.32"
                             ,"4.27 ± 8.25"
)
Mean_S.D.Drugs_per_Target<-c("11.63 ± 15.59",
                             "12.52 ± 23.93",
                             "10.71 ± 8.37",
                             "12.98 ± 17.57",
                             "23.44 ± 25.65",
                             "13.87 ± 34.23",
                             "8.20 ± 18.44",
                             "14.82 ± 9.36",
                             "17.43 ± 9.34"
)
Unique_Drug_Target_Associations<-c(
45276,
14598,
3599,
12439,
23048,
13872,
594,
2876,
3915)

db<-data.frame(Database,Organism,Unique_Drugs,Unique_Targets,Mean_S.D.Drugs_per_Target,Mean_S.D.Targets_per_Drug, Unique_Drug_Target_Associations)


#ui.r
library(shiny)
library(shinydashboard)
library(DT)
ui <- dashboardPage(
  dashboardHeader(
    title = "Stats Table"
  ),
  dashboardSidebar(
    uiOutput("dbase"),
    tags$hr(),
    uiOutput("stats")



  ),
  dashboardBody(
    DTOutput('tbl')
  )
)
#server.r
library(shiny)
library(shinydashboard)
library(DT)
server <- function(input, output,session) { 
  output$dbase<-renderUI({
    checkboxGroupInput("base", label = "Specify dataset(s)",
                 choices = list("Composite","DB","TC","RH","DGI","DCH","LDP"),
                 selected = c("Composite","DB","TC","RH","DGI","DCH","LDP")
    )
  })
  output$stats<-renderUI({
    checkboxGroupInput("sta", label = "Specify statistic(s)",
                       choices = list("# Unique Drugs"="Unique_Drugs",
                                      "# Unique Targets"="Unique_Targets",
                                      "# Unique Drug-Target Associations"="Unique_Drug_Target_Associations",
                                      "# of Targets per Drug"="Mean_S.D.Targets_per_Drug",
                                      "# of Drugs per Target"="Mean_S.D.Drugs_per_Target"
                       ),
                       selected = c("Unique_Drugs","Unique_Targets","Unique_Drug_Target_Associations","Mean_S.D.Targets_per_Drug","Mean_S.D.Drugs_per_Target")
    )
  })

  df_subset <- reactive({
    a <- subset(db, Database %in% input$base)
    keeps <- c("Database","Organism",input$sta)
    #a[, (colnames(a) %in% input$sta)]
    #a[keeps]
    a[ , which(names(a) %in% keeps)]
    return(a)
  })

  output$tbl = renderDT(
    df_subset(), options = list(lengthChange = FALSE)
  )
  }

1 个答案:

答案 0 :(得分:1)

您距离很近。第一个问题是在df_subset的定义中,您使用的a在脚本的其他任何地方都没有定义。我想您打算在那里使用db而不是a。第二个问题是,当您对a进行子集设置时,不会再将其分配给a,因此,当您返回a时,它将不会成为子集版本。以下是df_subset的固定版本。

df_subset <- reactive({

  # I stuck to the uncommented version of your subset, but I guess the others will work too
  keeps <- c("Database","Organism",input$sta)

  # assigning the subset of db to tmp
  tmp <- db[ , which(names(db) %in% keeps)]

  # returning tmp
  return(tmp)
})