user2814482
user2814482

Reputation: 651

how to select data based on a first selection - shiny app

I am new to using Shiny, I have read the tutorials, and a few questions on stacked overflow, but I think I"m still missing some key concept.

Basically I want users to first select a dataset. Then based on that dataset they can select an OTU of interest. Then I will display a plot and maybe a table.

I have the syntax for selecting the dataset correct, but how do I generate the choices of OTUs to select based on that ?

Any help appreciated.

thanks

 ui <- fluidPage(
  # Make a title to display in the app
  titlePanel(" Exploring the Effect of Metarhizium on the Soil and Root Microbiome "),
  # Make the Sidebar layout
  sidebarLayout(
    # Put in the sidebar all the input functions
    sidebarPanel(
      # drop down menu to select the dataset of interest
      selectInput('dataset', 'dataset', names(abundance_tables)),
      # drop down menu to select the OTU of interest
      uiOutput("otu"),
      #
      br(),
      # Add comment
      p("For details on OTU identification please refer to the original publications")
    ),
    # Put in the main panel of the layout the output functions 
    mainPanel(
        plotOutput('plot')
       # ,dataTableOutput("anova.tab")
      )
    )
)

server <- function(input, output){
    # Return the requested dataset ----
    datasetInput <- reactive({
      switch(input$dataset)
    })
    #
    dataset <- datasetInput()
    # output otus to choose basaed on dataset selection
    output$otu <- renderUI({
    selectInput(inputId = "otu", label = "otu",
                       choices = colnames(dataset))
    })
    output$plot <- renderPlot({
      # 
      dataset <- datasetInput() 
      otu <- input$otu
      #dataset<-abundance_tables[[1]]  
      ## melt and add sample metadata
      df_annot<-merge(dataset,sample_metadata,by="row.names",all.x=T)
      rownames(df_annot)<-df_annot[,1]
      df_annot<-df_annot[,-1]
      #
      dfM<-melt(df_annot,id.vars = c("Location","Bean","Fungi","Insect"),value.name="abund")
      # renaming Fungi level to metarhizium
      levels(dfM$Fungi)<-c("Metarhizium","No Meta")
      # 
      ggplot(subset(dfM, variable==otu),
      aes(x=Insect,y=abund,fill=Fungi))+geom_boxplot()+facet_wrap(~Location,scales="free_y" )+
      guides(fill=guide_legend("Metarhizium")) +
      ggtitle(otu)
  })
  }
  ##
  shinyApp(ui=ui,server=server)

Okay, I have made some fixes after some answers, but am now getting the following error.

Listening on http://127.0.0.1:5684
Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
Stack trace (innermost first):
    41: .getReactiveEnvironment()$currentContext
    40: .dependents$register
    39: datasetInput
    38: server [/Users/alisonwaller/Documents/Professional/Brock/Bidochka_Microbiome/shiny/Barelli_shiny.R#68]
     1: runApp
Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

Upvotes: 1

Views: 93

Answers (1)

Beeba
Beeba

Reputation: 642

Yes you are really close. Just replace this line: selectInput('otu', 'otu', uiOutput("otu")), with this: uiOutput("otu"),

There's no need for SelectInput() here since that is in the renderUI in the server function.

Upvotes: 1

Related Questions