【问题标题】:Displaying the last value in the table in R在R中显示表格中的最后一个值
【发布时间】:2020-06-11 09:24:40
【问题描述】:

我在闪亮中创建了一个简单的应用程序,我希望表格只显示最后一个可见值。正如我在下图中用红色标记的那样。我的代码如下:

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)

【问题讨论】:

    标签: r server datatable shiny render


    【解决方案1】:

    这是你想要的吗?

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多