0

I am quite new to R and have been trying to find a solution for my problem since weeks. I hope someone can help me.

1.I want to develop a shiny app in a dashboard, where the user can select values via selection_filter (e.g. out of the variable "age group" the value "40-49 years" and from "sex" the value "female"). Based on these selections, columns (e.g. column x,y, and z) from the original dataset will be aggregated. I already wrote a function using aggregate().

2.Based on the aggregated columns, new values shall be calculated (e.g. d=(x-y)/(z/2)).

3.The aggregated columns and the newly calculated values shall be displayed in a table to the user.

The function from 1)

aggreg.function <- function(a,b,c) {
  agg.data<- aggregate(cbind(x,y,z), shared_Cervix, sum, 
             subset=c(!AgeGroup %in% a & !Sex %in% b & !Edition %in% c))

  #Calculate new values
  agg.data$d<- agg.data$x+agg.data$y
  agg.data$f<- (agg.data$x+agg.data$y)/(agg.data$z/2)

  View(m.agg.data)
}

user_data<- reactive({
aggreg.function(input$AgeGroup, input$Sex, input$Edition)
  })

EDIT

Thanks for the recommendations. I change my code but now I struggle a bit with adding new columns. In total, I want to insert 17 new columns based on the filtered table (data_step2()). Is there a way to insert multiple columns at the same time. In my example: is it possible to combine data_step3 and data_step4?

ui<-fluidPage(
  selectInput("Age","Age:",sort(unique(Complete_test$Age))),
   selectInput("Raced","Race:",sort(unique(Complete_test$Race))),
    selectInput("Stage","Stage:",sort(unique(Complete_test$Stage))),
   selectInput("Grade","Grade:",sort(unique(Complete_test$Grade))),
   selectInput("Edition","Edition:",sort(unique(Complete$Edition))),
    DT::dataTableOutput("filtered.result")
  )

server = function(input, output) {

  data_step1 <- reactive({
   Complete%>% filter(Age %in% input$Age & Stage %in% input$Stage & Grade %in% input$Grade & Race %in% input$Race & Edition%in% input$_Edition)}) 

        data_step2 <- reactive({ 
    data_step1() %>% group_by(Age, Stage, Grade, Race, Edition, Year ) %>%  summarise(across(everything(), sum))
  })
        
        #is it possible to combine data_step3 and data_step_4 ?
       data_step3 <- reactive({
       data_step2() %>% mutate(xy=x+y)

       }) 
         
      data_step4 <- reactive({
       data_step3() %>% mutate(w=xy/(x2))

       }) 
         
          output$filtered.result <- DT::renderDataTable({ 
    data_step4() 
  })
}

      shinyApp(ui, server)
    ```
  • Welcome to SO. It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a working example your code which others could run and snippet of your data or some fake data. – stefan Dec 12 '22 at 09:51
  • This said: While `View` is useful in interactive sessions I wouldn't use it in a function. Perhaps you want `return(agg.data)` to return the result of your function. – stefan Dec 12 '22 at 09:53

1 Answers1

0

Here's something you might be able to do for step 1 of your question, although it's hard to tell what your data might look like and what you end result should look like. I'm assuming that you want to allow a user of your dashboard to select the AgeGroup and Sex variables to view data, so in the UI of the application, two selectInput functions were used to provide that functionality. For the server side, two reactive statements are used to filter the data as a user changes the inputs. In each one, an input is required and then the dataset is filtered by the input. Notice in the second reactive statement, "data_step1()" is used instead of "data_step1"; this allows the reactivity of these statements to continue to be updated as a user changes inputs.

For step 2 and 3, you'll want to use the "data_step2()" dataset to add the new columns and then you can use a function such as renderDataTable to display the output on your dashboard.

library(shiny)
library(dplyr)

ui <- fluidPage(
  selectInput("AgeGroup", "AgeGroup:", sort(unique(Data$AgeGroup))),
  selectInput("Sex", "Sex:", sort(unique(Data$Sex)))
)

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

data_step1 <- reactive({
  req(input$AgeGroup) # Require input
  data %>% filter(AgeGroup %in% input$AgeGroup)}) # Filter full dataset by AgeGroup input

data_step2 <- reactive({
  req(input$Sex) # Require input
  data_step1() %>% filter(Sex %in% input$Sex)}) # Filter full dataset by Sex input

# Do next steps with the filtered data_step2() dataset...

} 

# Run the application 
shinyApp(ui = ui, server = server)
mdc690
  • 48
  • 8
  • Hi Matthew, thank you very much for your help. I tried it again. But it still does not work. I added my new code. – user20755226 Dec 13 '22 at 14:09
  • If you're using reactive statements and passing a data frame through multiple operations, you need to have the paratheses attached to the name of the data frame. Something like "data_step1()" would work, while "data_step1" would not; see how the data_step2 reactive statement is set up for reference. – mdc690 Dec 14 '22 at 14:24
  • Thanks again for your hint. It took me a few days, but I could change my code. Now I would like to add several new columns to the filtered table (data_step2()). So that I don't have to program each time a new data_step for each new column, is it possible to insert several new columns at the same time? I edit my code above. I hope it helps to understand my question better. – user20755226 Dec 20 '22 at 11:29
  • Yes, you can just chain all of the mutate() statements together to create as many columns as you need in data_step3. Theoretically, you could do all of these operations in a single reactive statement, but I personally find it helpful to break it up a little so I can see what's happening a little easier. – mdc690 Dec 20 '22 at 12:33