我想创建一个使用shinyFiles
加载文件的闪亮应用程序,那么用户应该能够通过以下方式过滤数据表:
a)使用textArea
输入,用户可以在其中粘贴一些值,数据表将被过滤
b)用户上传一个文件,然后通过上传文件中的值对数据表进行过滤(上传的文件应该是带有标题和逐行基因的文件)
这是我的代码,但是如果我在同一textAreaInput
中使用shinyFilesButton
和tabItem
,则无法创建过滤器数据集。我想将其保留在相同的tabItem
上,而不要创建两个不同的tabItems
。
问题是,当我上传文件时,它可以正常运行,但是如果我输入textAreaInput
却没有任何反应,我想创建一个删除过滤器或类似的过滤器,使我可以运行一个或另一个过滤器。
这是我到目前为止的代码:
header <- dashboardHeader()
sidebar <- dashboardSidebar(
sidebarUserPanel("Panelado variantes"),
sidebarMenu(
# Setting id makes input$tabs give the tabName of currently-selected tab
id = "tabs",
menuItem("Archivo variantes", tabName = "fileupload", icon = icon("table")),
conditionalPanel("input.tabs == 'fileupload' ",
shinyFilesButton("file", "Choose a file" , multiple = FALSE, title = "Please select a file:",buttonType = "default", class = NULL)#,
),
menuItem("Panelado por genes", tabName="panelado"),
conditionalPanel("input.tabs == 'panelado'",
shinyFilesButton("file_gene", "upload gene list" , multiple = FALSE, title = "Please upload a gene list:", buttonType = "default", class = NULL),
textAreaInput(inputId = "genes", label = "Gene List", value = "Paste some genes", width = "180", height = "200"),
actionButton("go", "Filter")
)
)
)
body <- dashboardBody(
tags$style(type="text/css",
".shiny-output-error { visibility: hidden; }",
".shiny-output-error:before { visibility: hidden; }",
".shiny-output-error:after { visibility: hidden; }"),
tabItems(
tabItem(tabName = "fileupload",
fluidPage(
box(width=12, height = '1000px',
column(12,dataTableOutput("tabla"))
)
)
),
tabItem("panelado",
fluidPage(
box(width=18, height = '950px',
column(12,dataTableOutput("tablafilt")))
)
)
)
)
ui <- dashboardPage(header, sidebar, body)
server = function(input, output, session) {
volumes = getVolumes()
volumes <- c(Home = fs::path_home(), "R Installation" = R.home(), getVolumes()())
file_selected <- reactive({
shinyFileChoose(input, "file", roots = volumes, session = session)
if (is.null(input$file))
return(NULL)
print(parseFilePaths(volumes, input$file)$datapath)
return(parseFilePaths(volumes, input$file)$datapath)
})
contents <- reactive({
if (is.null(file_selected()))
return()
df <- read.delim(file_selected(), header = TRUE, stringsAsFactors=FALSE, as.is=TRUE)
return(df)
})
output$tabla <- DT::renderDataTable({
if(is.null(contents()))
return()
datos <- contents()
DT::datatable(datos)
})
file_selected2 <- reactive({
shinyFileChoose(input, "file_gene", roots = volumes, session = session)
if (is.null(input$file_gene))
return(NULL)
return(parseFilePaths(volumes, input$file_gene)$datapath)
})
gene_list <- reactive({
if (is.null(file_selected2()))
return()
df <- read.delim(file_selected2(), header = TRUE, stringsAsFactors=FALSE, as.is=TRUE)
genes <- as.character(df[,1])
})
filtrado1 <- reactive({
if (is.null(file_selected2()))
return()
df <- filter(contents(), Gene.refGene %in% gene_list())
})
glist <- reactive({
glist <- isolate(input$genes)
print (glist)
gnames <- gsub(" ","",glist,fixed=TRUE)
names <- unlist(strsplit(gnames,"\n"))
genes <- as.character(names)
})
filtrado2 <- reactive({
if (is.null(glist()))
return()
df <- filter(contents(), Gene.refGene %in% glist())
})
output$tablafilt <- DT::renderDataTable({
if(is.null(contents()))
return()
filtrado <- reactive({
if (!is.null(filtrado1())){
df <- filtrado1()
}
else if (!is.null(filtrado2())){
df <- filtrado2()
}
})
DT::datatable(filtrado())
})
}
shinyApp(ui, server)