用于过滤数据框的闪亮下拉菜单选择 [英] Shiny dropdown menu selection to filter a dataframe

查看:63
本文介绍了用于过滤数据框的闪亮下拉菜单选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 Shiny、下拉选择和该选择过滤的问题.

I've got a question about Shiny, dropdown selections and filtering on that selection.

我有下拉列表;很容易,但我很难接受该输入,然后根据该值过滤数据框.我一直在说只能在反应式表达式中完成"错误.

I have the dropdown list; easy enough but I'm having a difficult time taking that input and then filtering a dataframe based on that value. I keep getting "can only be done inside a reactive expression" errors.

对于闪亮的应用程序

library(dtplyr)
library(dplyr)
library(shiny)
library(shinyWidgets)
library(shinydashboard)

citylist <- data.frame("City" = c("Abilene", "Amarillo", "Omaha"))
wxdata <- data.frame("City" = c("Abilene", "Abilene", "Abilene", "Abilene", "Amarillo", "Amarillo", "Amarillo", "Amarillo", "Omaha", "Omaha", "Omaha", "Omaha"), "Temp" = c(12,15, 16,17,32,34,36,37,8,6,3,4))

ui <- fluidPage(
  inputPanel(selectInput("PlotCity",
                         label = "Select City",
                         choices = citylist)),
  
  plotOutput(outputId = 'minplot')
)

server <- function(input, output) {
 
  plotmin <- as.data.frame(subset(wxdata, City == input$PlotCity))

  minplot <- ggplot(data from plotmin DF)

}

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

我被困在这个问题上,我愿意打赌这是一个简单的解决方案,但我无法弄清楚反应性"东西.

I'm stuck on this and I'm willing to bet its a simple solution but I can't figure out the "reactive" thing.

无论如何感谢您的帮助

罗布

推荐答案

citylist 应该是一个向量,而不是一个数据框.在定义 wxdata 之后尝试定义 citylist <- unique(wxdata$City).

citylist ought to be a vector, not a data frame. Try defining citylist <- unique(wxdata$City) after you define wxdata.

我认为反应性错误是因为你没有调用 renderPlot().

I think the reactive error is because you haven't called renderPlot().

如果将绘制绘图(或其他计算)的逻辑分离到它们自己的函数中,我发现 Shiny 应用程序最容易推理,以便服务器代码仅用于渲染.

I find Shiny apps easiest to reason about if you separate the logic for drawing plots (or other calculations) into their own functions, so that the server code is only about rendering.

这是我的仪表板版本.

library(dplyr)
library(ggplot2)
library(shiny)

wxdata <- data.frame(
  City = rep(c("Abilene", "Amarillo", "Omaha"), each = 4), 
  Temp = c(12, 15, 16, 17, 32, 34, 36, 37, 8, 6, 3, 4)
)
citylist <- unique(wxdata$City)

draw_plot <- function(city_to_filter_by) {
  filtered_wx <- wxdata %>%
    filter(City == !!city_to_filter_by)
  ggplot(filtered_wx, aes(Temp)) +
    geom_histogram()
}

ui <- fluidPage(
  inputPanel(
    selectInput(
      "PlotCity",
      label = "Select City",
      choices = citylist
    )
  ),
  plotOutput('minplot')
)

server <- function(input, output) {
  output$minplot <- renderPlot(draw_plot(input$PlotCity))
}

shinyApp(ui = ui, server = server)

这篇关于用于过滤数据框的闪亮下拉菜单选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