首页 > 解决方案 > 在 Shiny App 中设置反应变量

问题描述

我正在尝试编写我的第一个 Shiny App。我从这个站点得到了很多帮助,但又被卡住了,并认为这与我的反应变量没有被初始化有关。

我在创建表的函数时遇到问题 - anova_tab2。如果我不包括 if(compFacts=="Metarhizium){} 部分,那么它工作正常,但是一旦我输入 if 语句,我就会收到关于“长度为零的争论”的错误,我认为意味着 if (compFacts== ) 不工作我假设因为 compFacts 变量是空的?

我只包含了我的服务器功能的一部分,给出了问题。谢谢

这是我的部分脚本:

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(
      tabsetPanel(id="tabs",
        tabPanel("otu", selectInput('dataset', 'dataset', names(abundance_tables),selected=names(abundance_tables)[1]),
                 uiOutput("otu"), br(),
                 # Add comment
                 p("For details on OTU identification please refer to the original publications")),
        tabPanel("anova", sliderInput('pval','p-value for significance',
                                      value=0.1,min=0,max=0.5,step=0.00001),
                 selectInput('dataset', 'dataset', names(abundance_tables)),
                 selectInput('fact_ofInt','factor of interest',FactorsOfInt,selected="Metarhizium"))
        ) 
    ),
    # Put in the main panel of the layout the output functions 
      mainPanel(
        conditionalPanel(condition="input.tabs == 'otu'",
                         plotOutput('plot'),
                         tableOutput("table1"),
                         p("anova for Soil Samples"),
                         tableOutput("tableS"),
                         tableOutput("tableR")

        ),
        conditionalPanel(condition="input.tabs == 'anova'",
                         #plotOutput('plot2')
                        # verbatimTextOutput("anovaText")
                        tableOutput("anova_tab2")

        )
      )
  )
)

# ========================= SERVER ####
  server <- function(input, output){
    # Return the requested dataset ----
    datasetInput <- reactive({
      abundance_tables[[input$dataset]]
    })
    pvalInput<-reactive({
      input$pval
   })
    comparisonInput<-reactive({
      input$FactorsOfInt
    })
    #
    # output otus to choose basaed on dataset selection
   output$otu <- renderUI({
     selectInput(inputId = "otu", label = "otu",
                       choices = colnames(datasetInput()),selected="Metarhizium")
    })
   otuInput<-reactive({
     input$otu
   })

  output$anova_tab2 <- renderTable({
    pval<-pvalInput()
    df<-datasetInput()
    compFacts<-comparisonInput()
    print(paste("compFacts:",compFacts))
    df_annot<-merge(df,sample_metadata,by="row.names",all.x=T)
    rownames(df_annot)<-df_annot[,1]
    df_annot<-df_annot[,-1]
    df_annot<-subset(df_annot,df_annot$Bean =="Bean")
    sum_tab.sig<-data.frame(Df=as.numeric(),Sum.Sq=as.numeric(),Mean.Sq=as.numeric(),Fval=as.numeric(),Pval=as.numeric(),OTU=as.character())
      for (i in 1:ncol(df)) {
      #  if (compFacts=="Metarhizium"){
          aov.ex <- aov(df_annot[,i]~df_annot$Fungi)
       # } 
        #else if (compFacts=="Insect"){
        #  aov.ex <- aov(df_annot[,i]~df_annot$Insect)
        #} else if (compFacts=="Sample_Type"){
        #  aov.ex <- aov(df_annot[,i]~df_annot$Location)
        #}
        ## summary of the anova given as a list
        sum_tab<-summary(aov.ex)[[1]]
        temp.sig<-sum_tab[sum_tab[,"Pr(>F)"]<pval,]  
        temp.sig<-transform(temp.sig,OTU=colnames(df)[i])
           sum_tab.sig<-rbind(sum_tab.sig,temp.sig)
          sum_tab.sig<-sum_tab.sig[!is.na(sum_tab.sig[,1]),]
          if (dim(sum_tab.sig)[1]==0){
            sum_tab.sig<-c(rep(0,5),"no significant trends at this pvalue")
          }
      }
    sum_tab.sig<-sum_tab.sig[-1,c(6,4,5)]
    colnames(sum_tab.sig)<-c("OTU","F-value","P-value")
    sum_tab.sig$'P-value'<-format(sum_tab.sig$'P-value',digits=3,scientific=5)
  return(sum_tab.sig)
  })
   ### end of server
  }
  ##

标签: rshiny

解决方案


推荐阅读