Большое спасибо!это сработало для сюжета!Однако мне нужно было продвинуть приложение, создав второй набор вкладок с широкой таблицей данных.Можно ли использовать ползунок диапазона для выбора лет в качестве столбцов в широкой таблице данных?Буду признателен за любые предложения.Основываясь на предыдущем решении, я написал это:
dat <<- read_excel("~/R/World estimates.xlsx")
datwide <<- read.csv("~/R/selected shiny.csv", check.names=FALSE)
ui <- fluidPage(
pageWithSidebar(
headerPanel("Data, 1990-2017"),
sidebarPanel(
conditionalPanel(
condition = "input.theTabs == 'firstTab' ",
h3('Time Series Plot '),
selectInput(inputId = "y",
label = "Y-axis:",
choices = c("Estimate", "Male", "Female"),
selected = "Estimate"),
# Select which types of movies to plot
selectInput(inputId = "Region",
label = "Select Region:",
choices = c("Africa", "Americas", "Asia", "Europe", "Oceania", "World"),
multiple = TRUE,
selected = "World")
,
h3("Year range"), # Third level header: Years
sliderInput(inputId = "slider",
label = "Years",
min = 1990,
max = 2017,
sep = "",
step = 1,
value = c(1990, 2017))
),
conditionalPanel(
condition = "input.theTabs == 'secondTab' ",
h3('Data Table'),
selectInput(inputId = "Region1",
label = "Select Region:",
choices = c("Africa", "Americas", "Asia", "Europe", "Oceania", "World"),
multiple = TRUE,
selected = "World"),
selectInput(inputId = "Indicator",
label = "Select Indicator(s):",
choices = c("Estimated Count", "Estimated male", "Estimated
female"),
multiple = TRUE,
selected = "Estimated Count"),
sliderInput(inputId = "sliderData",
label = "Years",
min = 1990,
max = 2017,
sep = "",
step = 1,
value = c(2007, 2017)),
downloadButton(outputId = "download_data",
label = "Download Selected Data")
),
conditionalPanel(
condition = "input.theTabs == 'thirdTab' ",
h3("Maps")
)
),
mainPanel(
tabsetPanel(
tabPanel( "Time series", plotlyOutput("timeSeries"),
value = "firstTab"),
tabPanel( "Data", DT::dataTableOutput("datatab"),
value = "secondTab"),
tabPanel( "Maps", plotOutput("map"),
value = "thirdTab"),
id = "theTabs"
)
)
)
)
А для сервера:
server <- function(input, output) {
years <- reactive({
seq(input$slider[1], input$slider[2], by = 1)
})
regions <- reactive({
dat %>%
filter(Region_Name %in% input$Region & Year %in% years())
})
output$timeSeries <- renderPlotly({
p <- ggplot(data = regions(), aes_string( x = 'Year', y = input$y))+
geom_line(aes(color = Region_Name)) +
geom_point()
ggplotly(p)
})
years2 <- reactive({
seq(input$sliderData[1], input$sliderData[2], by = 1)
})
output$datatab <- DT::renderDataTable({
d <-
datwide %>%
filter(Region %in% input$Region1 &
Variable %in% input$Indicator) %>%
select(Region, Variable, years2 %in% input$sliderData)
d
})
# Create a download handler
output$download_data <- downloadHandler(
filename = "selected_data.csv",
content = function(file) {
datwide %>%
filter(Region %in% input$Region1 &
Variable %in% input$Indicator) %>%
select(Region, Variable, years2 %in% input$sliderData)
d
# Write the filtered data into a CSV file
write.csv(d, file, row.names = FALSE)
}
)
}