R ShinyApp错误:选择输入,数据子集

R ShinyApp错误:选择输入,数据子集,r,data.table,shiny,subset,shinydashboard,R,Data.table,Shiny,Subset,Shinydashboard,我正在创建闪亮的应用程序。我的目标是根据输入可视化一些数据切片。我对结果非常满意。 但是,我的应用程序在加载时有一些错误。在绘制图形和可视化输入之前,它会在屏幕上显示一些错误(您可以启动应用程序并查看问题) 我认为,第一个问题是数据过滤。我不知道该怎么处理,问题是什么。我是否需要使用其他方法或其他软件包?(请参见输出$Brand) grep(模式、级别(向量))中出错:“模式”参数无效 第二个错误出现在我创建selectInput时。我想在一个绘图中可视化特定类别的所有品牌,并可以选择按品牌过滤

我正在创建闪亮的应用程序。我的目标是根据输入可视化一些数据切片。我对结果非常满意。 但是,我的应用程序在加载时有一些错误。在绘制图形和可视化输入之前,它会在屏幕上显示一些错误(您可以启动应用程序并查看问题)

我认为,第一个问题是数据过滤。我不知道该怎么处理,问题是什么。我是否需要使用其他方法或其他软件包?(请参见
输出$Brand

grep(模式、级别(向量))中出错:“模式”参数无效

第二个错误出现在我创建
selectInput
时。我想在一个绘图中可视化特定类别的所有品牌,并可以选择按品牌过滤数据。然而,我的方法并不奏效。有什么建议吗?(请参见
输出$Brand

if(input$Brand==“All”)中的
错误{:参数的长度为零

另外,我附上了代码,您可以生成它

你对如何简化代码还有什么建议吗

谢谢你的帮助

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)
库(闪亮)
图书馆(shinydashboard)
库(数据表)
图书馆(GG2)
图书馆(网格)
图书馆(比例尺)
图书馆(主题)
#标题-----------------------------------------------------------

header下面应该消除这些错误:对于#1,函数
datatable
中的
发出错误,因此我将其改为
%in%
。对于#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)
rm(list=ls())
图书馆(闪亮)
图书馆(shinydashboard)
库(数据表)
图书馆(GG2)
图书馆(网格)
图书馆(比例尺)
图书馆(主题)
#标题-----------------------------------------------------------

感谢您的回答。还有一个关于子集的问题。功能
like
允许部分匹配。是否有其他方法进行部分匹配?通常%
中的
%工作得很好,并且适用范围很广。或者您可以查看
匹配
子集
。还有许多版本在诸如
ddplyr
data.table
sqldf
这样的包中,我建议使用
data.table
包,因为在某些情况下,它的性能优于其他包,并且在数据表操作中提供了面包功能。除非您如果你正在做计算密集型的任务,那么最好使用与数据框架相反的列表和矩阵。我在这里发现了另一个问题:
df2