【问题标题】:Displaying selected variables in the table in R在 R 中的表中显示选定的变量
【发布时间】:2019-10-14 10:45:47
【问题描述】:

我只想获取从 selectInput(我的开关)中选择的表中的数据,因为现在该表显示了 Value 和 Amount。我尝试使用DT::datatable(tab_input1()),但它不起作用。怎么改?

我的代码:

library(plotly)
library(dplyr)
library(shiny)
library(shinyWidgets)
library(readxl)
library(tidyr)
library(DT)

df1 <- data.frame(Month = rep(month.abb[1:12],10,replace = TRUE), Product = paste0('Szafa ', rep(LETTERS[1:10], each = 12)),
                  Value = sample(c(0:300),120, replace = T), Amount = sample(c(1000:10000),120, replace = T),stringsAsFactors = F)

df2 <- data.frame(Month = rep(month.abb[1:12],10,replace = TRUE), Product = paste0('Fotel ', rep(LETTERS[1:10], each = 12)),
                  Value = sample(c(0:300),120, replace = T),Amount = sample(c(1000:10000),120, replace = T), stringsAsFactors = F)

# UI
ui <- fluidPage(
    column(
        6,fluidRow(column(6, selectizeInput("All", "Year: 2018", multiple = T,choices = unique(df1$Product), 
                                            options = list(maxItems = 5, placeholder = 'Choose a product:'))),
                   column(6, selectizeInput("All2", "Year: 2019", multiple = T,choices = unique(df2$Product), 
                                            options = list(maxItems = 5, placeholder = 'Choose a product:'))),
                   column(6, selectInput("y_axis1", "What you want to analyze?", choices = c("Value", "Amount")))
    )),
    column(
        12,fluidRow(column(12, plotlyOutput('plot'),
                           12, DT::dataTableOutput('tbl2'))
        )
    ) 
)

# Server code
server <- function(input, output) {

    tab_input1 <- reactive({
        switch(input$y_axis1,
               Value = "Value", 
               Amount = "Amount")
    })

    outVar <- reactive({
        df1 %>%
            filter(Product %in% input$All) %>%
            mutate(Product = paste(Product, "2018", sep = " ")) %>% 
            arrange(Month) %>%
            droplevels()
    })

    outVar2 <- reactive({
        df2 %>%
            filter(Product %in% input$All2) %>%
            mutate(Product = paste(Product, "2019", sep = " ")) %>% 
            arrange(Month) %>%
            droplevels()
    })

    output$plot <- renderPlotly({
        plot_ly(data=outVar(), x=~Month,  y = outVar()[,tab_input1()],
                type = 'scatter', mode = 'lines', legendgroup = "1",
                color = ~Product  , colors = c('red','blue', 'yellow', 'green', "orange")) %>%
            add_trace(data=outVar2(), x=~Month,  y = outVar2()[,tab_input1()],
                      type = 'scatter', mode = 'lines', legendgroup = "2",
                      color = ~Product , colors = c('red','blue', 'yellow', 'green', "orange"))  %>%
            layout(legend = list(orientation = 'h'))         
    }) 

    output$tbl2 <- DT::renderDataTable({
        DT::datatable(rbind(outVar(),outVar2()))
        #DT::datatable(tab_input1())
    })
}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r datatable shiny dt


    【解决方案1】:

    您可以在全局中定义c("Value", "Amount") 以使用它知道您要保留哪一列。

    library(plotly)
    library(dplyr)
    library(shiny)
    library(shinyWidgets)
    library(readxl)
    library(tidyr)
    library(DT)
    
    df1 <- data.frame(Month = rep(month.abb[1:12],10,replace = TRUE), Product = paste0('Szafa ', rep(LETTERS[1:10], each = 12)),
                      Value = sample(c(0:300),120, replace = T), Amount = sample(c(1000:10000),120, replace = T),stringsAsFactors = F)
    
    df2 <- data.frame(Month = rep(month.abb[1:12],10,replace = TRUE), Product = paste0('Fotel ', rep(LETTERS[1:10], each = 12)),
                      Value = sample(c(0:300),120, replace = T),Amount = sample(c(1000:10000),120, replace = T), stringsAsFactors = F)
    
    analyze <- c("Value", "Amount")
    
    # UI
    ui <- fluidPage(
      column(
        6,fluidRow(column(6, selectizeInput("All", "Year: 2018", multiple = T,choices = unique(df1$Product), 
                                            options = list(maxItems = 5, placeholder = 'Choose a product:'))),
                   column(6, selectizeInput("All2", "Year: 2019", multiple = T,choices = unique(df2$Product), 
                                            options = list(maxItems = 5, placeholder = 'Choose a product:'))),
                   column(6, selectInput("y_axis1", "What you want to analyze?", choices = analyze))
        )),
      column(
        12,fluidRow(column(12, plotlyOutput('plot'),
                           12, DT::dataTableOutput('tbl2'))
        )
      ) 
    )
    
    # Server code
    server <- function(input, output) {
    
      tab_input1 <- reactive({
        switch(input$y_axis1,
               Value = "Value", 
               Amount = "Amount")
      })
    
      outVar <- reactive({
        df1 %>%
          filter(Product %in% input$All) %>%
          mutate(Product = paste(Product, "2018", sep = " ")) %>% 
          arrange(Month) %>%
          droplevels()
      })
    
      outVar2 <- reactive({
        df2 %>%
          filter(Product %in% input$All2) %>%
          mutate(Product = paste(Product, "2019", sep = " ")) %>% 
          arrange(Month) %>%
          droplevels()
      })
    
      output$plot <- renderPlotly({
        plot_ly(data=outVar(), x=~Month,  y = outVar()[,tab_input1()],
                type = 'scatter', mode = 'lines', legendgroup = "1",
                color = ~Product  , colors = c('red','blue', 'yellow', 'green', "orange")) %>%
          add_trace(data=outVar2(), x=~Month,  y = outVar2()[,tab_input1()],
                    type = 'scatter', mode = 'lines', legendgroup = "2",
                    color = ~Product , colors = c('red','blue', 'yellow', 'green', "orange"))  %>%
          layout(legend = list(orientation = 'h'))         
      }) 
    
      output$tbl2 <- DT::renderDataTable({
        rbind_tab <- rbind(outVar(),outVar2())
        del_column <- analyze[analyze != tab_input1()] # get which column to delete
        rbind_tab[[del_column]] <- NULL 
        DT::datatable(rbind_tab)
        #DT::datatable(tab_input1())
      })
    }
    
    # Return a Shiny app object
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 你可以只使用input$y_axis1 而不是tab_input1()
    • 在开关中测试更多定量变量的代码,我得到错误:Warning: Error in &lt;-: replacement has length zero. 为什么?
    • 我删除了一个方括号[[del_column]] 并且代码有效。
    猜你喜欢
    • 1970-01-01
    • 2013-07-17
    • 2015-06-06
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 2020-07-31
    • 1970-01-01
    相关资源
    最近更新 更多