Mal_a
Mal_a

Reputation: 3760

R Shiny Interactive plot title for ggplot

I am trying to create Shiny App which is able to display interactive plot title (dependent on the choosen value for x axis)

Very simple example:

library(shiny)
library(DT)
library(ggplot2)

x <- as.numeric(1:1000000)
y <- as.numeric(1:1000000)
z <- as.numeric(1:1000000)
data <- data.frame(x,y, z)

shinyApp(
  ui = fluidPage(selectInput(inputId = "yaxis", 
                             label = "Y-axis",
                             choices = list("x","y","z"), 
                             selected = c("x")),
  dataTableOutput('tableId'),
                 plotOutput('plot1')),
  server = function(input, output) {    
    output$tableId = renderDataTable({
      datatable(data, options = list(pageLength = 10, lengthMenu=c(10,20,30)))
    })
    output$plot1 = renderPlot({
      filtered_data <- data[input$tableId_rows_all, ]
      ggplot(data=filtered_data, aes_string(x="x",y=input$yaxis)) + geom_line()  
    })
  }
)  

I have tried this code:

ggtitle("Line plot of x vs",input$yaxis)

It was not working, plot has not been displayed, giving me an Error:

Warning: Error in ggtitle: unused argument (input$yaxis)

[IMPORTANT]

using ggtitle(input$yaxis) gives me an interactive title, however i need to build up a sentence (like: Line plot of x vs input$yaxis), in which the reactive argument (input$yaxis) is a part of it!

Thanks for any help!

Cheers

Upvotes: 4

Views: 4424

Answers (1)

DeveauP
DeveauP

Reputation: 1237

Change:

ggtitle("Line plot of x vs",input$yaxis)

To

ggtitle(paste("Line plot of x vs",input$yaxis))

As the error suggests, you have too many arguments passed to the ggtitle function, paste will create a single character out of your two inputs, with a space in between. You can vary the separation between the two with sep =.

Upvotes: 9

Related Questions