Reputation: 8404
I have a simple shiny app in which I subset a dataframe by row selection and by column names selection. After subseting by rows I tried 3 different methods to subset it by columns as well but the dataframe remains the same. More specifically I have used :
keeps <- c("Database","Organism",input$sta)
#a[, (colnames(a) %in% input$sta)]
#a[keeps]
a[ , which(names(a) %in% keeps)]
but nothing seems to work
#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)
)
}
Upvotes: 0
Views: 45
Reputation: 2031
You were quite close. The first problem is that in your definition of df_subset
, you use a
which is not defined anywhere else in the script. I guess you meant to use db
there instead of a
. The second problem is that when you subset a
, you do not assign it again to a
, and so when you return a
it will not be the subsetted version. Below is the fixed version of 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)
})
Upvotes: 1