R-使用Shiny

时间:2018-07-25 10:39:23

标签: r shiny plotly reactive-programming

你好

我正在R中构建一个Shiny App,使用Plotly构建交互式绘图。

用户可以选择过滤我的数据:(1)按地理位置类型; (2)按位置(与地理选择有关的选择); (3)服务类型。之后,用户可以(理论上)选择替代的Y变量以针对等级(x轴)进行绘制:酒店床位数或位置。

NB:我以前一直在使用ggplot2,但找不到使用ggplotly()命令添加工具提示的令人满意的解决方案。

目前,我在Plotly中遇到两个问题,其中一个很简单,另一个却让我感到困惑:

  1. Plotly似乎将Loc_count变量视为一个因素,尽管它被编码为数字(参见图1)。我希望Plotly将Loc_count绘制为数字,并因此生成Y轴上特定地理位置而不是二进制0-1比例的位置数量的计数。

  2. 另一个问题是,当我在Y轴上选择“床”时,Plotly不会显示我的每个评分类别的所有观测值(请参见图2)。例如,在图像2中,似乎有xxx张床被评为“好”,而在数据中则有xxx张床。似乎正在发生的情况是,Plotly仅显示一个观测值(即旅馆)的数据。不知道为什么。

由于原始数据集的大小和内容,我只能用不同的变量名提供数据结构的模拟样本:

数据集

Data <- data.frame(Loc_count = c(1,1,1,1,1,1,1,1,1,1),
               Hotel = c("A","B","C","D","E","F","G","H","I","J"),
               Region = c(rep("North",10)),
               Authority = c("Bolton","Manchester","Bolton","Bolton","Bradford","Manchester","Preston","Bolton","Preston","Manchester"),
               Town = c("Milton","Warrington","Milton","Leigh-on-Sea","Basildon Town","Gadden","Manchester Central","Marm Central","Marsh","Lemington Spa"),
               Service = c("Motel","Hotel","Motel","Hotel","Hotel","Bedsit","Hotel","Hotel","Hotel","Hotel"),
               Rating = c("Good","Excellent","Excellent","Good","Good","Good","Poor","Adequate","Adequate","Good"),
               Beds = c(100,45,34,55,25,69,55,150,50,50)
               )

UI

       ui <- fluidPage(

   # App title
   titlePanel("Hotel analyser"),

   # Sidebar layout with input and output definitions
   sidebarLayout(
      sidebarPanel(id = "sidebar",
                   # Input 1: Select geography
                   uiOutput("Geography"),
                   # Input 2: Select location
                   uiOutput("Location"),
                   # Input 3: Select service type
                   uiOutput("Service"),
                   # Input: Select y variables
                   radioButtons("y", "Choose Y variable",
                                c("Beds",
                                  "Locations"),
                                selected = "Locations")
      ),

      # Main panel for displaying outputs ----
      mainPanel(
         # Output: Tabset w/ plot, summary, and table ----
         tabsetPanel(type = "tabs",
                     tabPanel("Bar Chart", plotlyOutput("plot", height = "450px")),
                     tabPanel("Underlying data", dataTableOutput("table"))
         )
      )
   )
)

服务器

        server <- function(input, output) {

      # Reactive output/inputs and datasets
      #Geography selection output
      output$Geography = renderUI({
         selectInput(inputId = "Geography",
                     label = "Select geography:", 
                     choices = c("Region","Authority","Town"),
                     selected = "Region")
      })

      Geography <- reactive(switch(input$Geography,
                                   "Region" = Data$Region,
                                   "Authority" = Data$Authority,
                                   "Town" = Data$Town))

      #Location selection output
      output$Location = renderUI({
         selectInput(inputId = "Location", 
                     label = "Select location:", 
                     choices = unique(Geography()),
                     selected = unique(Geography())[1]) 
      })

      datasub2 <- reactive({
         req(input$Location)
         filter(Data, Geography() %in% input$Location)
      })

      #Service selector
      output$Service = renderUI({

         selectInput(inputId = "Service",
                     label = "Select Service Type:",
                     choices = unique(datasub2()[,"Service"]),
                     selected = unique(datasub2()[,"Service"])[1])
      })

      datasub3 <- reactive({
         req(input$Location)
         filter(datasub2(), Service %in% input$Service)

      })

      y <- reactive(switch(input$y, 
                           "Beds" = datasub3()$Beds,
                           "Locations" = datasub3()$Loc_count))

      #  Bar chart
      output$plot = renderPlotly({

         plot_ly(
            datasub3(), x = ~Rating, y = ~y(), type = "bar") %>%
            layout(xaxis = list(title = "X", showgrid = F), yaxis = list(title = "Y"))
      })

      # Generate an data table view of the data ----
      output$table <- renderDataTable({
         datasub3()[,1:7]
      }) 

   } 

# Create Shiny app ----
shinyApp(ui, server)

enter image description here

图片1:您可以看到Plotly正在将我的列理算(Loc_count)作为一个因素

enter image description here

图片2:尽管该图显示了北部的150家合适的酒店,但数据显示那里有200家。似乎只显示了一个观测值的数据

0 个答案:

没有答案