简体   繁体   中英

R - Shiny - Dynamic selection bar plot

I would like to have a bar chart i R / Shiny there are dynamics. I select a country and then I would like to only see the data for this country. I have created some of the R code, but the relation between the selection of the country and the bar chart is missing.

server.R file

library(datasets)
salg <- read_excel("C:\\Users\\Tue Hellstern\\Documents\\Demo\\Demo\\data\\SalgsData.xlsx", sheet = "salgs_data")

# Define a server for the Shiny app
function(input, output) {


  output$selected_var <- renderText({ 
    paste("You have selected", input$valgtLand)
  })

  # Fill in the spot we created for a plot
  output$salgplot <- renderPlot({

    # Render a barplot
    salg %>%
      ggplot(aes(x=CompanyType, y=Total)) +
      geom_bar(stat="identity")
  })
}

ui.R file

library(datasets)
salg <- read_excel("C:\\Users\\Tue Hellstern\\Documents\\Demo\\Demo\\data\\SalgsData.xlsx", sheet = "salgs_data")

# Use a fluid Bootstrap layout
fluidPage(    

    # Give the page a title
    titlePanel("Salg efter kundetype"),

    # Generate a row with a sidebar
    sidebarLayout(      

        # Define the sidebar with one input
        sidebarPanel(helpText("Du har mulighed for at vaelge kun at se et bestemt land"), 

                     selectInput("valgtland", h3("Vaelg land"), 
                                 choices = salg$Country, 
                                 selected = 1)),

        # Create a spot for the barplot
        mainPanel(
            plotOutput("salgplot")  
        )

    )
)

I'm getting this layout but how do I make the selection // bar plot relation?

enter image description here

You need to filter your data according to input$valgtland before plotting.

Mock example using iris dataset, since you did not provide usable data:

library(shiny)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

server <- function(input, output) {
    output$selected_var <- renderText({
        paste("You have selected", input$valgtLand)
    })

    # Fill in the spot we created for a plot
    output$salgplot <- renderPlot({
        # Render a barplot
        dplyr::filter(iris, Species == input$valgtland) %>%
            ggplot(aes(x=cut_interval(Petal.Width, n=4), y=Sepal.Length)) +
            geom_bar(stat="identity")
    })
}

ui <- fluidPage(    

    # Give the page a title
    titlePanel("Salg efter kundetype"),

    # Generate a row with a sidebar
    sidebarLayout(      

        # Define the sidebar with one input
        sidebarPanel(helpText("Du har mulighed for at vaelge kun at se et bestemt land"), 

            selectInput("valgtland", h3("Vaelg land"), 
                choices = unique(iris$Species), 
                selected = "setosa")),

        # Create a spot for the barplot
        mainPanel(
            plotOutput("salgplot")  
        )

    )
)

shinyApp(ui = ui, server = server)
#> 
#> Listening on http://127.0.0.1:5699

Created on 2020-03-12 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM