2017-01-02 76 views
0

我有以下数据集时:无类错误创建一个闪亮的应用

Type <- c("Choice 1", "Choice 1", "Choice 1", "Choice 1", "Choice 1", 
    "Choice 1") 
Date <- c("02-02-2016", "02-03-2016", "02-04-2016", "02-05-2016", 
    "02-06-2016", "02-07-2016") 
Sentiment <- c(1, 2, 3, 4, 2, 3) 
df <- data.frame(Type, Date, Sentiment) 

现在我建立一个闪亮的应用程序,允许您过滤日期范围,并选择一个类型。那么应该给你一个子集中所有情感值的直方图。

因此,我创建了下面的闪亮代码

df <- read.csv2("sample.csv", stringAsFactors = F) 
df$Date <- as.Date(df$Date, format = "%d-%m-%Y") 

library(shiny) 
ui <- fluidPage(tabsetPanel(
    #Sliders for the first panel 
    tabPanel("Tab 1",sidebarPanel(
     dateRangeInput("daterange1", "Date range:", 
        start = "2015-01-01", 
        end = "2015-12-31"), 
     selectInput("select", label = h3("Select box"), 
     choices = list("Choice 1" = 1,"Choice 2" = 2,"Choice 3" = 3), 
     selected = 1)), 
    mainPanel(plotOutput("coolplot"))), 

    #Sliders for the second panel 
    tabPanel("Tab 2", mainPanel("the results of tab2")) 
)) 
server <- function(input, output) { 
    filtered <- reactive({ 
    if (is.null(input$select)) { 
     return(NULL) } 
    df %>% filter(Date >= input$dateRangeInput[1], 
      Date <= input$dateRangeInput[2], 
      Type == input$select) 
    }) 

    output$coolplot <- renderPlot({ 
    ggplot(filtered(), aes(Sentiment)) + geom_histogram() 
    }) 
} 
shinyApp(ui = ui, server = server) 

然而,当我跑我得到以下错误:

incorrect length (0), expecting: 10 

,我应该怎样做才能避免这个错误有什么想法?

+0

'ggplot(过滤,AES(观点))'应该是'ggplot(过滤(),AES(观点) )',因为'filtered'是一个**函数**返回一些东西。 – nrussell

+0

非常感谢nrussell!它带来了这个bug。直到现在,我遇到了以下错误...(见编辑)。有什么想法吗? –

+0

'gincorrect长度(0),期望:10'是* *完全错误消息? – nrussell

回答

-1

这是一个典型的Shiny初始化错误。定义任何输入之前,每个反应得到在开始执行时被调用一次,所以它们都是空在这一点上,这会导致各种不同的错误 - 而且很难诊断,如果你不知道这个问题的。

相当近一段时间,除了Shiny(req函数)之外,这个函数更容易解决。只需添加一个:

req(input$dateRangeInput); 

在你filter反应代码在你的第一道防线。

并且记住在任何时候你有反应。事实上,你还需要其他地方有你直接在observeoutput代码块使用input$something结构,像在那里你直接使用input$something

您需要闪亮的版本0.13.0或更好地为这一点。如果你有闪亮的旧版本,你有以下形式的语句来保护您的代码:

if (!is.null(input$something)){ 
    #your code that needs input$something 
} 
+0

为什么反对票? –