How to use radio buttons in Rshiny, one - to do nothing and another to transform variable ?

I am trying to give user an option to either transform the y axis variable in log scale or not to transform. This is what I have tried so far and its not working

UI

ui <- fluidPage(
    headerPanel(title = "Shiny Tabs output Example"),
    sidebarLayout(
      
      sidebarPanel( width = 3,
        selectInput("country_id", "Select your countries for cases (multiple selection allowed)", 
                    choices = unique(covid_df$Country_Region), multiple = T, 
                    selected = c("India","Russia","US"))
        
                  ),
    
      mainPanel( width = 9,
        tabsetPanel(type = "tab",
          tabPanel("Box Plot", 
                   radioButtons("scale_input", label = "Choose y axis scale", 
                                choices = c("Normal" = "", 
                                            "log" = "lnorm"), inline = T),
                   
                   plotOutput("boxplot_id")
          
                )
              )
  )
)

Server

server <- function(input, output) {
  
  filter_react <- reactive({
    
    gather_df %>% 
      filter(Country_Region %in% input$country_id)
    
  })
  
  output$boxplot_id <- renderPlot({
    
    scale_input <- switch(input$scale_input,
                          norm = "",
                          lnorm = "scale_y_continuous(trans = 'log2')"
                          )
    
    filter_react() %>% 
      
      ggplot(aes(x = Country_Region, y = scale_input(Cases_Count), colour = Country_Region)) + #  fill = c("maroon","red","blue","orange")
      # scale_y_continuous(trans = "log2") +
      geom_point(alpha = 0.3, position = "jitter") +
      geom_boxplot(alpha = 0, colour = "black") + # 
      facet_wrap(~Case_Type) +
      theme_classic() + 
      theme(axis.text.y = element_text(size = 11)) +
      labs(y="log of Cases Count", title = "Box Plot Comparison of Countries Cases according to Types" ) +
      scale_input()
  }) 
    
}

Issue
I am getting errors on both buttons and don't know how to resolve.

you seem to be using a single name 'scale_input' to try to do two different things
a) as a function that can provide a y aesthetic by transforming Cases_Count
b) as an optional scale_y_continious that might be added to a sequence of ggplot calls.

I think the latter may be superflous, but we can find a solution, if you look at my reprex and find it lacking.

library(shiny)
library(tidyverse)
ui <- fluidPage(
  headerPanel(title = "Shiny Tabs output Example"),
  sidebarLayout(
    
    sidebarPanel( width = 3,
                  selectInput("country_id", "Select your countries for cases (multiple selection allowed)", 
                              choices = c("India","Russia","US","UK"), multiple = T, 
                              selected = c("India","Russia","US")))
                  
    ,
    
    mainPanel( width = 9,
               tabsetPanel(type = "tab",
                           tabPanel("Box Plot", 
                                    radioButtons("scale_input", label = "Choose y axis scale", 
                                                 choices = c("Normal" = "norm", 
                                                             "log" = "lnorm"), inline = T),
                                    
                                    plotOutput("boxplot_id")
                                    
                           )
               )
    )
  )
)
  
  server <- function(input, output) {
    
    filter_react <- reactive({
      
      tibble(Country_Region=c("India","Russia","US","UK"),
             Cases_Count = c(1,2,4,8)
             ,Case_Type = c(1,1,1,1)
              ) %>% 
        filter(Country_Region %in% input$country_id)
      
    })
    
    output$boxplot_id <- renderPlot({
      
            scale_input_func <- function(x, y) {
        func <- switch(x,
          norm = identity,
          lnorm = log2
        )
        func(y)
      }
      
      filter_react() %>% 
        ggplot(aes(x = Country_Region, y = scale_input_func(input$scale_input,
                                                            Cases_Count), colour = Country_Region)) + #  fill = c("maroon","red","blue","orange")
        # scale_y_continuous(trans = "log2") +
        geom_point(alpha = 0.3, position = "jitter") +
        geom_boxplot(alpha = 0, colour = "black") + # 
        facet_wrap(~Case_Type) +
        theme_classic() + 
        theme(axis.text.y = element_text(size = 11)) +
        labs(y="log of Cases Count", title = "Box Plot Comparison of Countries Cases according to Types" ) 
    }) 
  }
  
shinyApp(ui, server)

Thanks @nirgrahamuk for the solution. Its working very well. Actually when I attempted this code for the first time it was alot similar to yours but that time i didn't know how to perform no transformation for normal selection. Thanks to your code I added "identity" in my first attempted code and it worked and your code is also working flawlessly.

Here is the piece of code that I ran finally:

output$boxplot_id <- renderPlot({
    
    scale_input <- switch(input$scale_input,
                    norm = identity,
                    lnorm = log2
                          )
    
    
    filter_react() %>% 
      
      ggplot(aes(x = Country_Region, y = scale_input(Cases_Count), colour = Country_Region)) + #  fill = c("maroon","red","blue","orange")
      # scale_y_continuous(trans = "log2") +
      geom_point(alpha = 0.3, position = "jitter") +
      geom_boxplot(alpha = 0, colour = "black") + # 
      facet_wrap(~Case_Type) +
      theme_classic() + 
      theme(axis.text.y = element_text(size = 11)) +
      labs(y="log of Cases Count", title = "Box Plot Comparison of Countries Cases according to Types" ) 
  })

And before getting your reply I was able to run it using if else condition based on radio buttons value but that made code much longer and sort of duplicated. Appreciate your help, its much better way. Thanks again :slight_smile:

Why not just use one button as a toggle? That is how I have users choose log-scale or normal. I think that is more user-friendly as well.

Yeah I agree that its a better fit for this situation. I didn't know that a toggle button is provided in shiny(as I am new to shiny) . Thanks for letting me know, I will use a toggle button only.

This topic was automatically closed 54 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.