1

I've been trying to develop an interactive boxplot with selective input in Shiny.

current code:

library(shiny)

shinyUI(fluidPage(

  titlePanel("Sample 1"),

  sidebarLayout(
    sidebarPanel(
      selectInput("p", "Choose your salaries", choices = c("low"='a',"mid"='b',"high"='c',"riches!"='d'), selected = 4)
    ),
    mainPanel(
      plotOutput("boxplot")
    )
  )


))



library(shiny)
read.csv("Salaries.csv")

Categories <- cut (Salaries$TotalPay, breaks = c(0,36466,73678,104359,567595), labels = c("low","mid","high","riches!"))

shinyServer(function(input, output){

  output$boxplot <- renderPlot({

    if(input$p=='a'){
      i<"1"

    }

    if(input$p=='b'){
      i<-"2"
    }

    if(input$p=='c'){
      i<-"3"
    }

    if(input$p=='d'){
      i<- "riches!"
    }


    boxplot(TotalPay~Categories[i])

  })
})

I can't get the boxplot to react to the selection made in the UI. I suspect it has to do with the levels as when I call:

> Categories["riches!"]
[1] <NA>
Levels: low mid high riches!

' Do i need to add factors to these? Or am I missing the point entirely? Thanks in advance!

1
  • Are you assigning your csv file to a dataframe? Seems to me you are missing: Salaries <- read.csv("Salaries.csv") Commented Dec 19, 2017 at 20:22

1 Answer 1

3

Have a look how to access the column by name. Example below is with mtcars dataset

library(shiny)

ui <- fluidPage(
  selectInput("p","p",choices = names(mtcars)),
  plotOutput("myplot"))

server <- function(input, output, session) {

  output$myplot <- renderPlot({
    boxplot(mtcars[,input$p])
    })
}

shinyApp(ui, server)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Pork, I'll try it out and get back to you
Hey Pork, Tried it and it gave me: error $ operator is invalid for atomic vectors.. ` library(shiny) read.csv("Salaries.csv") Categories <- cut (Salaries$TotalPay, breaks = c(0,36466,73678,104359,567595), labels = c("low","mid","high","riches!")) shinyServer(function(input, output){ output$boxplot <- renderPlot({ boxplot(TotalPay[,Categories$p]) }) }) `
U getting errors because u have no column called "Categories$p", your subset needs to be something like "low" or "mid" as you have those column names!
Quick question, the Dataset has the following columnames: '> names(Salaries) [1] "Id" "EmployeeName" "JobTitle" "BasePay" "OvertimePay" [6] "OtherPay" "Benefits" "TotalPay" "TotalPayBenefits" "Year" [11] "Notes" "Agency" "Status" ' Should I then use Categories is a devision of TotalPay as you probably already noticed, but TotalPay is a column in Salaries. How do I get the Categories out of TotalPay then? As low and high (i think) aren't colums either. Do I setup a new dataframe?
want me to post it here? its pretty large if anything its from here: kaggle.com/kaggle/sf-salaries
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.