如何根据第一个选择选择数据 - 闪亮的应用程序

时间:2018-05-30 18:02:48

标签: shiny

我是使用Shiny的新手,我已阅读过教程,以及关于堆叠溢出的一些问题,但我认为我仍然缺少一些关键概念。

基本上我希望用户首先选择一个数据集。 然后基于该数据集,他们可以选择感兴趣的OTU。 然后我会显示一个情节,也许还有一张桌子。

我有选择正确的数据集的语法,但是如何生成基于此选择的OTU选项?

任何帮助表示感谢。

感谢

 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)

好的,我已经在一些答案后做了一些修复,但现在出现了以下错误。

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.)

1 个答案:

答案 0 :(得分:1)

是的,你真的很亲密。只需替换此行: selectInput('otu', 'otu', uiOutput("otu")), 这个:uiOutput("otu"),

此处不需要SelectInput(),因为它位于服务器函数的renderUI中。