13

The following code is my Shiny ui:

library(shiny)
shinyUI(fluidPage(

titlePanel("All Country Spend"),

  sidebarLayout(
    sidebarPanel(  selectInput("split", 
        label = "Choose Fill For the Chart",
        choices = c("Action.Obligation","Action_Absolute_Value"),
      selected = "Action.Obligation"
        )
            ),
    mainPanel(plotOutput("SpendChart"))
)
))

And the following is the server code:

library(shiny)
library(ggplot2)

shinyServer(function(input, output) {

spend <- read.csv("data/Ctrdata.csv")
output$SpendChart <- renderPlot({

    Country <- spend$Principal.Place.of.Performance.Country.Name
    ggplot(spend, aes(x = Country, y = input$split)) + geom_bar(stat = "identity")

})

})

Every time I run it I get the following error:

"Error in eval(expr, envir, enclos) : object 'input' not found"

I am trying to render a simple bar chart that will toggle between the net and absolute value of contract spending in each country, but it won't recognize my input named "split" from the selectInput box.

Here is a sample of my data frame:

data.frame(Country = c("Turk", "Turk", "Saudi", "Saudi", "Ger", "Ger"),
Action.Obligation = c(120,-345,565,-454, 343,-565),
Action_Absolute_Value = c(120,345,565,454,343,565))
coip
  • 1,312
  • 16
  • 30
user2907249
  • 839
  • 7
  • 14
  • 32

2 Answers2

6

The problem is with ggplot evaluating the variable in the context of the data frame provided, spend in your case. What you want is:

ggplot(spend, aes_string(x = "Country", y = input$split))

So your working server.R code is:

library(shiny)
library(ggplot2)

shinyServer(function(input, output) {

spend <- data.frame(Country = c("Turk", "Turk", "Saudi", "Saudi", "Ger", "Ger"),
                    Action.Obligation = c(120,-345,565,-454, 343,-565),
                    Action_Absolute_Value = c(120,345,565,454,343,565))

    output$SpendChart <- renderPlot({


        ggplot(spend, aes_string(x = "Country", y = input$split)) +
            geom_bar(stat = "identity")

    })

})

Obviously, you can replace the spend df with your CSV import.

Konrad
  • 17,740
  • 16
  • 106
  • 167
  • Thanks but I have another problem now. I can't subset the data before feeding it to ggplot using a selectInput box. See my subsequent question for details. – user2907249 Feb 12 '15 at 21:58
  • @user2907249 you would have to elaborate on your problem for someone to help. If the problem is new just start a new question. On principle, you would use `subset` command and refer required values from the `input$whatever`. As you are importing your data from CSV and you want to subset the strings it may be worthwhile to consider whether any cleaning of the data is necessary. In order to ensure that you are referencing your R objects properly have a look at [this article](http://shiny.rstudio.com/articles/scoping.html) on scoping rules in Shiny. – Konrad Feb 13 '15 at 15:59
  • I do not understand what is difference in this code and the code in the question. Can you explain me? – scarface Mar 18 '18 at 20:10
  • 1
    @scarface it uses aes_string to reference column passed as string. – Konrad Mar 18 '18 at 20:13
4

I've solved this issue with "<<-". I don't know for sure, but it is related to global environment:

output$SpendChart <- renderPlot({

choice <<- input$split

    Country <- spend$Principal.Place.of.Performance.Country.Name
    ggplot(spend, aes(x = Country, y = choice)) + geom_bar(stat = "identity")

})