子集存储为反应式表达式 eventReactive 的数据帧的列 [英] Subset a Column of a dataframe stored as a reactive expression eventReactive
问题描述
请原谅这个不可复制的例子.理论上的解决方案会很好.我想知道如何对存储在一个特定列的反应式表达式中的数据帧进行子集化,分配一个唯一的 output_id,并最终显示在 UI 中.这类似于访问数据帧的列,如下所示:df$column_name
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
我使用与 actionButton()
链接的 eventReactive()
将数据帧存储为名为 data()
的反应式表达式用户界面.
I store a dataframe as a reactive expression called data()
, using eventReactive()
which is linked with an actionButton()
in the UI.
环境中的代码:
# dataframe with n columns and k rows
df
用户界面:
actionButton(inputId = "go", label = "Update")
服务器:
# 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 })
推荐答案
以下示例可能会回答您的要求.UI 有一个多选列表,列表的条目可用于对iris
数据集的Species
列进行子集化.
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)
这篇关于子集存储为反应式表达式 eventReactive 的数据帧的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!