[英]R - Shiny - Dynamic selection bar plot
I would like to have a bar chart i R / Shiny there are dynamics.我想要一个条形图 i R / Shiny 有动态。 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.我已经创建了一些 R 代码,但是缺少国家/地区选择和条形图之间的关系。
server.R file 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 file 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")
)
)
)
I'm getting this layout but how do I make the selection // bar plot relation?我得到了这个布局,但如何进行选择 // 条形图关系?
You need to filter your data according to input$valgtland
before plotting.在绘图之前,您需要根据input$valgtland
过滤数据。
Mock example using iris dataset, since you did not provide usable data:使用 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
Created on 2020-03-12 by the reprex package (v0.3.0)由reprex 包(v0.3.0) 于 2020 年 3 月 12 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.