Home > Back-end >  Plotting categorical and numerical varibles with a shiny r app
Plotting categorical and numerical varibles with a shiny r app

Time:05-11

I'm working on a Shiny R app but I don't get why my function draw_plot_1 is not working. Because is only plotting the data in the last else. I would like to

plot a boxplot when one variable is numeric and the other one is categorical.

Outside the shiny app, my function works fine. Any suggestions would be great!

Here is my code:

not_sel <- "No selected"

about_page <- tabPanel(
  title = "Info",
  titlePanel("Info"),
  "Creado con R Shiny",
  br(),
  "Mayo 2022"
)

main_page <- tabPanel(
  title = "Analysis",
  titlePanel("Analysis"),
  sidebarLayout(
    sidebarPanel(
      title = "Inputs",
      fileInput("csv_input", "Select CSV File to Import", accept = ".csv"),
      selectInput("num_var_1", "Variable 1", choices = c(not_sel)),
      selectInput("num_var_2", "Variable 2", choices = c(not_sel)),
      br(),
      actionButton("run_button", "Run Analysis", icon = icon("play"))
    ),
    mainPanel(
      tabsetPanel(
        tabPanel(
          title = "Plot",
          plotOutput("plot_1")
        
        
          )
        
        )
      
    )
  )
)

draw_plot_1 <- function(data_input, num_var_1, num_var_2){
  
  if(num_var_1 != not_sel & num_var_2 != not_sel)
  {
    if(is.character(num_var_2) & is.numeric(num_var_1))
    {
      ggplot(data = data_input,
             aes_string(x = num_var_1, y = num_var_2))  
        geom_boxplot()  
    }
    else if (is.character(num_var_1) & is.numeric(num_var_2))
    {
      ggplot(data = data_input,
             aes_string(x = num_var_2, y = num_var_1))  
        geom_boxplot()  
    }
    else
    {
      ggplot(data = data_input,
             aes_string(x = num_var_2, y = num_var_1))  
        geom_point()  
    }
    } 
}

ui <- navbarPage(
  title = "Data Analyser",
  theme = shinytheme('united'),
  main_page,
  about_page
)

server <- function(input, output){
  
  options(shiny.maxRequestSize=10*1024^2) 
  
  data_input <- reactive({
    req(input$csv_input)
    fread(input$csv_input$datapath)
  })
  
  observeEvent(data_input(),{
    choices <- c(not_sel,names(data_input()))
    updateSelectInput(inputId = "num_var_1", choices = choices)
    updateSelectInput(inputId = "num_var_2", choices = choices)
  })
  
  num_var_1 <- eventReactive(input$run_button,input$num_var_1)
  num_var_2 <- eventReactive(input$run_button,input$num_var_2)

  
  # plot
  
  plot_1 <- eventReactive(input$run_button,{
    draw_plot_1(data_input(), num_var_1(), num_var_2())
  })
  
  output$plot_1 <- renderPlot(plot_1())
  
  output$num_var_1_title <- renderText(paste("Num Var 1:",num_var_1()))
  
}

shinyApp(ui = ui, server = server)

CodePudding user response:

Because the return value from the select list input control is a character. I've attached one screenshot showing the value of num_var_1() and num_var_2().

And I would suggest you to insert browser() function to debug your code and you could run the code below to have a better understanding of your code.

not_sel <- "No selected"

about_page <- tabPanel(
  title = "Info",
  titlePanel("Info"),
  "Creado con R Shiny",
  br(),
  "Mayo 2022"
)

main_page <- tabPanel(
  title = "Analysis",
  titlePanel("Analysis"),
  sidebarLayout(
    sidebarPanel(
      title = "Inputs",
      fileInput("csv_input", "Select CSV File to Import", accept = ".csv"),
      selectInput("num_var_1", "Variable 1", choices = c(not_sel)),
      selectInput("num_var_2", "Variable 2", choices = c(not_sel)),
      br(),
      actionButton("run_button", "Run Analysis", icon = icon("play"))
    ),
    mainPanel(
      tabsetPanel(
        tabPanel(
          title = "Plot",
          plotOutput("plot_1")
        
        
          )
        
        )
      
    )
  )
)

draw_plot_1 <- function(data_input, num_var_1, num_var_2){
  
  if(num_var_1 != not_sel & num_var_2 != not_sel)
  {
    if(is.character(num_var_2) & is.numeric(num_var_1))
    {
      ggplot(data = data_input,
             aes_string(x = num_var_1, y = num_var_2))  
        geom_boxplot()  
    }
    else if (is.character(num_var_1) & is.numeric(num_var_2))
    {
      ggplot(data = data_input,
             aes_string(x = num_var_2, y = num_var_1))  
        geom_boxplot()  
    }
    else
    {
      ggplot(data = data_input,
             aes_string(x = num_var_2, y = num_var_1))  
        geom_point()  
    }
    } 
}

ui <- navbarPage(
  title = "Data Analyser",
  theme = shinytheme('united'),
  main_page,
  about_page
)

server <- function(input, output){
  
  options(shiny.maxRequestSize=10*1024^2) 
  
  data_input <- reactive({
    req(input$csv_input)
    fread(input$csv_input$datapath)
  })
  
  observeEvent(data_input(),{
    choices <- c(not_sel,names(data_input()))
    updateSelectInput(inputId = "num_var_1", choices = choices)
    updateSelectInput(inputId = "num_var_2", choices = choices)
  })
  
  num_var_1 <- eventReactive(input$run_button,input$num_var_1)
  num_var_2 <- eventReactive(input$run_button,input$num_var_2)

  
  # plot
  
  plot_1 <- eventReactive(input$run_button,{
    draw_plot_1(data_input(), num_var_1(), num_var_2())
  })
  
  output$plot_1 <- renderPlot(plot_1())
  
  output$num_var_1_title <- renderText(paste("Num Var 1:",num_var_1()))
  
}

shinyApp(ui = ui, server = server)

enter image description here

  • Related