Problem working with data in shiny

I am working on an app in Shiny for which I import a dataset from a csv to use for visualizations. It works for me to make a plot with ggplot because I specify the dataset, and I can print the dataset from inside the app (so I know it's uploading correctly).

I run into a problem when I want to do some data manipulation that involves the user input (outcome) before I put it into ggplot. Specifically, my dataset is called ace_data and a relelvant variable in that data set is ace_score, and my input variables are the names of the other variables in the ace_data data set. outcome is the label for the user input.

  freq <- reactive({
    freqs <- c()
    for (i in 0:4) {
      {freqs <- c(freqs,((sum(ace_data[ace_data$ace_score== i,]$input$outcome))/(sum(ace_data$ace_score == i))))
      }}
    return(freqs)
  })

When I run the app and print freq() I get a vector with all 0s which is incorrect. I have tried attach(ace_data) and ace_data$(input$outcome) but neither of those worked. Thank you!

Its not clear to me what you are trying to accomplish by calculating frequencies this way, but you need to reference your user input with ace_data[ace_data$ace_score== i,][[as.name(input$outcome)]] Here is an example with the iris dataset

input_outcome <- "Sepal.Length"
freqs <- c()
for (i in unique(iris$Species)) {
    freqs <- c(freqs,sum(iris[iris$Species == i,][[as.name(input_outcome)]])/sum(iris$Species == i))
}
freqs
#> [1] 5.006 5.936 6.588

Created on 2019-03-09 by the reprex package (v0.2.1)

1 Like

I copied your syntax and it fixed it, thank you @andresrcs!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.