Shiny dashboard

...I'm fairly new on here so hopefully I put this in the right way.
I'm trying to run a ggplot in a shiny dashboard app based on variables input from a user using a SelectInput field but can't seem to get it to work. Any suggestions

library(shiny)
library(shinydashboard)
library(ggplot2)
library(DT)
library(tidyverse)


header <- dashboardHeader(title = "Customer Acquisition compared to Customer Retention")

 
sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Marketing", tabName = "Marketing", icon = icon("dashboard"))
  ))


body <- dashboardBody(
  tabItems(
    tabItem(tabName = "Marketing",
            h3("Annual Marketing Costs"),
            fluidRow(
              box(title = "Direct Costs", solidHeader = TRUE, status = "primary",
                  textInput("company", "Company", "Company Name"),
                  sliderInput("wd", "Web Design", min=0, max =1000, 3000, round=TRUE),
                  sliderInput("wh", "Web Hosting", min=0, max =1000,500, round=TRUE),
                  box(title = "Customer Acquisition vs Customer Retention? ", solidHeader = TRUE, status = "primary",
                      plotOutput("plot"),
                      tableOutput("data")))))))


ui <- dashboardPage(skin="blue",
                    header,
                    sidebar,
                    body
)


server <- function(input, output) {
  #text name
  output$company <- renderText({ input$company })  
  


  sum <- reactive({
    input$wd + input$wh
  })
  
  output$data <- renderTable({
    df <- data.frame(Company = input$company, WebDesign = input$wd,WebHosting = input$wh) %>% 
      gather("Company", "WebDesign:WebHosting") %>% 
      rename("Expenditure" = "WebDesign:WebHosting")
  }) 
  output$plot <- renderPlot({
    ggplot(df(),aes_string(factor(input$Company), input$Expenditure)) +
      geom_col()
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

Hi @sueb

Could you please edit your post to apply the proper formatting :sparkles: to your code? We ask posters here to do this in order to make the site easier to read and to prevent confusion (unformatted code can mistakenly trigger other types of automatic formatting, such as turning all your comments into headers! :dizzy_face:). You can read all the details of how to do it here: FAQ: How to make your code look nice? Markdown Formatting

Or try the easy way :grin: : select any code (whether in blocks or in bits and pieces mixed into your sentences) and click the little </> button at the top of the posting box.

Thank you, and sorry :slight_smile:

1 Like

Thanks for fixing the formatting! :grin:

A few things were going wrong here (and I hope I've correctly understood what the code is aiming for — let me know if not!):

  • The df() reactive used as the data argument to ggplot() wasn't actually created anywhere
  • Instead, a data frame named df was created inside the renderTable(), but this won't be available to anything outside of renderTable()
  • The x aesthetic for the plot is set to factor(input$Company), but...
    • there is no input$Company (you have an input$company, instead — note capitalization!)
    • assuming the code is meant to be plotting the data frame defined inside renderTable(), then the only variables in that data frame are Company and Expenditures (there is no variable named after whatever the user has entered in input$company).

A couple minor notes:

  • You don't need to load both ggplot2 and tidyverseggplot2 gets loaded automatically when you load tidyverse
  • You don't need to use factor() on the x variable in your plot, since the data frame was made with data.frame() which converts character variables into factors by default oops, Company is created as a character variable at the gather() step, but it still doesn't matter since geom_col() treats whatever you give it for x as categorical data.
  • The server() code includes the line output$company <- renderText({ input$company }), but there isn't actually any output called "company". The sum() reactive also isn't used anywhere. I removed these two definitions to simplify, but you should put them back if they're important for your real code.

Does this version do what you're hoping for? If not, can you explain more about what you want the plot to look like?

library(shiny)
library(shinydashboard)
library(DT)
library(tidyverse)


header <- dashboardHeader(title = "Customer Acquisition compared to Customer Retention")


sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Marketing", tabName = "Marketing", icon = icon("dashboard"))
  ))


body <- dashboardBody(
  tabItems(
    tabItem(tabName = "Marketing",
            h3("Annual Marketing Costs"),
            fluidRow(
              box(title = "Direct Costs", solidHeader = TRUE, status = "primary",
                  textInput("company", "Company", "Company Name"),
                  sliderInput("wd", "Web Design", min=0, max =1000, 3000, round=TRUE),
                  sliderInput("wh", "Web Hosting", min=0, max =1000,500, round=TRUE),
                  box(title = "Customer Acquisition vs Customer Retention? ", solidHeader = TRUE, status = "primary",
                      plotOutput("plot"),
                      tableOutput("data")))))))


ui <- dashboardPage(skin="blue",
                    header,
                    sidebar,
                    body
)


server <- function(input, output) {
  
  df <- reactive({
    data.frame(Company = input$company, WebDesign = input$wd, WebHosting = input$wh) %>% 
    gather("Company", "WebDesign:WebHosting") %>% 
    rename("Expenditure" = "WebDesign:WebHosting")
  })
  
  output$data <- renderTable({
    df() %>% set_names(input$company, names(df())[2])
  }) 
  
  output$plot <- renderPlot({
    ggplot(df(), aes(Company, Expenditure)) +
      geom_col() + 
      labs(x = input$company)
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

5 Likes

Thank you so much, I could cry! Have been trying to do this for hours and I think I just got myself into a pickle. Have used your code and embedded it into my code and all works :slight_smile: :slight_smile:

1 Like

Yay! :tada: I know the feeling of going in code circles for hours :weary: — glad to have helped you escape! :grin:

1 Like

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