1

So here's my code below, that I can't get Shiny to show the data in the graph. It just shows the column name, and not the data. Any suggestions?

The data are just showing up in a row in the graph instead of being correctly plotted. I've tried going through the examples in the R Shiny cheat sheet, but no luck.

budget_d <- tribble(~opdate, ~r.pd.bdgt, ~r.pt.bdgt, ~r.au.bdgt,
                    ymd("2018-05-31"), 81, 73, 65,
                    ymd("2018-06-11"), 86, 79, 69,
                    ymd("2018-06-30"), 89, 76, 67,
                    ymd("2018-07-21"), 82, 72, 65,
                    ymd("2018-08-01"), 81, 73, 63,
                    ymd("2018-08-08"), 83, 75, 67,
                    ymd("2018-11-17"), 84, 74, 66)

require(shiny)
require(tidyverse)
require(lubridate)


ui <- fluidPage(
    dateRangeInput(
                    inputId = "period",
                    label = "Choose a Period",
                    start = ymd("2018-04-01"),
                    end = ymd("2018-09-01"),
                    min =  ymd("2018-01-01"),
                    max = ymd("2018-12-31")
                   ),
    selectInput(
                    inputId = "elements",
                    label = "Select an Element",
                    choices = c("Pd Recovery" = "r.pd.bdgt",
                                "Pt Recovery" = "r.pt.bdgt",
                                "Au Recovery" = "r.au.bdgt")
    ),
    plotOutput(
        outputId = "hist"
    )
)



server <- function(input, output){
    output$hist <- renderPlot({
        ggplot(budget_d, aes(opdate, input$elements)) +
                    geom_point() +
                   xlim(input$period)
    })
}



shinyApp(ui = ui, server = server)
Florian
  • 24,425
  • 4
  • 49
  • 80
Reuben Mathew
  • 598
  • 4
  • 22
  • Please provide *sample* data within the question, not as a link, for at least two reasons: links can die, leaving this question unreproducible; and some people are hesitant to click on innocent-looking links to download arbitrary files. Even if it's "obviously" a clean/safe file, it's still a level of indirection that us lazy people (and perhaps other answerers, too) will likely not take. – r2evans Dec 16 '17 at 21:30
  • 2
    Good point. I've edited the question to reflect your concerns. I appreciate the heads up. – Reuben Mathew Dec 16 '17 at 21:47

1 Answers1

1

See here. input$elements is variable, and if you want to use a variable you should use aes_string instead of aes. Working example with sample data:

require(shiny)
require(tidyverse)
require(lubridate)
library(ggplot2)



ui <- fluidPage(
  selectInput(
    inputId = "elements",
    label = "Select an Element",
    choices = colnames(iris)
  ),
  plotOutput(
    outputId = "hist"
  )
)



server <- function(input, output){
  output$hist <- renderPlot({

    # This works.
    ggplot(iris, aes_string("Sepal.Length", input$elements)) +
      geom_point()

    # This does not work.
    # ggplot(iris, aes(Sepal.Length, input$elements)) +
    #   geom_point()
  })
}

shinyApp(ui,server)

Hope this helps!

Florian
  • 24,425
  • 4
  • 49
  • 80