我是R和Shiny的完全新手。
我一直在努力开发一种探索经济数据的工具。我正在使用GDP指数数据。到目前为止,该工具允许您加载不同的扇区,但我想控制多年。这就是GDP Tool目前的样子。
我想使用滑块来控制它们在绘图上出现的年份。
这是我到目前为止的代码:
library(shiny)
library(ggplot2)
GDPData = read.csv("Data/GDP_Data_New.csv", stringsAsFactors = FALSE)
ui <- fluidPage(
sidebarPanel(
tags$h1("Scottish GDP by Sector")
),
mainPanel(
tabsetPanel(
tabPanel("Broad Sectors",
plotOutput("Highlevel"),
sliderInput("SectorTime", "Select a time period", min = 1998, max = 2016,
value = c(1998,2016)),
checkboxGroupInput("Indicator","Select an indicator:",
c("Total" = "Total ", "Production" = "Production", "Construction" = "Construction","Services" = "Services", "Agriculture, forestry and fishing" = "AFF"),
selected = "Total ")),
tabPanel("Production",
plotOutput("Production"),
checkboxGroupInput("ProductionIndicator","Select an indicator:",
c("Total" = "Production", "Manufacturing" = "Manufacturing", "Mining and Quarrying" = "Mning","Electricity & Gas Supply" = "Electricty", "Water Supply & Waste Management" = "WaterWaste"),
selected = "Production")),
tabPanel("Services",
plotOutput("Services"),
checkboxGroupInput("ServicesIndicator","Select an indicator:",
c("Total" = "Services", "Distribution, Hotels and Catering" = "Distribution","Transport, Storage and Communication" = "Transport", "Business Services and Finance" = "Professional","Government, and Other Services" = "Public"),
selected = "Services"))
)
)
)
server <- function(input, output, session) {
SectorsData=reactive({
return(GDPData[GDPData$Sector%in%input$Indicator,])
})
ProductionData=reactive({
return(GDPData[GDPData$Sector%in%input$ProductionIndicator,])
})
ServicesData=reactive({
return(GDPData[GDPData$Sector%in%input$ServicesIndicator,])
})
output$Highlevel <- renderPlot(ggplot(SectorsData(), aes(x=Year, y=Index, group=Sector, colour=Sector)) + geom_line()+geom_point()+theme_minimal())
output$Production <- renderPlot(ggplot(ProductionData(), aes(x=Year, y=Index, group=Sector, colour=Sector)) + geom_line()+geom_point()+theme_minimal())
output$Services <- renderPlot(ggplot(ServicesData(), aes(x=Year, y=Index, group=Sector, colour=Sector)) + geom_line()+geom_point()+theme_minimal())
}
shinyApp(ui = ui, server = server)
CSV文件有三列“扇区”,“年份”,“索引”
任何帮助将不胜感激。
这是您需要做的示例:
library(shiny)
ui <- fluidPage(
sliderInput(inputId = "year", label = "YEAR", min = 1900, max = 2000, value = c(1920, 1980), step = 5)
,
plotOutput("plot")
)
server <- function(input, output, session) {
main_data <- reactive({
data.frame(a= 1900:2000, b=rnorm(101) )
})
output$plot <- renderPlot({
df <- main_data()
df <- df[df$a>input$year[1] & df$a<input$year[2],]
plot(df$a, df$b, type = "l")
})
}
shinyApp(ui = ui, server = server)