首页 文章

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

提问于
浏览
0

Issues between inputs and plot output

嗨,

我正在测试一个基本的ShinyApp,在那里我可以生成按地理和服务类型细分的商业服务图 .

我的想法是希望用户使用三个下拉菜单输入,每个输入取决于之前的选择,对数据进行子集化,然后在ggplot中输出 .

但是,我在将输入连接到绘图输出时遇到问题(见下文) . 输入在选择时工作正常和反应,但我无法弄清楚如何将其与情节联系起来,我感觉我没有使用正确的数据源(但不知道如何确保它是) . 此外,我不熟悉如何添加第三个过滤器(用于“服务”),因为我不知道如何首先链接我的数据源 .

对不起,这可能很简单,但一些帮助将非常感激 .

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)

enter image description here

1 回答

  • 1

    很难说应该如何在应用程序中过滤提供的数据,但这段代码至少会运行并且是交互式的 . 希望从那里你可以弄清楚如何调整数据集 .

    正如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)
    

相关问题