从列表中选择一个数据集以使用R Shiny执行静态分析

时间:2018-03-22 10:19:40

标签: r shiny

以下代码有助于在不同的列中选择多个值     通过闪亮应用程序中的复选框和右侧显示的选定数据     面板。我想对来自的任何选定数据集执行相同的操作     列表,即我有两个样本数据,一个应该能够选择一个数据集     从下拉菜单和选定的样本数据(每周或每月)     将在右侧面板中提供对通过复选框执行操作。     非常感谢您的帮助

---Sample_wkly   
MO_ID   DrugType    Customer    Revenue DrugClass
W1  Drug 1  Manufacturer5   12765   ART
W2  Drug 2  Manufacturer4   41528   II
W3  Drug 3  Manufacturer3   4204    NNRTI
W4  Drug 4  Manufacturer2   57222   PI
W5  Drug 5  Manufacturer1   3121    STR
W6  Drug 1  Manufacturer5   11917   NNRTI
W7  Drug 2  Manufacturer4   2059    PI
W8  Drug 3  Manufacturer3   11116   STR
W9  Drug 4  Manufacturer2   3815    NNRTI
W10 Drug 5  Manufacturer1   21071   PI
W11 Drug 1  Manufacturer5   9830    STR
W12 Drug 2  Manufacturer4   152061  NNRTI
W13 Drug 3  Manufacturer3   7215    PI
W14 Drug 4  Manufacturer2   10508   STR
W15 Drug 5  Manufacturer1   22802   ART
W16 Drug 1  Manufacturer5   7486    II
W17 Drug 2  Manufacturer4   13627   NNRTI
W18 Drug 3  Manufacturer3   27594   PI
W19 Drug 4  Manufacturer2   31728   STR
W20 Drug 5  Manufacturer1   12711   NNRTI
W21 Drug 1  Manufacturer3   1241    PI

Sample_mthly
MO_ID   DrugType    Customer    Revenue DrugClass
Jan-00  Drug 1  Manufacturer5   12765   ART
Feb-01  Drug 2  Manufacturer4   41528   II
Mar-02  Drug 3  Manufacturer3   4204    NNRTI
Apr-03  Drug 4  Manufacturer2   57222   PI
May-04  Drug 5  Manufacturer1   3121    STR
Jun-05  Drug 1  Manufacturer5   11917   NNRTI
Jul-06  Drug 2  Manufacturer4   2059    PI
Aug-07  Drug 3  Manufacturer3   11116   STR
Sep-08  Drug 4  Manufacturer2   3815    NNRTI
Oct-09  Drug 5  Manufacturer1   21071   PI
Nov-10  Drug 1  Manufacturer5   9830    STR
Dec-11  Drug 2  Manufacturer4   152061  NNRTI
Jan-12  Drug 3  Manufacturer3   7215    PI

Source Code-
library(shiny)
library(ggplot2)
library(dplyr)
library(DT)

#############################
# Importing Data
cDat <- read.table(file = "data/sample_wkly.csv", sep = ",",  header = 
                 TRUE, row.names = NULL)
cDat1 <- read.table(file = "data/sample_mthly.csv", sep = ",",  header = 
                 TRUE, row.names = NULL)
############################

###### Preparing list for Checkbox & Change rownames
#campaigns_list <- unique(cDat$MANUFACTURER)

MANUFACTURER_list <- unique(cDat$MANUFACTURER)
DrugClass_list <- unique(cDat$DrugClass)
DrugType_list <- unique(cDat$DrugType)
columns <- names(cDat)
####################### Server.R##########
server <- function(input, output, session) {

  filteredData <- reactive({ switch(input$Data,
                                "Weekly" = cDat,
                                "Monthly" = cDat1)
    cDat %>% filter(MANUFACTURER %in% input$campaign &
                      DrugClass %in% input$campaign1 &
                      DrugType %in% input$campaign2 )
  })


  selectedData <- reactive({

    filteredData() %>% 
      select_(.dots = input$columns)
  })
  output$mytable1 <- renderDataTable({
    selectedData()
  })
}


####################### UI.R ##############
ui <- fluidPage(
  # Application title
  titlePanel("Example"),
  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel( selectInput("Data", "Choose data:", 
                              choices =  c("Weekly","Monthly"), 
selected =         "Weekly"),
      checkboxGroupInput('campaign', 'Manufacturer',
                     MANUFACTURER_list,
                     selected = MANUFACTURER_list),
  checkboxGroupInput('campaign1', 'DrugClass',
                     DrugClass_list,
                     selected = DrugClass_list),
  checkboxGroupInput('campaign2', 'DrugType',
                     DrugType_list,
                     selected = DrugType_list),
  checkboxGroupInput('columns', 'Columns',
                     columns,
                     selected = columns)
    ),


    ##### Show data in Main Panel  
    # Show a plot of the generated distribution
    mainPanel(
  dataTableOutput('mytable1'))
  )
)
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

# Assigning Packages
library(shiny)
library(dplyr)
library(DT)


header <- dashboardHeader(


#######################################################################################
# Importing Data
#######################################################################################
cDat <- read.table(file = "data/sample_wkly.csv", sep = ",",  header = 
                 TRUE, row.names = NULL)
cDat1 <- read.table(file = "data/sample_mthly.csv", sep = ",",  header = 
                 TRUE, row.names = NULL)

data_sets <- c("cDat", "cDat1")
#######################################################################################
###### Preparing list for Checkbox & Change rownames
MANUFACTURER_list <- unique(cDat$MANUFACTURER)
DrugClass_list <- unique(cDat$DrugClass)
DrugType_list <- unique(cDat$DrugType)
columns <- names(cDat)

####################### Server.R##########
server <- function(input, output, session) {

  filteredData <- reactive({  if(is.null(input$dataset))
    return()
    cDat <- get(input$dataset)
    cDat %>% filter(MANUFACTURER %in% input$campaign |
                  DrugClass %in% input$campaign1 |
                  DrugType %in% input$campaign2 )
  })


  selectedData <- reactive({

    filteredData() %>% 
      select_(.dots = input$columns)
  })
  output$mytable1 <- renderDataTable({

    selectedData()

  })
}

####################### UI.R ##############
ui <- fluidPage(
  # Application title
  titlePanel("Forecasting Engine"),
  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel( 
   checkboxGroupInput('campaign', 'Manufacturer',
                     MANUFACTURER_list,
                     selected = MANUFACTURER_list),
  checkboxGroupInput('campaign1', 'DrugClass',
                     DrugClass_list,
                     selected = DrugClass_list),
  checkboxGroupInput('campaign2', 'DrugType',
                     DrugType_list,
                     selected = DrugType_list),
  checkboxGroupInput('columns', 'Columns',
                     columns,
                     selected = columns),
  selectInput("dataset", "Select Frequency", as.list(data_sets))
    ),


    ##### Show data in Main Panel  
    # Show a plot of the generated distribution
    mainPanel(
      dataTableOutput('mytable1'))
  )
)
shinyApp(ui = ui, server = server)