使用输入变量在Shiny中切片数据集

时间:2017-09-27 07:21:00

标签: r shiny

我有以下闪亮的代码。我想使用此切片数据集并从该数据子集创建图形。

library(shiny)
library(ggplot2)
library(dplyr)

# Define UI for dataset viewer app ----
ui <- fluidPage(

  # App title ----
  titlePanel("Shiny Text"),

  # Sidebar layout with a input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Selector for choosing dataset ----
      selectInput(inputId = "dataset",
                  label = "Choose a dataset:",
                  choices = c("ga_data")),

      selectInput(inputId = "week",
                  label = "Choose a number:",
                  choices = c(21000, 23400, 26800)),

      # Input: Numeric entry for number of obs to view ----
      numericInput(inputId = "obs",
                   label = "Number of observations to view:",
                   value = 10)
    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Verbatim text for data summary ----
      verbatimTextOutput("summary"),

      # Output: HTML table with requested number of observations ----
      plotOutput("view")

    )
  )
)

# Define server logic to summarize and view selected dataset ----
server <- function(input, output) {


    employee <- c('John Doe','Peter Gynn','Jolie Hope')
    salary <- c(21000, 23400, 26800)
    startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))

    ga_data <- data.frame(employee, salary, startdate)
    ga_data %>%
    filter(salary == input$week)

  # Return the requested dataset ----
  datasetInput <- reactive({
    switch(input$dataset,
           "ga_data" = ga_data)
  })


  # Show the first "n" observations ----
  output$view <- renderPlot({

    ggplot(data=ga_data, aes(x=employee, y=salary)) + geom_bar(stat="identity")
    + ggtitle("input$week")

  })

}

# Create Shiny app ----
shinyApp(ui = ui, server = server)

有了这个,我希望能够根据输入列表过滤数据集。因此,如果我选择df应该被切片的数字。

然而,当我尝试它时,我得到:

invalid argument to unary operator

对这里出了什么问题的想法?

1 个答案:

答案 0 :(得分:0)

你好像没用,所以我把它留了出来:

datasetInput <- reactive({ switch(input$dataset, "ga_data" = ga_data) })

我也在服务器外部定义了数据框

library(shiny)
library(ggplot2)
library(dplyr)

employee <- c('John Doe','Peter Gynn','Jolie Hope')
salary <- c(21000, 23400, 26800)
startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
ga_data <- data.frame(employee, salary, startdate)

# Define UI for dataset viewer app ----
ui <- fluidPage(

  # App title ----
  titlePanel("Shiny Text"),

  # Sidebar layout with a input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Selector for choosing dataset ----
      selectInput(inputId = "dataset",
                  label = "Choose a dataset:",
                  choices = c("ga_data")),

      selectInput(inputId = "week",
                  label = "Choose a number:",
                  choices = c(21000, 23400, 26800)),

      # Input: Numeric entry for number of obs to view ----
      numericInput(inputId = "obs",
                   label = "Number of observations to view:",
                   value = 10)
    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Verbatim text for data summary ----
      verbatimTextOutput("summary"),

      # Output: HTML table with requested number of observations ----
      plotOutput("view")

    )
  )
)

# Define server logic to summarize and view selected dataset ----
server <- function(input, output) {

  data <- eventReactive(input$week,{
    ga_data[ga_data$salary %in% as.numeric(input$week),]
  })

  # Show the first "n" observations ----
  output$view <- renderPlot({
    ggplot(data= data(), aes(x=employee, y=salary)) + geom_bar(stat="identity")+ ggtitle(input$week)
  })

}

# Create Shiny app ----
shinyApp(ui = ui, server = server)