0

It's my first time create a web app. I had some problems about subsetting the data under shiny server. It's a large dataset. I want to subset the data by the conditions we enter in the UI. But I failed.

It shows "Error in eval: attempt to apply non-function". Could someone help me with it? Thank you so much. My ultimate goal is to build the app to subset data through a large dataset and apply the calculation on the subset. Finally, present the result through the shiny app.

I have coded the calculation process and UI. How can I combine those? Do we have to code everything under server? Can I just place some code under the global environment? Will the global environment have connection with the server? Thank you.

Attached is the picture of my code. How could I revise it to make it work?Thanks!!!!!

 server <- function(input,output){
 sex <- reactive({sex <- input$sex})
 impairment <- reactive({impairment <- input$impairment})
 sub1<- reactive({subset(a2e,Sex == input$sex() & AVS.Impairment == 
 input$impairment() )})


 output$distPlot <- renderPlot(plot{sub1()$Age})

 }

 shinyApp(ui=ui,server=server)

I wanted to subset the data based on sex and impairments. Just to check if it is working, I chose to plot random column in the dataset. But it didn't work. Please help me. :)

1 Answers1

0

First, I don't think you need sex <- reactive({sex <- input$sex}) when you can just refer to input$sex in your other reactives. (Even if you do, it could just be sex <- reactive(input$sex), but that just looks unnecessary. Maybe I'm missing something though.)

Anyhow, I think the problem is likely the input$sex() in the sub1<- line. You probably just need input$sex, or if there is some reason you need to use the reactive function then sex(), but here it looks like you combined the two ideas. Similar with input$impairment(), it should just be input$impairment.

Brian Stamper
  • 2,143
  • 1
  • 18
  • 41
  • Thank you Brian. It seems work. I just did what you told me. But then it shows "insufficient observations" error. I am very confused about the global environment and the server relationship. Do you have any suggestions for me? Thank you so much for your response. – Alex Fischer Apr 30 '18 at 19:39
  • I'm not really sure what you mean by the global environment in this context (although there is such a thing in R, I don't see how it applies here). It sounds like your example data just doesn't have enough points to produce a plot? Subsetting data based on inputs is a pretty commonly asked about task, an example is in the accepted answer to this question: https://stackoverflow.com/questions/48548573/shiny-reactive-subset – Brian Stamper Apr 30 '18 at 19:45
  • Perhaps you are talking about scoping? Where you define things affects where they are available. See http://shiny.rstudio.com/articles/scoping.html – Brian Stamper Apr 30 '18 at 19:49