【问题标题】:Subset a Column of a dataframe stored as a reactive expression eventReactive子集存储为反应式表达式 eventReactive 的数据帧的列
【发布时间】:2017-09-11 07:30:21
【问题描述】:

请原谅不可重现的例子。理论上的解决方案就可以了。我想知道如何对存储在反应式表达式中的数据框进行子集化处理,以分配一个唯一的 output_id,并最终显示在 UI 中。这类似于访问数据框的列,如下所示:df$column_name


我使用eventReactive() 将数据帧存储为名为@9​​87654322@ 的响应式表达式,该表达式与UI 中的actionButton() 链接。

环境中的代码:

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

【问题讨论】:

    标签: r shiny reactive-programming


    【解决方案1】:

    可能是以下示例回答了您的需求。 UI 有一个多选列表,列表的条目可用于子集iris 数据集的Species 列。

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

    【讨论】:

    • 感谢您的回答,但这并不是我要问的。在您的示例中,在 output$subdata &lt;- renderTable(data()) 中,我不想调用 all 的过滤数据。我只想要 一列 的过滤数据,例如 data()$SepalLength。我需要知道如何将data() 子集仅用于一列
    • 我已经更新了解决方案,希望对你有用。
    • 优秀。真的就像使用 $ 那么简单吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 2021-08-18
    • 2016-10-27
    • 1970-01-01
    相关资源
    最近更新 更多