基于上传文件的变量或 R Shiny 中的默认数据集的数据分析
Data analysis based on variables of a uploaded file OR the default dataset in R Shiny
我想制作一个 Shiny 应用程序,用户可以在其中上传自己的 rda/rds 文件或使用默认数据集。 input selections 选项将根据他们是要使用他们的向下数据还是默认数据而改变。
例如在我的代码中,我希望根据条件面板上的值更改 mtbSelection 的选项。
我无法理解如何在服务器函数中加载 .rda/.rds 文件,而且我不确定为什么 updateSelectInput 不起作用。如有任何帮助,我们将不胜感激!
library(shiny)
library(shinythemes)
ui <- fluidPage(
theme = shinytheme("paper"),
checkboxInput("default_data", "Would you like to use default datasets?", value = TRUE),
conditionalPanel(condition = "input.default_data == true",
selectizeInput(inputId = "mtb2", label = "Please choose a metabolomic dataset",
choices = "mtb2",
options = list(placeholder = 'Select a default metabolomic file below',onInitialize = I('function() { this.setValue(""); }'))
),
selectizeInput(inputId = "geneExp2", label = "Please choose a transcriptome dataset",
choices = "geneExp2",
options = list(placeholder = 'Select a default transcriptome file below',onInitialize = I('function() { this.setValue(""); }'))
)
),
conditionalPanel(condition = "input.default_data == false",
fileInput(inputId = "file_mtb", label = "Please upload a metabolomic dataset",
multiple = FALSE, accept = c('.RData', '.rda', '.rds'), placeholder = " No file selected"
),
fileInput(inputId = "file_ge", label = "Please upload a transcriptome dataset",
multiple = FALSE, accept = c('.RData', '.rda', '.rds'), placeholder = " No file selected"
)
),
selectInput("mtbSelection", strong("Select a metabolite of interest"), choices = "",
multiple = FALSE)
)
server <- function(input, output, session) {
UploadMtbData <- reactive({
infile <- input$file_mtb
if (is.null(infile)){
return()
} else {
return(readRDS(infile$datapath))
}
})
observe({
if (is.null(input$file_mtb)) #makes sure that the uploaded file is not null
return()
obj<-switch(input$file_mtb,
mtb2,
infile)
var.opts <- colnames(obj)
updateSelectInput(session, "mtbSelction", choices = var.opts)
})
}
shinyApp(ui = ui, server = server)
更新:我更新了 if 语句。非常重要的是 updateSelectInput 由于打字错误而无法正常工作!
这是生成我使用的虚拟数据的代码:
data(cars)
saveRDS(cars,'cars.rds')
我建议以下服务器代码(其余可以保持原样):
server <- function(input, output, session) {
# reactive data
mtbData <- reactive({
# read default or user data
if(input$default_data == TRUE || is.null(input$file_mtb)){
# load your data here
} else {
# get input
infile <- input$file_mtb
# read and return
readRDS(infile$datapath)
}
})
# update observer
observe({
# update
updateSelectInput(session, "mtbSelection", choices = colnames(mtbData()))
})
}
我想制作一个 Shiny 应用程序,用户可以在其中上传自己的 rda/rds 文件或使用默认数据集。 input selections 选项将根据他们是要使用他们的向下数据还是默认数据而改变。
例如在我的代码中,我希望根据条件面板上的值更改 mtbSelection 的选项。
我无法理解如何在服务器函数中加载 .rda/.rds 文件,而且我不确定为什么 updateSelectInput 不起作用。如有任何帮助,我们将不胜感激!
library(shiny)
library(shinythemes)
ui <- fluidPage(
theme = shinytheme("paper"),
checkboxInput("default_data", "Would you like to use default datasets?", value = TRUE),
conditionalPanel(condition = "input.default_data == true",
selectizeInput(inputId = "mtb2", label = "Please choose a metabolomic dataset",
choices = "mtb2",
options = list(placeholder = 'Select a default metabolomic file below',onInitialize = I('function() { this.setValue(""); }'))
),
selectizeInput(inputId = "geneExp2", label = "Please choose a transcriptome dataset",
choices = "geneExp2",
options = list(placeholder = 'Select a default transcriptome file below',onInitialize = I('function() { this.setValue(""); }'))
)
),
conditionalPanel(condition = "input.default_data == false",
fileInput(inputId = "file_mtb", label = "Please upload a metabolomic dataset",
multiple = FALSE, accept = c('.RData', '.rda', '.rds'), placeholder = " No file selected"
),
fileInput(inputId = "file_ge", label = "Please upload a transcriptome dataset",
multiple = FALSE, accept = c('.RData', '.rda', '.rds'), placeholder = " No file selected"
)
),
selectInput("mtbSelection", strong("Select a metabolite of interest"), choices = "",
multiple = FALSE)
)
server <- function(input, output, session) {
UploadMtbData <- reactive({
infile <- input$file_mtb
if (is.null(infile)){
return()
} else {
return(readRDS(infile$datapath))
}
})
observe({
if (is.null(input$file_mtb)) #makes sure that the uploaded file is not null
return()
obj<-switch(input$file_mtb,
mtb2,
infile)
var.opts <- colnames(obj)
updateSelectInput(session, "mtbSelction", choices = var.opts)
})
}
shinyApp(ui = ui, server = server)
更新:我更新了 if 语句。非常重要的是 updateSelectInput 由于打字错误而无法正常工作! 这是生成我使用的虚拟数据的代码:
data(cars)
saveRDS(cars,'cars.rds')
我建议以下服务器代码(其余可以保持原样):
server <- function(input, output, session) {
# reactive data
mtbData <- reactive({
# read default or user data
if(input$default_data == TRUE || is.null(input$file_mtb)){
# load your data here
} else {
# get input
infile <- input$file_mtb
# read and return
readRDS(infile$datapath)
}
})
# update observer
observe({
# update
updateSelectInput(session, "mtbSelection", choices = colnames(mtbData()))
})
}