繁体   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