部署Shiny app时出错:警告:data.frame中的错误:参数意味着行数不同:2,20268

时间:2018-05-23 06:02:58

标签: r shiny

编辑:似乎问题可能来自ggridges软件包,这与Shiny一起工作吗?

我已经构建了一个R Shiny应用程序,在本地工作正常但在我尝试部署它时会崩溃

我认为问题出在下拉菜单上,但我不确定。我有一个数据框,为两个图提供数据,每次进行选择时反应事件都会产生一个临时数据帧(包括没有选择和使用所有数据时)。我彻底浏览了网页和Stack Overflow,但还没有设法解决它。

当我尝试部署应用程序时,浏览器会打开并显示下拉菜单,但图表却没有。检查Shiny中的日志显示了帖子标题中包含的错误消息。

感谢任何帮助,如果需要,我可以提供更多信息。

以下是我的代码。

谢谢!

UI:

library(shiny)

shinyUI(fluidPage(

  titlePanel("test"),

  inputPanel(
    selectInput("region", 
                "Choose a region:", 
                choices = c("All Regions", "Midwest", "Northeast", "South", "West"),
                selected = "All Regions"),
    selectInput("year", 
                "Choose a year:", 
                choices = c("All Years", sapply(1999:2016, as.character)),
                selected = "All Years")
  ),
  plotOutput("plot")
)
)

服务器:

library(shiny)
library(readr)
library(tidyr)
library(dplyr)
library(forcats)
library(ggplot2)
library(ggridges)
library(cowplot)
library(magrittr)
theme_set(theme_grey())

drug_eco <- read_csv("drug_eco.csv")

usa_region <- read_csv("usa_region.csv")

drug_eco[,1:2] <- drug_eco[,1:2] %>% lapply(factor)
drug_eco <- drug_eco %>% 
  mutate(drug_deaths_per_100k = round(drug_deaths/(population/100000), 1))

colnames(usa_region) <- colnames(usa_region) %>% tolower()
usa_region[,1:4] <- usa_region[,1:4] %>% lapply(factor)

drug_eco <- drug_eco %>%  
  left_join(usa_region[,c(1,3)], by = "state")

drug_eco_long <- drug_eco %>% 
  select(c(1:2,5:7)) %>% 
  gather(variable, value, -c(1:2,5))

drug_eco_long$variable <- drug_eco_long$variable %>% 
  factor(levels = c("drug_deaths_per_100k", "unemployment_rate"),
         labels = c("Drug Related Deaths per 100k Population", "Unemployment Rate %"))

fix_x <- data.frame(
  value = c(0, 55, 0, 15),
  year = c(1, 18, 1, 18),
  variable = c(rep("Drug Related Deaths per 100k Population", 2),
               rep("Unemployment Rate %", 2))
)

gfc_annotation <- data.frame(
  value = 45, 
  year = "2010", 
  variable = "Drug Related Deaths per 100k Population",
  label = "GFC"
) 

shinyServer(function(input, output) {

  density_df <- reactive({
    if (input$region == "All Regions") {
      temp_1 <- drug_eco_long
    } else {
      temp_1 <- filter(drug_eco_long, region == input$region)
    }
  })

  point_df <- reactive({
    if (input$region == "All Regions" & input$year == "All Years") {
      temp_2 <- drug_eco_long
    } else if (input$region != "All Regions" & input$year == "All Years") {
      temp_2 <- filter(drug_eco_long, region == input$region)
    } else if (input$region == "All Regions" & input$year != "All Years") {
      temp_2 <- filter(drug_eco_long, year == input$year) 
    } else {
      temp_2 <- filter(drug_eco_long, region == input$region & year == input$year)
    }
  })

  output$plot <- renderPlot({
    p1 <- ggplot(density_df(), aes(value, year, fill = variable)) + 
      geom_density_ridges(jittered_points = T, 
                          position = position_points_jitter(width = 0.05, height = 0),
                          point_shape = "|",
                          point_size = 2.5,
                          size = 0.5, 
                          scale = 1.5, 
                          alpha = 0.8, 
                          rel_min_height = 0.005) +
      scale_fill_brewer(palette = "Set2") +
      facet_wrap(~variable, scales = "free_x") +
      geom_blank(data = fix_x) +
      scale_x_continuous(limits = c(0, NA)) +
      annotate("rect", ymin = 10, ymax = 14, xmin = 0, xmax = Inf, alpha = 0.1) +
      geom_text(data = gfc_annotation, aes(label = label), size = 5) +
      labs(title = "test",
           subtitle = "",
           x = "", 
           y = "Year") +
      theme(plot.title = element_text(hjust = 0.5),
            legend.position = "none") 

    p2 <- ggplot(point_df(), 
                 aes(fct_reorder2(state, -variable, -value), value, colour = variable)) +
      geom_point(size = 3, alpha = 0.8) +
      scale_colour_brewer(palette = "Set2") +
      scale_y_continuous(limits = c(0, 50)) +
      coord_flip() +
      labs(title = "test", 
           subtitle = "test", 
           x = "State (inc. DC)", 
           y = "", 
           colour = "Variable") +
      theme(plot.title = element_text(hjust = 0.5),
            legend.position = "bottom") +
      guides(colour = guide_legend(title.position = "top", title.hjust = 0.5))

    plot_grid(p1, p2)
  })
})

1 个答案:

答案 0 :(得分:0)

我只是使用Rstudio面对这个问题。我决定更新软件包,然后上传应用程序。它起作用了!