这是我建立一个应用程序,我想知道的是,我失去了我的代码中的功能或参数的第一次。 我想显示一个正常的柱状图,其动态过滤“公司”,我想看到显示的性能变量(如“利润”)和日期范围。
我相信我的错误是不精确的结果shiny
电话,因为我要的是相当简单的。 你可以通过运行排序得到它(但按规定没有可能相互作用的shiny
)下面的代码:
library(ggplot2)
sample_data = data.frame(Company_Name=c("Company 1","Company 2","Company 3",
"Company 1","Company 2","Company 3",
"Company 1","Company 2","Company 3"),
Profits_MM = c(20,100,80,
45,120,70,
50,110,90),
Sales_MM = c(200,800,520,
300,1000,630,
410,1150,770),
Year=c(2016,2016,2016,
2017,2017,2017,
2018,2018,2018))
ggplot(data = sample_data, aes(x=Year, y = Profits_MM,
fill=as.factor(Company_Name))) + geom_col(position="dodge")
但我只提到它这样一个可能有什么我想作为输出的想法。 我已经将有光泽的代码实际如下:
rm(list=ls()); gc()
library(shiny)
library(ggplot2)
library(dplyr)
sample_data = data.frame(Company_Name=c("Company 1","Company 2","Company 3",
"Company 1","Company 2","Company 3",
"Company 1","Company 2","Company 3"),
Profits_MM = c(20,100,80,
45,120,70,
50,110,90),
Sales_MM = c(200,800,520,
300,1000,630,
410,1150,770),
Year=c(2016,2016,2016,
2017,2017,2017,
2018,2018,2018))
# UI
ui <- fluidPage(
sidebarLayout(
# Input(s)
sidebarPanel(
checkboxGroupInput(inputId = "sel_com",
label = "Company Selection:",
choices = c("Company 1","Company 2","Company 3"),
selected = "Company 1"),
selectInput(inputId = "y",
label = "Performance Variable",
choices = c("Profits (in Millions)" = "Profits_MM",
"Sales (in Millions)" = "Sales_MM"),
selected = "Profits_MM"),
sliderInput("year","Year Selection:",
min=2016,
max=2018,
value=c(2017,2018),
step=1)
),
# Output(s)
mainPanel(
plotOutput(outputId = "barplot")
)
)
)
# Server
server <- function(input, output, session) {
companies_sel <- reactive({
req(input$sel_com)
filter(sample_data, Company_Name %in% input$sel_com)
})
year_sample <- reactive({
req(input$year)
if(length(input$year)>1){
Years = seq(input$year[1],input$year[2])
filter(companies_sel(), Year %in% Years)
}
if(length(input$year==1)){
filter(companies_sel(), Year %in% input$year)
}
})
output$barplot <- renderPlot({
ggplot(data = year_sample(), aes_string(x=input$year, y = input$y, fill=as.factor(input$sel_com))) +
geom_col(position="dodge")
})
}
shinyApp(ui = ui, server = server)
我能得到一些输出,但只为一家公司的时间和不改变的范围大小。 也许我不明白使用的observeEvent
功能,这可能会在此发挥作用。 那么,如何让inputSlider
-也许其他的选择-一个充分互动ggplot2
图?
这是在ouptut错误的例子: