我正在做一些工作来显示由web服务器上的maftools生成的图片,但它似乎是subsetMaf()" query"参数无法从Web服务器获得Shiny反应值输入。以下是一个简单的测试: subsetMaf()在R上的控制台上运行良好,如下所示:
library(magrittr)
mc3_pass <- readr::read_rds(file.path(config$database, "TCGA", "snv", "snv_mutation_mc3_public.pass.filtered_maf-4cancers.rds.gz")) # test data include clinical data, which include a column called "cancer_types"
mc3_pass %>% maftools::getClinicalData()
Tumor_Sample_Barcode cancer_types
1: TCGA-05-4244-01A-01D-1105-08 LUAD
2: TCGA-05-4249-01A-01D-1105-08 LUAD
3: TCGA-05-4250-01A-01D-1105-08 LUAD
4: TCGA-05-4382-01A-01D-1931-08 LUAD
5: TCGA-05-4384-01A-01D-1753-08 LUAD
---
2245: TCGA-Z2-A8RT-06A-11D-A372-08 SKCM
2246: TCGA-Z2-AA3S-06A-11D-A397-08 SKCM
2247: TCGA-Z2-AA3V-06A-11D-A397-08 SKCM
2248: TCGA-Z7-A8R5-01A-42D-A41F-09 BRCA
2249: TCGA-Z7-A8R6-01A-11D-A41F-09 BRCA
ct <- reactive({c("LUAD")}) # pre define a object stored a cancer type name like reactive value in shiny.
query = as.expression("cancer_types %in% isolate(ct())") # define a query for filter, isolate() used to get the value of reactive value on console.
maftools::subsetMaf(mc3_pass, query = query, mafObj = T) -> gene_list_maf # subsetMaf filter
gene_list_maf %>%
maftools::getClinicalData() # check the filter results
Tumor_Sample_Barcode cancer_types
1: TCGA-05-4244-01A-01D-1105-08 LUAD
2: TCGA-05-4249-01A-01D-1105-08 LUAD
3: TCGA-05-4250-01A-01D-1105-08 LUAD
4: TCGA-05-4382-01A-01D-1931-08 LUAD
5: TCGA-05-4384-01A-01D-1753-08 LUAD
508: TCGA-NJ-A55O-01A-11D-A25L-08 LUAD
509: TCGA-NJ-A55R-01A-11D-A25L-08 LUAD
510: TCGA-NJ-A7XG-01A-12D-A397-08 LUAD
511: TCGA-O1-A52J-01A-11D-A25L-08 LUAD
512: TCGA-S2-AA1A-01A-12D-A397-08 LUAD
在运行以下代码之前,请删除之前生成的对象。
mc3_pass <- readr::read_rds(file.path(config$database, "TCGA", "snv", "snv_mutation_mc3_public.pass.filtered_maf-4cancers.rds.gz")) # test data include clinical data, which include a column called "cancer_types"
library(shiny)
Lung_choice <- list(
"Lung Adenocarcinoma(LUAD)" = "LUAD",
"Lung Squamous Cell Carcinoma(LUSC)" = "LUSC"
)
ui <- fluidPage(
checkboxGroupInput(inputId = "select",label = "Select cancers",
choices = Lung_choice),
actionButton("go",label = "Go"),
shiny::textOutput(outputId = "table")
)
server <- function(input, output, session) {
ct <- reactive({
input$select
})
observeEvent(input$go,{
print(ct())
print(class(ct()))
ct_2 <- as.character(ct())
print(ct_2)
# query = as.expression("cancer_types %in% ct()") # Error in ct: could not find function "ct"
# query = as.expression("cancer_types %in% ct_2") # Error in eval: object 'ct_2' not found
query = as.expression("cancer_types %in% input$select") # Error in eval: object 'input' not found
print(query)
maftools::subsetMaf(mc3_pass, query = query, mafObj = T) -> gene_list_maf # subsetMaf filter
print("maf done!")
gene_list_maf %>%
maftools::getClinicalData() %>%
dplyr::select(cancer_types) %>%
unique() %>% t() %>%
as.vector()->.x# check the filter results
print(".x done!")
output$table <- renderText({
return(.x)
})
print("out done!")
print(.x)
})
}
shinyApp(ui, server)
# Below is output shows that object ct() and ct_2 exactly stored the value from webpage input
[1] "LUAD"
[1] "character"
[1] "LUAD"
expression("cancer_types %in% input$select")
Warning: Error in eval: object 'input' not found
Stack trace (innermost first):
75: eval
74: eval
73: [.data.table
72: [
71: maftools::subsetMaf
70: observeEventHandler [#14]
4:
3: do.call
2: print.shiny.appobj
1:
我想这是环境问题,但我很难解决这个问题,你有什么建议吗?有什么方法可以解决这个问题吗? 非常感谢,任何建议都会有所帮助。
答案 0 :(得分:0)
代码中的问题是query = as.expression("cancer_types %in% input$select")
。
您可以通过以下代码替换上述代码来解决此问题:
query = as.expression(paste0("cancer_types %in%'",input$select,"'"))
我们从paste0("cancer_types %in%'",input$select,"'")
获得的是什么
expression("cancer_types %in%'LUAD'")
。由于input$select
功能的本地环境中不存在subsetMaf
,因此创建了错误,现在通过此更改,我们传递了数据集中存在的'LUAD'
。
修改强>
当所选输入大于1时,您可以先组合输入然后传递它。代码如下:
InpSel <- paste0(input$select, collapse = "','")
query = as.expression(paste0("cancer_types %in% c('",InpSel,"')"))
希望它有所帮助!