Reputation: 503
I created a simple application in shiny and I would like the table to display only the last visible value. As I marked it in the graphics below in red. My code below:
library(shiny)
mat <- replicate(10, c(sample(c(1000:100), 6, replace = T), sample(NA, 6, replace = T)))
df1 <- data.frame(Month = rep(seq(as.Date("2020-01-03"), by="day", len=12),10,replace = TRUE),
Product = rep(LETTERS[1:10], each = 12),
Value = sample(c(0:300),120, replace = T),
Amount = c(mat),stringsAsFactors = F)
ui <- fluidPage(
pickerInput("All", "Choose", multiple = F, choices = unique(df1$Product) ,
options = list(`max-options` = 4,size = 10)),
plotlyOutput('plot'),
DT::dataTableOutput('tbl2')
)
server <- function(input, output) {
trend<- reactive({
df1 %>%
filter(Product %in% input$All) %>%
arrange(Month) %>%
droplevels()
})
output$plot <- renderPlotly({
plot_ly(data=trend(), x=~Month, y = ~Value,
type = 'scatter', mode = 'lines+markers') %>%
add_trace(y = ~Amount, mode = 'lines', yaxis = "y2" , connectgaps = TRUE) %>%
layout(yaxis2 = list(overlaying = "y", side = "right"))
})
output$tbl2 <- DT::renderDataTable({
DT::datatable(trend()[,c(1,2,4)])
})
}
shinyApp(ui = ui, server = server)
Upvotes: 0
Views: 62
Reputation: 29387
Is this what you want?
library(shiny)
library(plotly)
library(shinyWidgets)
set.seed(666)
mat <- replicate(10, c(sample(c(1000:100), 6, replace = T), sample(NA, 6, replace = T)))
df1 <- data.frame(Month = rep(seq(as.Date("2020-01-03"), by="day", len=12),10,replace = TRUE),
Product = rep(LETTERS[1:10], each = 12),
Value = sample(c(0:300),120, replace = T),
Amount = c(mat),stringsAsFactors = F)
ui <- fluidPage(
pickerInput("All", "Choose", multiple = F, choices = unique(df1$Product) ,
options = list(`max-options` = 4,size = 10)),
plotlyOutput('plot'),
DT::dataTableOutput('tbl2')
)
server <- function(input, output) {
trend<- reactive({
df1 %>%
filter(Product %in% input$All) %>%
arrange(Month) %>%
droplevels()
})
output$plot <- renderPlotly({
plot_ly(data=trend(), x=~Month, y = ~Value,
type = 'scatter', mode = 'lines+markers') %>%
add_trace(y = ~Amount, mode = 'lines', yaxis = "y2" , connectgaps = TRUE) %>%
layout(yaxis2 = list(overlaying = "y", side = "right"))
})
output$tbl2 <- DT::renderDataTable({
mdata <- trend()[,c(1,2,4)]
mdata <- tail(mdata[!is.na(mdata$Amount),],1)
DT::datatable(mdata)
})
}
shinyApp(ui = ui, server = server)
Upvotes: 1