簡體   English   中英

R - Shiny - 動態選擇條形圖

[英]R - Shiny - Dynamic selection bar plot

我想要一個條形圖 i R / Shiny 有動態。 我選擇了一個國家,然后我只想查看這個國家的數據。 我已經創建了一些 R 代碼,但是缺少國家/地區選擇和條形圖之間的關系。

server.R 文件

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文件

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")  
        )

    )
)

我得到了這個布局,但如何進行選擇 //​​ 條形圖關系?

在此處輸入圖片說明

在繪圖之前,您需要根據input$valgtland過濾數據。

使用 iris 數據集的模擬示例,因為您沒有提供可用數據:

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

reprex 包(v0.3.0) 於 2020 年 3 月 12 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM