dismal-audience
dismal-audience

Reputation: 27

Issue with changing a non-reactive dataframe using reactive elements

I am working with R Shiny, and this is the problem that I am facing:

I have a dataset (named dataset) that is non-reactive. The user of my shiny app will input a value that denotes the percentage of rows that will be affected.

For example, if the user enters "20", then my server will take a sample of 20% of my dataset's rows, modify 1 column in the row, and put this changed row back into the dataset.

I would also like to eventually print a summary of the original dataset, and then a summary of the changed dataset.

This is what I have done (the pop is a user input value):

#the number of rows I want to change
popint1 <- reactive({round(input$pop * 0.01 * nrow(dataset)})

#then get the summary of the original dataset
orig_summary <- reactive({dataset %>%
          group_by(age_group) %>%
          summarise(original_cvd_count = sum(as.numeric(s723d)),
                    meanSBP = mean(sb16s, na.rm = T),
                    meanDBP = mean(sb16d, na.rm = T)) %>%
          mutate(new_cvd_count = newRisk(meanSBP, meanDBP) * original_cvd_count)
      })


#then change the 'sb16s' column in a random sample of rows and put the changed rows back
env <- reactive({
        req(dataset)

        dataset[sample(nrow(dataset), popint1()), 'sb16s'] <- dataset[sample(nrow(dataset), popint1(), seed), 'sb16s'] + 5
      
})


#now get the new summary
int3_summary<- reactive({dataset %>%
        group_by(age_group) %>%
        summarise(original_cvd_count = sum(as.numeric(s723d)),
                  meanSBP = mean(sb16s, na.rm = T),
                  meanDBP = mean(sb16d, na.rm = T)) %>%
          mutate(new_cvd_count = newRisk(meanSBP, meanDBP) * original_cvd_count)
      })

output$summary1 =  renderTable({orig_summary()})
output$summary2 =  renderTable({int3_summary()})

But my summary1 and summary2 are shown as the same (talking about the meanSBP column, it should be higher in summary2 since I added 5 to the rows). What mistake am I making and how do I correct it?

Upvotes: 0

Views: 69

Answers (1)

Tom
Tom

Reputation: 592

env returns the subset. So, in your reactive summary function you should use env instead of dataset. To give you an idea:


summary_func <- function(df){
         df %>%
          group_by(age_group) %>%
          summarise(original_cvd_count = sum(as.numeric(s723d)),
                    meanSBP = mean(sb16s, na.rm = T),
                    meanDBP = mean(sb16d, na.rm = T)) %>%
          mutate(new_cvd_count = newRisk(meanSBP, meanDBP) * original_cvd_count)
})

env <- reactive({
        req(dataset)
        popint1 <- round(input$pop * 0.01 * nrow(dataset)
        dataset[sample(nrow(dataset), popint1), 'sb16s'] <- dataset[sample(nrow(dataset), popint1, seed), 'sb16s'] + 5        
        return(dataset)
})

int3_summary<- reactive({
        env %>%
         summary_func
})


output$summary1 =  renderTable({dataset %>% summary_func})
output$summary2 =  renderTable({int3_summary()})

I couldn't test the code, so there may be some bugs. Please provide a working minimal example next time.

Upvotes: 2

Related Questions