r - R - Shiny - 动态选择条图
问题描述
我想要一个条形图 i R / Shiny 有动态。我选择了一个国家,然后我只想查看这个国家的数据。我创建了一些 R 代码,但缺少国家/地区选择与条形图之间的关系。
服务器.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 包于 2020-03-12 创建(v0.3.0)
推荐阅读
- php - SQLSTATE[HY000] [1044] 上传到 PhpMyAdmin 数据库表时,用户“@'localhost' 的访问被拒绝
- markdown - 为什么 gatsby-transformer-remark 不将 ID 添加到标题以便我可以链接到它们?
- reactjs - 如果我需要从 fetch 中渲染数据怎么办?
- swagger - 在没有 swagger.json 的情况下使用 OpenAPI/Swagger CodeGen
- laravel - 如何通过多对多关系获取与同一张表相关的行 - Laravel
- javascript - 无法在 Mocha 测试中将 VueX 商店导入路由器和路由器进入商店
- r - 使用 dplyr 在两列中查找重复的字符值
- powershell - 用powershell用字符串替换数字(用双引号括起来)
- docker - 避免在 Docker 中有多个容器
- reactjs - 如何测试依赖于 useContext 钩子的反应组件?