Friday, May 25, 2018
Data analysis based on variables of a uploaded file OR the default dataset in R Shiny
Answer
Answer
I would like to make a Shiny app where the user can either upload their own rda/rds files or use the default datasets. The input selections choices will change depending on whether they want to use their down data or the default data.
E.g. in my code, I want the choices for mtbSelection to change depending on the values on the conditionalPanel.
I'm having trouble understanding how to load a .rda/.rds file in the server function and I'm not sure why the updateSelectInput doesn't work. Any help would be appreciated!
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)
Answer
Update: I updated the if statement. And very important the updateSelectInput was not working because of a typo!
Here is the code to generate the dummy data I used:
data(cars)
saveRDS(cars,'cars.rds')
I suggest the following server code (rest can stay as it is):
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()))
})
}
plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV
In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...
-
How to display something on or after selecting a specific radio button, for example is I have a group of radio button Interest Value o 3% ...
-
I use the code below to update the url link based on user text input: Right now it works pretty fine when someone clicks the submit ...
-
I need to be able to determine when an object is created (not a DOM element -- a javascript object). An answer to this question has some ve...
No comments:
Post a Comment