使用Plotly图表向Shiny应用程序添加自定义标题

时间:2018-12-07 17:47:09

标签: r shiny plotly

我有以下Shiny应用程序,使用带有5个下拉选项的Plotly显示时间序列折线图。这可行,但是我想在每个选择的顶部附近添加自定义标题。有没有简单的方法可以添加到此应用程序中,如果可以,我应该在哪里添加它?

#Import libraries
library(shiny)
library(plotly)
library(tidyr)
library(bindrcpp)

# Define UI for application that draws a histogram
ui <- bootstrapPage(

  selectInput(inputId = "n_breaks",
              label = "Select Data:",
              choices = c("Distinct_FLD","Not_On_MM","API_Call_Count","Cost","CACHE_Count"),

              selected = "Distinct_FLD"),

  plotlyOutput(outputId = "main_plot", height = "600px")
)

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

  #Import data
  df = read.csv("API_Statistics.csv")  

  output$main_plot <- renderPlotly({

    if (input$n_breaks == "Distinct_FLD") {
      #Create variable X getting rid of NA values
      x <- df$Date[!is.na(df$Distinct_FLD)]
      #Create variable Y getting rid of NA values
      y <- df$Distinct_FLD[!is.na(df$Distinct_FLD)]
      #Plotly plot
      plot_ly(x = x, y = y, type = 'scatter', mode = 'lines') 
    }
    else if (input$n_breaks == "Not_On_MM") {
      #Create variable X getting rid of NA values
      x <- df$Date[!is.na(df$Not_On_MM)]
      #Create variable Y getting rid of NA values
      y <- df$Not_On_MM[!is.na(df$Not_On_MM)]
      #Plotly plot
      plot_ly(x = x, y = y, type = 'scatter', mode = 'lines')  
    }
    else if (input$n_breaks == "API_Call_Count") {
      #Create variable X getting rid of NA values
      x <- df$Date[!is.na(df$API_Call_Count)]
      #Create variable Y getting rid of NA values
      y <- df$API_Call_Count[!is.na(df$API_Call_Count)]
      #Plotly plot
      plot_ly(x = x, y = y, type = 'scatter', mode = 'lines')  
    } 
    else if (input$n_breaks == "Cost") {
      #Create variable X getting rid of NA values
      x <- df$Date[!is.na(df$Cost)]
      #Create variable Y getting rid of NA values
      y <- df$Cost[!is.na(df$Cost)]
      #Plotly plot
      plot_ly(x = x, y = y, type = 'scatter', mode = 'lines')  
    }
    else if (input$n_breaks == "CACHE_Count") {
      #Create variable X getting rid of NA values
      x <- df$Date[!is.na(df$CACHE_Count)]
      #Create variable Y getting rid of NA values
      y <- df$CACHE_Count[!is.na(df$CACHE_Count)]
      #Plotly plot
      plot_ly(x = x, y = y, type = 'scatter', mode = 'lines')  
    }
  })   
}
# Run the application 
shinyApp(ui = ui, server = server)

是否有一种简单的方法可以向此应用程序中的每个图表添加自定义标题,如果可以,我应该在哪里添加它?

1 个答案:

答案 0 :(得分:0)

我去了:

h_(textOutput("Title"))