Search code examples
rshinyshinyapps

How can I call the output of a Select box in Shiny?


I am creating an app in Shiny in which there are two inputs: a selectInput (inputId = tax) and a textInput (inputId = clade). The input$clade I call it inside output$pr2. This output is a data.frame that I have to call in the function seq.

Below I leave my code where I want the "x" to be the output$pr2.

Thank you very much for your help.

library(shiny)
library(dplyr)

ui <- fluidPage(
  
  titlePanel("Shiny App"),
  
  sidebarLayout(
    
    sidebarPanel(
      
      selectInput(inputId = "tax",
                  label = "Choose taxonomic group:",
                  choices = c("Domain", "Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species"),
                  selected = "Order"),
      
      textInput(inputId = "clade",
                label = "Group name:",
                value = "Suessiales")      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      # Output: Formatted text for clade (h3 = header type 3)
      h3(textOutput("clade", container = span)),
      
      # Output: HTML table with requested number of observations ----
      dataTableOutput("pr2")
      
    )
  )
)

# Define server logic to summarize and view selected dataset ----
server <- function(input, output) {
  
  output$pr2 <- renderDataTable({
    group <- switch(input$tax,
                    "Domain" = pr2 %>% dplyr::filter(domain == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence),
                    "Kingdom" = pr2 %>% dplyr::filter(kingdom == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence),
                    "Phylum" = pr2 %>% dplyr::filter(phylum == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence),
                    "Class" = pr2 %>% dplyr::filter(class == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence),
                    "Order" = pr2 %>% dplyr::filter(order == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence),
                    "Family" = pr2 %>% dplyr::filter(family == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence),
                    "Genus" = pr2 %>% dplyr::filter(genus == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence),
                    "Species" = pr2 %>% dplyr::filter(species == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence))
  })
  
  
  # Part of the code that does not work
  output$seq_clade <- function(x){
  seq_clade <- Biostrings::DNAStringSet(x$sequence)
  names(seq_clade) <- paste(x$genbank_accession, sep="|")
  return(as.data.frame(seq_clade))
  }
  
}

# Create Shiny app ----
shinyApp(ui, server)

Solution

  • I am not sure if I have understood your question well, but I will try to answer.

    Getting pr2_clade using this code:

    pr2_clade <- pr2 %>% dplyr::filter(order == "Suessiales") %>% dplyr::select(genbank_accession, sequence, sequence_length) 
    

    It looks like this: enter image description here

    You want to use this function:

    seq_clade <- function(x){
      seq_clade <- Biostrings::DNAStringSet(x$sequence)
      names(seq_clade) <- paste(x$genbank_accession, sep="|")
      return(as.data.frame(seq_clade))
    }
    

    Where x can be pr2_clade right? So, if you call the function and save the output in one variable:

    mydf = seq_clade(pr2_clade)
    

    You get this dataframe: enter image description here

    Am I right? So, the problem that you have in your shiny app is that you cannot use the function (?)

    If it is that case, you don't have to save your function inside an output$, just save it as a normal function. And since you want to use the table that you get after filtering, you have to save it into a reactive function. Therefore, you final table will be after you use the function with your data.

    Here is the code. Note that I have made some changes to your function in order to show the final table properly.

    library(shiny)
    library(dplyr)
    library(pr2database)
    
    ui <- fluidPage(
      
      titlePanel("Shiny App"),
      
      sidebarLayout(
        
        sidebarPanel(
          
          selectInput(inputId = "tax",
                      label = "Choose taxonomic group:",
                      choices = c("Domain", "Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species"),
                      selected = "Order"),
          
          textInput(inputId = "clade",
                    label = "Group name:",
                    value = "Suessiales")      
        ),
        
        # Main panel for displaying outputs ----
        mainPanel(
          
          # Output: Formatted text for clade (h3 = header type 3)
          h3(textOutput("clade", container = span)),
          
          # Output: HTML table with requested number of observations ----
          dataTableOutput("pr2")
    
          
        )
      )
    )
    
    # Define server logic to summarize and view selected dataset ----
    server <- function(input, output) {
      
      mydf <- reactive({
        group <- switch(input$tax,
                        "Domain" = pr2 %>% dplyr::filter(domain == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence),
                        "Kingdom" = pr2 %>% dplyr::filter(kingdom == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence),
                        "Phylum" = pr2 %>% dplyr::filter(phylum == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence),
                        "Class" = pr2 %>% dplyr::filter(class == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence),
                        "Order" = pr2 %>% dplyr::filter(order == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence),
                        "Family" = pr2 %>% dplyr::filter(family == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence),
                        "Genus" = pr2 %>% dplyr::filter(genus == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence),
                        "Species" = pr2 %>% dplyr::filter(species == input$clade) %>% dplyr::select(genbank_accession, sequence_length, sequence))
        
        return(group)
      })
    
      
      seq_clade <- function(x){
        seq_clade <- Biostrings::DNAStringSet(x$sequence)
        names(seq_clade) <- paste(x$genbank_accession, sep="|")
        df = as.data.frame(seq_clade) #transform into a dataframe
        df$genbank_accession <- rownames(df) # save the rownames as a new column
        rownames(df) <- NULL # remove rownames
        colnames(df)[1] <- "sequence" # change name of the first column
        df <- df %>% 
          dplyr::select(genbank_accession, everything()) # change order of the columns
        return(df)
      }
      
    
        
      output$pr2 <- renderDataTable({
         seq_clade(mydf())
        })
     
      
    }
    
    # Create Shiny app ----
    shinyApp(ui, server)
    

    Result: enter image description here

    Hope this helps or at least I hope having understood your question.