-1

In Shiny, how do you log transform a variable (x and or y) given a checkbox input? If the input variables are numeric, I am looking to log transform the variable given the checkbox input.

library(shiny)
library(ggplot2)

ui <- fluidPage(
  selectInput(inputId = "xvariable",
              label = "X Variable",
              choices = colnames(mtcars)),
  checkboxInput("LogX", "Log Transform", FALSE),
  selectInput(inputId = "yvariable",
              label = "Y Variable",
              choices = colnames(mtcars)),
  checkboxInput("LogY", "Log Transform", FALSE),
  
  h3(""),
  plotOutput("plot")
)

server <- function(input, output, session) {
  output$plot <- renderPlot({
    req(input$xvariable)
    req(input$yvariable)
    g <- ggplot(mtcars, aes(x = !!as.symbol(input$xvariable), y = !!as.symbol(input$yvariable)))
    if (input$xvariable %in% c("mpg", "disp", "hp", "drat", "wt", "qsec")) {
      # numeric
      g <- g + geom_point()
    } else {
      # categorical
      g <- g + geom_bar()
    }
    g
  })
}

shinyApp(ui, server)
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 21 '20 at 03:46
  • @MrFlick, sorry about that I added an example. – datastats612 Jul 21 '20 at 04:26
  • Well, you already conditionally add the different geom. Why not do the same for your scale? You can optionally add either `scale_x_log10` or `scale_y_log10` if you want to do a log10 transform. – MrFlick Jul 21 '20 at 04:36

1 Answers1

0

This should work. Based on the selected variable, you create a new X and Y column. If log transformation is on, transform that column. Also makes the ggplot call a bit easier.

library(shiny)
library(ggplot2)

ui <- fluidPage(
  selectInput(inputId = "xvariable",
              label = "X Variable",
              choices = colnames(mtcars)),
  checkboxInput("LogX", "Log Transform", FALSE),
  selectInput(inputId = "yvariable",
              label = "Y Variable",
              choices = colnames(mtcars)),
  checkboxInput("LogY", "Log Transform", FALSE),
  
  h3(""),
  plotOutput("plot")
)

server <- function(input, output, session) {
  output$plot <- renderPlot({
    req(input$xvariable)
    req(input$yvariable)
    
    ## Copy mtcars into new dataframe
    df <- mtcars
    
    ## Create Y Variable
    # If LogY is TRUE
    if(isTRUE(input$LogY)){
      df$y <- log(df[[as.character(input$yvariable)]])
    }
    # If LogY is FALSE
    if(!isTRUE(input$LogY)){
      df$y <- df[[as.character(input$yvariable)]]
    }

    ## Modify X Variable Based on Variable
    if (input$xvariable %in% c("mpg", "disp", "hp", "drat", "wt", "qsec")) {
    ## Create X Variable
      # If LogX is TRUE
    if(isTRUE(input$LogX)){
      df$x <- log(df[[as.character(input$xvariable)]])
    }
      # If LogX is FALSE
    if(!isTRUE(input$LogX)){
      df$x <- df[[as.character(input$xvariable)]]
    }
      
      ## Graph
      g <- ggplot(df, aes(x = x, y = y)) + geom_point()
      
    } else {
      df$x <- as.factor(df[[as.character(input$xvariable)]])
      
      ## Graph
      g <- ggplot(df, aes(x = x)) + geom_bar()
    }

    g
  })
}
rfineman
  • 128
  • 2
  • 9