2017-09-27 57 views
0

我有以下Shiny代碼。我想用這個切片數據集,並從該數據子集創建一個圖形。使用輸入變量在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 

在什麼位置出了問題有什麼想法?

回答

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) 
相關問題