简体   繁体   English

Shiny中的动态过滤器和无功图

[英]Dynamic filters and reactive plot in Shiny

Issues between inputs and plot output 输入和绘图输出之间的问题

Hi, 嗨,

I'm testing out a basic ShinyApp where I can generate a plot of commercial services broken down by geography and service type. 我正在测试一个基本的ShinyApp,可以在其中生成按地理位置和服务类型细分的商业服务图。

The idea is I want the user to use three drop-down menu inputs, each dependent upon the previous selection, to subset the data, which then gets output in a ggplot. 我的想法是,我希望用户使用三个下拉菜单输入(分别取决于先前的选择)来对数据进行子集化,然后将其输出到ggplot中。

However, I'm having issues connecting the inputs to the plot output (see below). 但是,在将输入连接到绘图输出时遇到问题(请参阅下文)。 The inputs are working fine and reactive when selected, but I can't work out how to link that to the plot, I get the feeling I'm not using the right data source (but have no idea how to ensure it is). 选择输入后,它们可以正常工作并具有反应性,但是我无法弄清楚如何将其链接到绘图,我感到我没有使用正确的数据源(但不知道如何确保正确)。 Furthermore, I'm not familiar with how I would go about adding a third filter (for "service") seeing as I don't know how to link my data source in the first place. 此外,我不知道如何添加第三个过滤器(用于“服务”),因为我一开始不知道如何链接数据源。

Sorry this is probably simple, but some help would be really appreciated. 抱歉,这可能很简单,但是一定会有所帮助。

UI UI

 #Data
 Test <- dataframe(
           Geography1 = c("Region","Local Authority","County"...),
           Geography2 = c("North West","Aldershot","Cheshire"...),
           Service = c("Shop","Cafe","Library"...),
           Overall_rating = c("Awesome","Good","Fantatstic"...),
           Locations = c(4000, 1300, 1700...)
  )

 #SHINY APP
 ui <- fluidPage(
  titlePanel("Tool"),
   sidebarLayout(
    sidebarPanel(
     uiOutput("geography1"),
     uiOutput("geography2"),
     uiOutput("service")),

  mainPanel(
     plotOutput("plot", height = "400px"))
     )
  )

Server 服务器

   server <- function(input, output) {

 output$geography1 = renderUI({
    selectInput(inputId = "geog1",
              label = "Geography 1:", 
              choices = as.character(unique(Test$Geography1)),
              selected = "Region")
  })

 output$geography2 = renderUI({

   datasub <- Test[Test$Geography1 == input$geog1, "Name"]

     selectInput(inputId = "geog2", 
                 label = "Geography2:", 
                 choices = unique(datasub),
                 selected = unique(datasub)[1])
  })

  output$service = renderUI({

     datasub2 <- unique(datasub)

     selectInput(inputId = "service",
                 label = "Service type:",
                 choices = unique(...),
                 selected = unique(...)[1])
   })

  output$plot = renderPlot({

    ggplot(datasub2(),aes(x = Overall_rating, y = Locations, fill= Overall_rating))+
     geom_bar(stat = "identity")

   })
 }

  shinyApp(ui, server)

在此处输入图片说明

It's hard to tell how the provided data is supposed to be filtered in the app but this code will at least run and be interactive. 很难说出应如何在应用程序中过滤提供的数据,但是此代码至少会运行并具有交互性。 Hopefully from there you can figure out how to adjust the dataset. 希望您可以从那里弄清楚如何调整数据集。

As BigDataScientist said one fault is that you're not using a reactive dataset. 正如BigDataScientist所说,一个错误是您没有使用反应性数据集。

#Data
Test <- data.frame(
  Geography1 = c("Region","Local Authority","County"),
  Geography2 = c("North West","Aldershot","Cheshire"),
  Service = c("Shop","Cafe","Library"),
  Overall_rating = c("Awesome","Good","Fantatstic"),
  Locations = c(4000, 1300, 1700)
)

#SHINY APP
ui <- fluidPage(
  titlePanel("Tool"),
  sidebarLayout(
    sidebarPanel(
      uiOutput("geography1"),
      uiOutput("geography2"),
      uiOutput("service")),

    mainPanel(
      plotOutput("plot", height = "400px"))
  )
)

server <- function(input, output) {

  output$geography1 = renderUI({
    selectInput(inputId = "geog1",
                label = "Geography 1:", 
                choices = as.character(unique(Test$Geography1)),
                selected = "Region")
  })

  datasub <- reactive({
    Test[Test$Geography1 == input$geog1,]
  })

  output$geography2 = renderUI({
    selectInput(inputId = "geog2", 
                label = "Geography2:", 
                choices = unique(datasub()[,"Geography2"]),
                selected = unique(datasub()[,"Geography2"])[1])
  })

  datasub2 <- reactive({
    datasub()[Test$Geography2 == input$geog2, ]
  })

  output$service = renderUI({
    selectInput(inputId = "service",
                label = "Service type:",
                choices = unique(datasub2()[,"Service"]),
                selected = unique(datasub2()[,"Service"])[1])
  })

  datasub3 <- reactive({
    datasub()[Test$Service == input$service, ]
  })

  output$plot = renderPlot({
    ggplot(datasub3(),aes(x = Overall_rating, y = Locations, fill= Overall_rating))+
      geom_bar(stat = "identity")

  })
}


shinyApp(ui, server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM