我想制作一个Shiny应用程序,用户可以在其中上传自己的rda / rds文件或使用默认数据集。输入选择选项将根据他们要使用其向下数据还是默认数据而变化。
例如在我的代码中,我希望mtbSelection的选择根据conditionalPanel上的值进行更改。
我在理解如何在服务器功能中加载.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)
答案 0 :(得分:0)
更新:我更新了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()))
})
}