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)