【问题标题】:Shiny App: Error in filter_impl: Result must have length 4090, not 0闪亮的应用程序:filter_impl 中的错误:结果的长度必须为 4090,而不是 0
【发布时间】:2019-01-11 21:09:46
【问题描述】:

尝试运行闪亮的应用程序,但不断收到错误:filter_impl 中的错误:结果的长度必须为 4090,而不是 0

我试过了:

  • 通过删除单个过滤器进行调试以尝试隔离问题。

  • 使用dplyr::filter强制dplr的过滤器

  • 确保所有过滤器都在反应函数中

  • 检查是否是 ui.R 和 server.r 之间共享输入的问题

  • 检查是否是由之前的df转换引起的。

花了大约 3 个小时试图找到原因,但没有成功。

你能帮忙吗?

Server.R

rm(list = ls())

library(shiny)
library(tidyverse)
library(shiny)
library(ggplot2)
library(singer)
library(ggvis)
library(dplyr)


# Set Up DataFrames
data(package = "singer")
data(singer_locations)
sdf <- singer_locations %>% filter(year != 0) # filter out songs with missing years for simplicity
sdf %>% skim() %>% kable() # Check to see missing and incomplete values
sdf <- sdf %>% filter(complete.cases(.)) # filter out songs with missing observations for simplicity
sdf %>% skim() %>% kable() # Check to see if missing and incomplete values have been ignored

sdf <- sdf %>% select(
  track_id, title, song_id, release, artist_id, artist_name, year, duration, 
  artist_hotttnesss, artist_familiarity, name, city, longitude, latitude
)

# add new columns with rounded data (for nicer graphs later)
sdf$latitude_rounded <- round(sdf$latitude, 0)
sdf$longitude_rounded <- round(sdf$longitude, 0)
sdf$duration_rounded <- round(sdf$duration, 0)


# Add song_popularity & very_popular_song columns
pops <- sdf$artist_hotttnesss + sdf$artist_familiarity
sdf$artist_popularity <- round(pops, 0)
sdf$very_popular_song <- round(sdf$artist_popularity)
sdf$very_popular_song[sdf$very_popular_song < 1] <- "No"
sdf$very_popular_song[sdf$very_popular_song >= 1] <- "Yes"

# Select() relevant variables so they can be passed into server below (without having to use df[,"VAR"])
songs_list <- sdf %>% select(
  track_id, title, song_id, release, artist_id, artist_name, year, duration_rounded, duration, 
  artist_hotttnesss, artist_familiarity, name, city, latitude_rounded, longitude_rounded, longitude,
  latitude, artist_popularity, very_popular_song
)

#axis_variables <- reactive({
axis_variables <- c(
  "Length of Song (Seconds)" = "duration_rounded",
  "Rating" = "artist_hotttnesss",
  "Rating" = "artist_familiarity",
  "Year" = "year",
  "Popularity Rating" = "artist_popularity"
)

