Rich Pauloo
Rich Pauloo

Reputation: 8402

Subset a Column of a dataframe stored as a reactive expression eventReactive

Forgive the non-reproducible example. A theoretical solution will do just fine. I want to know how to subset a dataframe stored in a reactive expression for one particular column, to be be assigned a unique output_id, and ultimately displayed in the UI. This is analogous to accessing a column of a dataframe like so: df$column_name


I store a dataframe as a reactive expression called data(), using eventReactive() which is linked with an actionButton() in the UI.

Code in Environment:

# dataframe with n columns and k rows
df 

UI:

actionButton(inputId = "go", label = "Update")

SERVER:

# create a reactive expression 'data()', a subsetted data.frame based on other reactive values

data() <- eventReactive( input$go, { df %>% filter(based on other reactive values) } )

output$output_id <- renderSomething( { code depends on data()$specific column })

Upvotes: 2

Views: 2582

Answers (1)

Imran Ali
Imran Ali

Reputation: 2279

May be the following example answers what you are after. The UI has a multi select list, the entries of the lists can be used to subset the Species column of iris data set.

# Using multi select list to sum columns
library(shiny)
library(dplyr)
# Define UI for application that draws a histogram
ui <- fluidPage(
  # Application title
  titlePanel("Subset and sum a column of iris"),
   fluidRow(
     selectInput('Species', 'Species', levels(iris$Species), multiple = TRUE, selectize = FALSE)
   ),
   fluidRow(verbatimTextOutput('selection')),
  fluidRow(tableOutput('dataColumn')),
  fluidRow(
    tags$h2("sum:"),
    verbatimTextOutput('sum')
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  output$selection <- reactive(input$Species)
  subiris = reactive({
    subset(iris, Species %in% input$Species)
  })
  output$dataColumn <- renderTable(subiris()$Sepal.Length)
 output$sum <- renderPrint(sum(subiris()$Sepal.Length)) 
} 


# Run the application 
shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions