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

牧云@^-^@ 提交于 2021-01-28 05:33:52

问题


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

回答1:


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)


来源:https://stackoverflow.com/questions/46150358/subset-a-column-of-a-dataframe-stored-as-a-reactive-expression-eventreactive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!