尝试在Plotly调用中使用Shiny输入静态调用子集在完整数据帧上

时间:2017-10-31 17:21:55

标签: shiny plotly

以下代码在某种程度上起作用 - 它绘制了一个图形,但它显然不是原始数据框的子集,而是根据所有指标,年份等进行绘图。对于为什么有任何想法?我已尝试使用select进行反馈,使用select(来自dplyr的过滤器,使用“”输入$指示符等等。我花了大约4个小时查看这里的各种建议,Plotly和Shiny网站,没有解决方案。开始怀疑我我将永远掌握这一点。

    #
    # This is a Shiny web application. You can run the application by clicking
    # the 'Run App' button above.
    #

    library(dplyr)
    library(shiny)
    library(fingertipsR)
    library(viridis)
    library(plotly)

    #Import QOF indicators
    setwd("/Users/ianbowns/Documents/R/ShinyFT")
    dat <- readRDS("data")
    my.df <- as.data.frame(dat)
    # Define UI for application that draws boxplot
    ui <- fluidPage( 
   # Application title
   titlePanel("FingerTips QOF Prevalences"),

   # Input for year, area and indicator 
   sidebarLayout(
      sidebarPanel(
                selectInput(inputId = "indicator",
                          label = "Choose indicator:",
                          choices = levels(my.df$IndicatorName),
                          selected = "Hypertension: QOF prevalence (all ages)"),              
                selectInput(inputId = "areatype",
                          label = "Type of area:",
                          choices = levels(my.df$AreaType),
                          selected = "County & UA"),              
                selectInput(inputId = "year",
                          label = "Choose a year:",
                          choices = levels(my.df$Timeperiod),
                          selected = "2015/16")),

      # Show a plot of the generated distribution
      mainPanel(
         plotlyOutput("bPlot", height = 500, width = 1000)
      )
    ))

    # Define server logic required to draw a histogram
    server <- function(input, output) {

    # draw the boxplot
        output$bPlot <- renderPlotly({
                plot_ly(data = subset(my.df, my.df$IndicatorName == 
    input$indicator & my.df$AreaType == input$areatype &
                        my.df$Timeperiod == input$year), y = my.df$Value, color 
    = my.df$ParentName, type = "box",
                         colors = viridis_pal(alpha = 1, begin = 0, end = 1, 
    direction = -1, option = "D")(3)) %>% 
                        layout(title = input$indicator, titlefont = list(family 
    = "Helvetica", size = 16),
                        xaxis = list(type = "category", tickfont = list(family = 
    "Helvetica", size = 8)),
                        yaxis = list(title = "Prevalence (%)", titlefont = 
    list(family = "Helvetica", size = 12)))})
                }
    # Run the application
    shinyApp(ui = ui, server = server)

0 个答案:

没有答案