简体   繁体   中英

shiny r: numericInput from the uploaded data

Ive just started using shiny and got the following basic questions.

1) The uploaded longitudinal data includes the column with Treatment names (eg A,B,C,D) and the other one includes corresponding numeric codes: eg 1,2,4,6. The coding might be different depending on the data uploaded. Each treatments is administrated on group of patients.

I want to use numeric codes to select treatments to be compared, sort of numericInput(). I need the list to be updated according to the coding in the actual dataset provided. So far I did it with numericInput() assuming the coding will be between 1 and 10 (see code below).

2) What If I want to select according to Treatment names (here A,B,C,D), which may differ among the datasets of interest?

Help much appreciated.

shinyServer(function(input, output){
## Data reactives:
uploaded_Data <- reactive({
    inFile <- input $ data
    if(is.null(inFile)){return(NULL)}
    read.csv(file = inFile $ datapath,
             header=TRUE)

output $ raw_data <- renderTable({
    uploaded_Data()
})## for table


})

shinyUI(pageWithSidebar(
headerPanel(''),
sidebarPanel(
    fileInput('data', 'File to upload (.csv only):',
              accept=c('.csv')),
    tags $ hr(),
    h4('Select treatments:'),
    numericInput('T1','Treatment1 code:',1, min=1, max=10, step=1),
    numericInput('T2','Treatment2 code:',2, min=2, max=10, step=1)
    ),

## Option for tabsets:
mainPanel(
    tabsetPanel(
        tabPanel('Uploaded Data',
                 tableOutput('raw_data'))
        )
    )
))## overall

I think that what you're asking is how to render a dynamic UI input based on uploaded data?

If that is the case then try integrating the following strategy into your app:

server.R:

#incomplete example
output$groupsToCompare <- renderUI({
  data <- uploaded_data()
  if(is.null(data)){return(NULL)} #This prevents an error if no data is loaded

  #In this example I will use selectInput, but you can also use checkboxInput or whatever really
  selectInput(inputId = 'selectedGroups', label = 'Select which groups to compare', choices = unique(data$treatments), multiple = TRUE)
})

#an example of how to use this input object
output$dataToShow <- renderTable ({
  data <- uploaded_data()
  if(is.null(data)){return(NULL)}

  #subset the data of interest; There are MANY ways to do this, I'm being verbose
  subsetData <- subset(data, input$selectedGroups)

  #alternatively, you could do data[input$selectedGroups]
  return(subsetData)
})

ui.R:

#incomplete example
uiOutput('selectedGroups')

This will dynamically generate a list of unique factors which can be selected as inputs. In your case, it will either generate a list of "A", "B", "C", "D" or a numeric factor list. These inputs can be used to subset your data, or select certain variables for whatever you can dream of. I think that this answers both of your questions, but if not please let me know so I can clarify. I haven't tested the selectInput with multiple = TRUE before, but I imagine it will work just fine. This will feel unusual at first, as you're basically setting up a UI element in server.R, where you're used to building the UI in ui.R, but once you do it a couple of times it gets gravy.

I think using the updateCheckboxGroupInput() -function could be helpful

In server.R :

First change shinyServer(function(input, output) ... to shinyServer(function(input, output, session) ...

than add:

observe({
    x <- uploaded_Data()
    updateCheckboxGroupInput(session,
                             inputID = "selectedTreatments",
                             label = "Select which groups to compare",
                             choices = unique(x$treatments), # treatment columns
                             selected = unique(x$treatments),
                             inline = FALSE)
})

In ui.R in the sidebarPanel() add to have

checkboxGroupInput(inputId = 'selectedTreatments',
                   label = 'no treatments to select yet',
                   choices = 1)

This creates a dummy checkbox, which will be replaced with the treatments ,after uploading the file. The observe function makes the 'update'-process invisible. The numeric inputs can be done analogously.

Feedback would be nice because I'm starting to learn it myself and had the same problem. This solution worked for me. I dont know if there's a way to let the checkbox appear only when the file is uploaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM