How to combine multiple functions selected by selectInput

Dear all,
I'd like to apply different pre-processing strategies to my data and, eventually, I'd like to combine multiple techniques by applying them subsequentially.
Here's a reprex of what I'd like to do. In particular, I'd like the user to apply log10 transformation or scaling to a certaing dataframe. Moreover, I'd like to allow the user to apply both of them (in the specific order the user select) to the data.
Despite I can easily apply one of the two available pre-processing to the data, I'm not able to apply them subsequentially.
Here's is an example of the app I'd like to obtain, I tried to solve this issue but the code I wrote doesn't work when I select both the pre-processing strategies.

data.frame(
  check.names = FALSE,
  row.names = c("sample_1","sample_2","sample_3",
                "sample_4","sample_5","sample_6","sample_7","sample_8"),
  `649.92` = c(0.06, 0.191, 0.0827, 0.0251, 0.065, 0.0447, 0.0433, 0.042),
  `650.4` = c(0.0602, 0.1908, 0.0827, 0.0251, 0.0651, 0.0447, 0.0433, 0.042),
  `650.89` = c(0.0604, 0.1909, 0.0828, 0.0251, 0.065, 0.0447, 0.0434, 0.042),
  `651.37` = c(0.0606, 0.1911, 0.0829, 0.025, 0.065, 0.0448, 0.0435, 0.0421),
  `651.85` = c(0.0608, 0.1914, 0.0831, 0.025, 0.0649, 0.0448, 0.0436, 0.0422),
  `747.31` = c(0.0445, 0.1335, 0.0715, 0.021, 0.0611, 0.0324, 0.0354, 0.036),
  `1044.79` = c(0.0722,0.2081,0.0877,0.0278,0.0998,
                0.0466,0.0455,0.0375)
)

if (interactive()) {
  shinyApp(
    ui <- fluidPage(
      titlePanel("Tabsets"),
      sidebarLayout(
        sidebarPanel(
          selectInput("funct","Pre-processing type",
                      c("Logaritm" = "log10",
                        "Scaling" = "scale"),
                      multiple = TRUE)),
        mainPanel(
           tabsetPanel(type = "tabs",
                      tabPanel("Table", tableOutput("table"))
          )
        )
      )
    ),
    server <- function(input, output) {
      d <- reactive({
        funct <- switch(input$funct,
                       log10 = log10,
                       scale = scale,
                       log10)
        funct(data)
      })
      output$table <- renderTable({
        d()
      })
    }
  )
}

I'd be very grateful if anybody could attend to this matter

Your reprex had an error in so far as you hadn't named your example dataframe, but presumably tried to refer to it with funct(data) after your switch.
Anyway here is a way to do it that works.

library(shiny)

mydata <- data.frame(
  check.names = FALSE,
  row.names = c("sample_1","sample_2","sample_3",
                "sample_4","sample_5","sample_6","sample_7","sample_8"),
  `649.92` = c(0.06, 0.191, 0.0827, 0.0251, 0.065, 0.0447, 0.0433, 0.042),
  `650.4` = c(0.0602, 0.1908, 0.0827, 0.0251, 0.0651, 0.0447, 0.0433, 0.042),
  `650.89` = c(0.0604, 0.1909, 0.0828, 0.0251, 0.065, 0.0447, 0.0434, 0.042),
  `651.37` = c(0.0606, 0.1911, 0.0829, 0.025, 0.065, 0.0448, 0.0435, 0.0421),
  `651.85` = c(0.0608, 0.1914, 0.0831, 0.025, 0.0649, 0.0448, 0.0436, 0.0422),
  `747.31` = c(0.0445, 0.1335, 0.0715, 0.021, 0.0611, 0.0324, 0.0354, 0.036),
  `1044.79` = c(0.0722,0.2081,0.0877,0.0278,0.0998,
                0.0466,0.0455,0.0375)
)

if (interactive()) {
  shinyApp(
    ui <- fluidPage(
      titlePanel("Tabsets"),
      sidebarLayout(
        sidebarPanel(
          selectInput("funct","Pre-processing type",
                      c("Logaritm" = "log10",
                        "Scaling" = "scale"),
                      multiple = TRUE)),
        mainPanel(
          tabsetPanel(type = "tabs",
                      tabPanel("Table", tableOutput("table"))
          )
        )
      )
    ),
    server <- function(input, output) {
      d <- reactive({

        transformed_data  <- mydata
        if(  "log10" %in% input$funct){
          transformed_data <- log10(transformed_data)
        }
        if(  "scale" %in% input$funct) {
          transformed_data <- scale(transformed_data)
        }

        transformed_data
      })
      output$table <- renderTable({
        d()
      })
    }
  )
}
1 Like

Dear @nirgrahamuk,
thank you very much for your help, it is properly what I was looking for.
Thanks a lot for this!

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