2015-10-13 56 views
0

我正在創建閃亮的應用程序。我的目標是根據輸入可視化一些數據切片。我對結果非常滿意。 但是,當應用程序加載時,我的應用程序有一些錯誤。繪製圖形和可視化輸入之前,它會在屏幕上顯示一些錯誤(您可以激活應用程序並查看問題)。ShinyApp錯誤:selectInput,數據子集

我認爲,第一個問題是數據過濾。我無法弄清楚如何處理這個問題,這是什麼問題。我需要使用其他方法還是其他包? (見output$Brand)。

Error in grep(pattern, levels(vector)) : invalid 'pattern' argument

第二個錯誤,當我創建selectInput來。我想在一個地塊中將特定類別的所有品牌可視化,並且可以選擇按品牌過濾數據。但是,我的方法運行不正常。有什麼建議麼? (見output$Brand)。

Error in if (input$Brand == "All") { : argument is of length zero

此外,我附上了代碼,您可以生成。

可否有更多的建議如何簡化代碼?

感謝您的幫助!

library(shiny) 
library(shinydashboard) 
library(data.table) 
library(ggplot2) 
library(grid) 
library(scales) 
library(ggthemes) 



# Header ----------------------------------------------------------- 

header <- dashboardHeader(title="Dashboard") 

# Sidebar -------------------------------------------------------------- 

sm <- sidebarMenu(
    menuItem(
    text="Graph1", 
    tabName="Graph1", 
    icon=icon("home") 
    ) 
) 

sidebar <- dashboardSidebar(sm) 

# Body -------------------------------------------------- 

body <- dashboardBody(

# Layout -------------------------------------------- 

tabItems(
tabItem(
    tabName="Graph1", 

    fluidPage(
     fluidRow(

     box(
     title = "Inputs", status = "warning", width = 2, solidHeader = TRUE, 

     uiOutput("Year"), 
     uiOutput("Category"), 
     uiOutput("Brand"), 
     sliderInput("Finalas.Range", "Months:", 
        min = 1, max = 12, value = c(1,12)) 

     ), 

     box(
     title = "Season", width = 10, status = "info", solidHeader = TRUE, 

     plotOutput("Graph1") 

    ) 
) 
) 
) 
) 
) 

# Setup Shiny app UI components ------------------------------------------- 

ui <- dashboardPage(header, sidebar, body, skin="black") 

# Setup Shiny app back-end components ------------------------------------- 

server <- function(input, output) { 

# Generate data -------------------------------------- 

    set.seed(1992) 
    n=99 
    Year <- sample(2013:2015, n, replace = TRUE, prob = NULL) 
    Month <- sample(1:12, n, replace = TRUE, prob = NULL) 
    Category <- sample(c("Car", "Bus", "Bike"), n, replace = TRUE, prob = NULL) 
    Brand <- sample("Brand", n, replace = TRUE, prob = NULL) 
    Brand <- paste0(Brand, sample(1:14, n, replace = TRUE, prob = NULL)) 
    USD <- abs(rnorm(n))*100 

    df <- data.frame(Year, Month, Category, Brand, USD) 



    # Inputs -------------------------------------- 
    output$Year <- renderUI({ 
    selectInput("Year", 
      "Year:", 
      c(unique(as.character(df$Year))), selected = "2015") 
    }) 


    output$Category <- renderUI({ 
    selectInput("Category", "Choose category:", 
      choices = c("Car","Bus", "Bike")) 
    }) 


    output$Brand <- renderUI({ 
    df2 <- (data.table(df))[like(df$Category,input$Category)] 
    selectInput("Brand", 
      "Brand:", 
      c("All", unique(as.character(df2$Brand)))) 
    }) 


    # Plot -------------------------------- 

    output$Graph1 <- renderPlot({ 

df <- data.table(df) 

     if (input$Brand == "All") { 

     df <- df[like(df$Year, input$Year)] 
     df <- df[like(df$Category,input$Category)] 

     ggplot(df, aes(x=factor(Month,levels=1:12), y=USD, fill=Brand))+ 
      geom_bar(stat='identity')+ 
      scale_x_discrete('Month', breaks=factor(1:12), drop=FALSE)+ 
      scale_fill_gdocs(guide = guide_legend(title = "Brand")) 

     } else { 


     df <- df[like(df$Year, input$Year)] 
     df <- df[like(df$Category,input$Category)] 
     df <- df[which(df$Brand == input$Brand),] 

     validate(
      need(sum(df$USD)>0, paste(input$Brand, "was inactive in Year:",input$Year)) 
     ) 

     ggplot(df, aes(x=factor(Month,levels=1:12), y=USD, fill=Brand))+ 
      geom_bar(stat='identity')+ 
      scale_x_discrete('Month', breaks=factor(1:12), drop=FALSE) 
     } 

    }) 

# ----------------------------------------------------------------------------- 

} 

# Render Shiny app -------------------------------------------------------- 

shinyApp(ui, server) 

回答

2

下應該消除這些錯誤:所以我改成了%in%代替datatable爲#1的功能like給出了錯誤。和#2你有null作爲默認設置,以便照顧與一個if聲明

rm(list = ls()) 
library(shiny) 
library(shinydashboard) 
library(data.table) 
library(ggplot2) 
library(grid) 
library(scales) 
library(ggthemes) 


# Header ----------------------------------------------------------- 

header <- dashboardHeader(title="Dashboard") 

# Sidebar -------------------------------------------------------------- 

sm <- sidebarMenu(
    menuItem(
    text="Graph1", 
    tabName="Graph1", 
    icon=icon("home") 
) 
) 

sidebar <- dashboardSidebar(sm) 

# Body -------------------------------------------------- 

body <- dashboardBody(

    # Layout -------------------------------------------- 

    tabItems(
    tabItem(
     tabName="Graph1", 

     fluidPage(
     fluidRow(

      box(
      title = "Inputs", status = "warning", width = 2, solidHeader = TRUE, 

      uiOutput("Year"), 
      uiOutput("Category"), 
      uiOutput("Brand"), 
      sliderInput("Finalas.Range", "Months:", 
         min = 1, max = 12, value = c(1,12)) 

     ), 

      box(
      title = "Season", width = 10, status = "info", solidHeader = TRUE, 

      plotOutput("Graph1") 

     ) 
     ) 
    ) 
    ) 
) 
) 

# Setup Shiny app UI components ------------------------------------------- 

ui <- dashboardPage(header, sidebar, body, skin="black") 

# Setup Shiny app back-end components ------------------------------------- 

server <- function(input, output) { 

    # Generate data -------------------------------------- 

    set.seed(1992) 
    n=99 
    Year <- sample(2013:2015, n, replace = TRUE, prob = NULL) 
    Month <- sample(1:12, n, replace = TRUE, prob = NULL) 
    Category <- sample(c("Car", "Bus", "Bike"), n, replace = TRUE, prob = NULL) 
    Brand <- sample("Brand", n, replace = TRUE, prob = NULL) 
    Brand <- paste0(Brand, sample(1:14, n, replace = TRUE, prob = NULL)) 
    USD <- abs(rnorm(n))*100 

    df <- data.frame(Year, Month, Category, Brand, USD) 



    # Inputs -------------------------------------- 
    output$Year <- renderUI({ 
    selectInput("Year", 
       "Year:", 
       c(unique(as.character(df$Year))), selected = "2015") 
    }) 


    output$Category <- renderUI({ 
    selectInput("Category", "Choose category:", 
       choices = c("Car","Bus", "Bike")) 
    }) 


    output$Brand <- renderUI({ 


    # first error 
    #df2 <- (data.table(df))[like(df$Category,input$Category)] 

    df2 <- df[df$Category %in% input$Category,] 


    selectInput("Brand", 
       "Brand:", 
       c("All", unique(as.character(df2$Brand)))) 
    }) 


    # Plot -------------------------------- 

    output$Graph1 <- renderPlot({ 

    df <- data.table(df) 

    if(is.null(input$Brand) || is.na(input$Brand)){return()} 

    else if (input$Brand == "All") { 

     df <- df[like(df$Year, input$Year)] 
     df <- df[like(df$Category,input$Category)] 

     ggplot(df, aes(x=factor(Month,levels=1:12), y=USD, fill=Brand))+ 
     geom_bar(stat='identity')+ 
     scale_x_discrete('Month', breaks=factor(1:12), drop=FALSE)+ 
     scale_fill_gdocs(guide = guide_legend(title = "Brand")) 

    } else { 


     df <- df[like(df$Year, input$Year)] 
     df <- df[like(df$Category,input$Category)] 
     df <- df[which(df$Brand == input$Brand),] 

     validate(
     need(sum(df$USD)>0, paste(input$Brand, "was inactive in Year:",input$Year)) 
    ) 

     ggplot(df, aes(x=factor(Month,levels=1:12), y=USD, fill=Brand))+ 
     geom_bar(stat='identity')+ 
     scale_x_discrete('Month', breaks=factor(1:12), drop=FALSE) 
    } 

    }) 

    # ----------------------------------------------------------------------------- 

} 

# Render Shiny app -------------------------------------------------------- 

shinyApp(ui, server) 
+0

感謝您的回答。還有另外一個關於修改的問題。 功能'like'允許部分匹配。是否有另一種方法來進行部分匹配? – AK47

+0

通常'%in%'工作得很好,並且可以廣泛應用。或者,您可以查看「匹配」或「子集」。在'ddplyr','data.table','sqldf'等軟件包中還有很多版本可以完成子集引用,我建議使用'data.table'軟件包,因爲它在某些情況下優於其他,並在數據表操作中提供麪包功能。不要花太多時間進行子設置,除非你做了非常計算密集的任務,那麼它更好地處理列表和矩陣與數據幀相反 –

+0

我在這裏發現了另外一個問題: 'df2 < - df [ df $ Category%in%input $ Category,] df2 < - (data.table(df))[like(df $ Category,input $ Category)]' 第一行很好。 但是,我喜歡做的是過濾部分匹配。例如,當我選擇'Midfielder'過濾器時,必須包含'Midfielder/Striker'。 「like」函數完成這項工作,但是當應用程序加載時出現錯誤。有什麼建議麼? – AK47