如何在两个不同的排除过程中使用相同的变量名上传带有R闪亮应用程序的表



我开发了一个非常复杂的Shiny应用程序,可以帮助用户过滤上传的遗传变异表。这样,用户就可以上传一个表,应用不同的过滤器,并查看剩余的变体。现在我希望用户能够以两种不同的排除方式上传表格:

首先:用户使用fileInput直接上传表格。第二:用户按下一个按钮,用一个在外部运行的python程序对正在应用的表进行一系列更改,然后为会话创建一个经过处理的表,并上传到另一个按钮进行过滤。

这两个选项都会产生一个上传的表,可以用我的程序进行过滤,所以我想在这两种情况下都保留相同的变量名。当另一个被评论时,两个进程都能完美地工作,但我希望用户可以同时使用这两个上传选项。由于程序的复杂性,我不能在这里展示一个完全可复制的例子,但我可以向您展示我想使用的代码部分。

library(shiny)
library(DT)    
library(shinyWidgets)
library(shinyBS)
library(shinyFiles)
ui = fluidPage(
# Uploading variant table straight away with a file input (way 1):
fileInput("file1", "Upload your SNV File",
multiple = FALSE,
accept = c("text/csv", "text/comma-separated-values,text/plain", ".tsv")),
# User presses a button if changes previous to the upload want to be applied (way 2):
actionBttn(
inputId = "WGS",
label = "Analysis of WGS", 
),
# User needs to fill a survey before the python program is launched:
bsModal("survey", "Select WGS data information","WGS", 
prettyCheckbox(inputId="canonical_filters", label = "Canonical", value = TRUE),
shinyFilesButton("Btn_GetFile", "Process WGS variant file", title = "WGS variant file:", multiple = FALSE),
actionButton("EnterWGS", "Read file")),
# Table is rendered
DTOutput("contents")
)

server <- function(input, output, session) {
# Datatable is uploaded straight away (WAY 1)
df <- reactive({
req(input$file1)
df <- read.table(input$file1$datapath, fill = TRUE, quote = "", header = TRUE,
sep = 't', na.strings=c("",".","NA"), colClasses = NA)

})
# The path of the file where the changes are going to be applied can be selected and the python program (process_file.py) is launched with the system function. A processed variant table is created for the session.
observeEvent(input$Btn_GetFile, {  
volumes = getVolumes()
shinyFileChoose(input, "Btn_GetFile", roots=volumes, session = session, filetypes = c('', 'txt',  "tsv", "csv"))
file_selected<-parseFilePaths(volumes, input$Btn_GetFile)
if (length(file_selected$datapath)!=0){
system('process_file.py', file_selected$datapath )
}
})

# The processed variant table is uploaded when the button is pressed (WAY 2)  
df <- eventReactive(input$EnterWGS, {
df <- read.table('temp_file', fill = TRUE, quote = "", header = TRUE,
sep = 't', na.strings=c("",".","NA"), check.names = FALSE, colClasses = NA)
}, ignoreNULL = T)

# Rest of the functions...
# Table renderization.
output$contents <- renderDT({
req(df())
datatable(
df(),
filter = "top", 
class = "display nowrap compact",
escape = FALSE)},
server = FALSE)

}
shinyApp(ui, server)

我真的希望这是可以理解的。如有任何帮助,我们将不胜感激。

非常感谢

Rachael

您可以定义要显示的reactiveValues对象,该对象设置为表1或表2。试试这个

library(shiny)
library(DT)    
library(shinyWidgets)
library(shinyBS)
library(shinyFiles)
ui = fluidPage(

# Uploading variant table straight away with a file input (way 1):
fileInput("file1", "Upload your SNV File",
multiple = FALSE,
accept = c("text/csv", "text/comma-separated-values,text/plain", ".tsv")),

# User presses a button if changes previous to the upload want to be applied (way 2):
actionBttn(
inputId = "WGS",
label = "Analysis of WGS", 
),

# User needs to fill a survey before the python program is launched:
bsModal("survey", "Select WGS data information","WGS", 
prettyCheckbox(inputId="canonical_filters", label = "Canonical", value = TRUE),
shinyFilesButton("Btn_GetFile", "Process WGS variant file", title = "WGS variant file:", multiple = FALSE),
actionButton("EnterWGS", "Read file")),

# Table is rendered
DTOutput("contents")
)

server <- function(input, output, session) {
rv <- reactiveValues(df=NULL)
# Datatable is uploaded straight away (WAY 1)
df1 <- reactive({
req(input$file1)
df <- read.table(input$file1$datapath, fill = TRUE, quote = "", header = TRUE,
sep = 't', na.strings=c("",".","NA"), colClasses = NA)

})

# The path of the file where the changes are going to be applied can be selected and the python program (process_file.py) is launched with the system function. A processed variant table is created for the session.
observeEvent(input$Btn_GetFile, {  
volumes = getVolumes()
shinyFileChoose(input, "Btn_GetFile", roots=volumes, session = session, filetypes = c('', 'txt',  "tsv", "csv"))
file_selected<-parseFilePaths(volumes, input$Btn_GetFile)
if (length(file_selected$datapath)!=0){
system('process_file.py', file_selected$datapath )
}
})


# The processed variant table is uploaded when the button is pressed (WAY 2)  
df2 <- eventReactive(input$EnterWGS, {
df <- read.table('temp_file', fill = TRUE, quote = "", header = TRUE,
sep = 't', na.strings=c("",".","NA"), check.names = FALSE, colClasses = NA)
}, ignoreNULL = T)

###  condition this observer to display df1()
observeEvent(df1(), {
rv$df <- df1()
})

###  condition this observer to display df2()
observeEvent(input$WGS, {
rv$df <- df2()
})

# Rest of the functions...

# Table renderization.
output$contents <- renderDT({
datatable(
rv$df,
filter = "top", 
class = "display nowrap compact",
escape = FALSE)},
server = FALSE)

}
shinyApp(ui, server)

最新更新