3

I have a data set that looks like the following:

Win    Gender    Score (1-4)    Weight     SHE
1      F         2              140        Y
0      M         3              155        Y
1      M         4              134        N

I am creating a Shiny App that will create side by side bar charts for the rows that belong to the different scores (4 groups). I want the user to be able to select the variable that will be on the x-axis (either Win, Gender of SHE), and have the charts update to reflect the counts of that variable within each group.

I have tried the following code in server.R:

shinyServer(function(input, output){
     output$plotOne <- renderPlot({
ggplot(data=GroupOne, aes(x=input$var))+geom_bar(stat='bin')
})
})

'var' is the name that I have specified for the variable the user selects based on the radio button input in ui.R. The error message I am getting is that input is not found.

I have also tried the above code, but with

histogram(~input$var, data=GroupOne). 

This resulted in errors about negative length vectors and other things.

6
  • is 'GroupOne' is the above data set or just a subset of it? Commented Jun 2, 2015 at 13:59
  • It is the subset of the data where Score==1. Commented Jun 2, 2015 at 13:59
  • which is empty for this specific example. does the problem persists for score==2? Commented Jun 2, 2015 at 14:03
  • The problem persists for all of the groups. The data set is larger, I just included the first three rows as an example, but all groups contain multiple entries. Commented Jun 2, 2015 at 14:14
  • Can you say explicitly what the error says? The "other things" could be significant. And when you say that it says "input is not found," do you mean the input to the function, or the object "input"? Commented Jun 2, 2015 at 17:48

1 Answer 1

2

Try using a reactive function to tie the input into your data frame:

server <- function(input, output) {

  data <- reactive({
if ( "Win" %in% input$var) return(your_data_frame$Win)
if ( "Gender" %in% input$var) return(your_data_frame$Gender)
if ( "Score" %in% input$var) return(your_data_frame$Score)
if ( "Weight" %in% input$var) return(your_data_frame$Weight)
if ( "SHE" %in% input$var) return(your_data_frame$SHE)
  })

  output$plotOne <- renderPlot({
ggplot(data=GroupOne, aes(x=data()))+geom_bar(stat='bin')
  })
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.