Friday, 25 May 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()))
})
}
php - file_get_contents shows unexpected output while reading a file
I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...
-
I have an app which needs a login and a registration with SQLite. I have the database and a user can login and register. But i would like th...
-
I got an error in my Java program. I think this happens because of the constructor is not intialized properly. My Base class Program public ...
-
I would like to use enhanced REP MOVSB (ERMSB) to get a high bandwidth for a custom memcpy . ERMSB was introduced with the Ivy Bridge micro...
No comments:
Post a Comment