################################### SHINY SERVER #########################################
function(input, output) {

  songs <- reactive({  # Create Reactive Filtering Component
    duration_s <- input$duration_s
    artist_hotttnesss_s <- input$artist_hotttnesss_s
    artist_familiarity_s <- input$artist_familiarity_s
    latitude_s <- input$latitude_s
    longitude_s <- input$longitude_s
    year_s <- input$year_s
    artist_popularity_s <- input$artist_popularity_s


    # Apply filters
    songs_df <- songs_list %>%
      dplyr::filter(
        duration_rounded >= duration_s,
        artist_hotttnesss >= artist_hotttnesss_s,
        artist_familiarity >= artist_familiarity_s,
        latitude_rounded >= latitude_s,
        longitude_rounded >= longitude_s,
        year >= year_s,
        artist_popularity >= artist_popularity_s
      ) %>%
      arrange(duration_rounded)

    # filter by city option
    if (input$city_in != "All") {
      city_in_temp <- paste0("%", input$city_in, "%")
      songs_df <- songs_df %>% dplyr::filter(songs_df$city %like% city_in_temp)
    }

    # filter by artist_name option 
    if (input$artist_name_in != "" && !is.null(input$artist_name_in)) {
      artist_name_temp <- paste0("%", input$artist_name_in, "%")
      songs_df <- songs_df %>% dplyr::filter(songs_df$artist_name %like% artist_name_temp)
    }

      songs_df <- as.data.frame(songs_df)
      songs_df # return df

    })


  # search fuction
  song_search <- function(s) {
    if (is.null(s)) return(NULL)
    if (is.null(s$track_id)) return(NULL)

    # Isolate the given ID
    songs_df <- isolate(songs())
    temp_song <- songs_df[songs_df$track_id == s$track_id, ]

    paste0("<b>", temp_song$artist_name, "</b><br>",
           temp_song$year, "<br>",
           "popularity ", format(temp_song$artist_popularity, big.mark = ",", scientific = FALSE)
    )
  }

  # A reactive expression with the ggvis plot
  vis <- reactive({
    # setting variablex & variabley (input names are type str)
    variablex <- prop("x", as.symbol(input$variablex))    
    variabley <- prop("y", as.symbol(input$variabley))

    # Lables for axes
    xvar_name <- names(axis_variables)[axis_variables == input$variablex]
    yvar_name <- names(axis_variables)[axis_variables == input$variabley]

    songs %>%
      ggvis(x = variablex, y = variabley) %>%
      layer_points(size := 50, size.hover := 200,
                   fillOpacity := 0.2, fillOpacity.hover := 0.5,
                   stroke = ~artist_popularity, key := ~artist_name) %>%
      add_tooltip(song_search, "hover") %>%
      add_axis("x", title = xvar_name) %>%
      add_axis("y", title = yvar_name) %>%
      add_legend("stroke", title = "Very Popular", values = c("Yes", "No")) %>%
      scale_nominal("stroke", domain = c("Yes", "No"),
                    range = c("orange", "#aaa")) %>%
      set_options(width = 500, height = 500)
  })

  vis %>% bind_shiny("plot1")
  output$songs_selected <- renderText({ nrow(songs()) })

}

Ui.R

rm(list = ls())

library(tidyverse)
library(shiny)
library(ggplot2)
library(singer)
library(ggvis)
library(dplyr)


#axis_variables <- reactive({
axis_variables <- c(
    "Length of Song (Seconds)" = "duration_rounded",
    "Hotness Rating" = "artist_hotttnesss",
    "Familiarity Rating" = "artist_familiarity",
    "Year" = "year",
    "Popularity Rating" = "artist_popularity"
)

# Define UI for application that draws a histogram
shinyUI(fluidPage(
  shinythemes::themeSelector(),
  titlePanel("Artist & Song Data"),
  fluidRow(
    column(3,
           wellPanel(
             h4("Filter By"),
             # Slider Options for Data Exploration
             sliderInput("duration_s", "Minimum duration of song (seconds)", 10, 500, 100, step = 10),
             sliderInput("year_s", "Year released", 1900, 2018, value = c(1980, 2018)),
             sliderInput("artist_hotttnesss_s", "Ranking / 10 for popularity", 0, 2, 0, step = 0.1),
             sliderInput("artist_familiarity_s", "Ranking / 10 for familiarity", 0, 2, 0, step = 0.1),
             sliderInput("artist_popularity", "Ranking / 10 for familiarity", 0, 2, 0, step = 0.1),


             # Filter by custom input condition
             textInput("city_in", "Name of the city"),
             textInput("artist_name_in", "Artist's name contains (e.g Pink f)")
           ),
           wellPanel(
             selectInput("variablex", "X-axis", axis_variables, selected = "year"),
             selectInput("variabley", "Y-axis", axis_variables, selected = "duration_rounded")
           )
    ),

    column(9,
           ggvisOutput("plot1"),
           wellPanel(
             span("Degrees of Freedom",
                  textOutput("songs_selected")
             )
           )
    )
    )

【问题讨论】:

  • 在将其移植到 Shiny 之前,您是否尝试过让它在 Shiny 上下文之外运行?
  • 给定的示例代码太长。绘图,轴名称部分对于调试真的有必要吗?你能把它只减少到必要的部分吗?
  • @RomanLuštrik - 是的,我有,没有占上风。
  • @PoGibas - 我不确定问题出在哪里,所以我将其全部包含在内。
  • 您需要将其归结为重现错误所需的绝对最少代码。可能不需要用闪亮的包装。

标签: r filter shiny dplyr


【解决方案1】:

您似乎正在使用input$XXX 创建的数据进行过滤。尝试将req(input$XXX, req(input$YYY, ...) 放在反应元素的开头。

另请阅读 this tweet 关于以 rm(list = ls()) 开头的信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 2016-06-02
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多