Ошибка замены имеет 1 строку, данные имеют 0 в блестящем приложении - PullRequest
0 голосов
/ 07 апреля 2020

Я пытаюсь повторно связать два фрейма данных в блестящем, так что я могу построить их вместе, но я получаю замену error "1 строка, данные имеют 0" , когда я делаю. Я включил мое полное приложение ниже - и я предполагаю, что ошибка возникает в следующих строках сервера:

# create new cases and new deaths data frames and add a New "NAME" value to each row
ncdf <- cd[,c(1,4)]
nddf <- cd[,c(1,5)]
ncdf$name <- "New Cases"
nddf$name <- "New Deaths"

Приложение работает локально даже с ошибкой, но когда я пытаюсь загрузить его в shinyapps, оно делает не.

global.R
library(RCurl)
library(dplyr)
library(ggplot2)
library(data.table)
library(ggthemes)
library(plotly)
library(DT)

# Pull the data from NYT github and turn it into a data frame
x <- getURL("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv")
csv <- read.csv(text=x)
df <- as.data.frame(csv)

# Create a sorted list of unique states
state_list <- as.character(levels(df$state))
ui.R
ui <- fluidPage(
  title = "Covid-19 Cases/Deaths",
  headerPanel("Covid-19 Cases/Deaths by County"),
  fluidRow(
    column(3, uiOutput("sel_state")),
    column(4, conditionalPanel(condition = "input.state.length > 0", uiOutput("sel_county")))
    ),
  plotOutput("plot"),
  hr(),
)

server.R
function(input, output, session) {

# create a list of selected counties based on which State is selected
  selected_state_counties <- reactive({
    req(length(input$state) > 0)
    df %>% 
      filter(state == input$state) %>% 
      pull(county) %>% 
      as.character()
  })

# create the state drop down menu based on the state list from global
  output$sel_state <- renderUI({
    selectizeInput('state', 'Select a State', choices=c("Choose One" = "", state_list))
  })

# create the county drop down menu based on the selected state above
  output$sel_county <- renderUI({
    selectizeInput('county', 'Select a County', choices=c("Choose One" = "", selected_state_counties()))
  })

# create a new data frame of just the selected county data
  tab <- reactive({
    df %>%
      filter(state == input$state) %>%
      filter(county == input$county)
  })

# plot the specific county data
  output$plot <- renderPlot({

    # turn tab into a data table for calculation
    cd <- as.data.table(tab())

    ## add and calculate 'new_cases' & 'new_deaths' columns
    cd[, new_cases := cases - c(0, head(cases, -1))]
    cd[, new_deaths := deaths - c(0, head(deaths, -1))]

    # convert to data frame
    cd <- as.data.frame(cd)

    # strip unused data
    cd <- cd[, c(1,5,6,7,8)]

    # mutate fields to as date > date, and all other columns as numerical
    cd <- mutate(cd, date=as.Date(date), cases=as.numeric(cases), deaths=as.numeric(deaths), new_cases=as.numeric(new_cases), new_deaths=as.numeric(new_deaths))

    # create new cases and new deaths data frames and add a New "NAME" value to each row
    ncdf <- cd[,c(1,4)]
    nddf <- cd[,c(1,5)]
    ncdf$name <- "New Cases"
    nddf$name <- "New Deaths"

    #rename the column with the count in it to cases for both data frames
    ncdf <- ncdf %>% rename(cases = new_cases)
    nddf <- nddf %>% rename(cases = new_deaths)

    # rbind both 'new' data frames together to be plotted as ndf
    ndf <- rbind(ncdf, nddf)

    ## line plot colors/legend
    lines <- c('Cases' = 'lightskyblue3', 'Deaths' = 'lightcoral')
    ## bar plot colors/legend
    bars <- c('New Cases' = 'steelblue1', 'New Deaths' = 'tomato1')
    ## chart title
    chart_title <- paste(input$county, "County - Covid-19 Cases/Deaths")

    ## define plot formats as 'p' and add formatting/titles  
    p <- ggplot() + labs(title=chart_title, x="Date", color="Legend", fill="") + theme(legend.position="bottom", plot.title = element_text(hjust = 0.5), axis.text.x = element_text(angle = 90)) + scale_color_manual(values = lines) + scale_fill_manual(values = bars) + scale_y_continuous(name="Cases", labels = scales::number_format(accuracy = 1))

    ## plot cases/deaths as lines from cd data frame; plot new_cases/new_deaths as bar from ndf data frame; using 'p' for formatting
    p + geom_line(data=cd, aes(date, cases, group=1, color='Cases'), size=1) + geom_line(data=cd, aes(date, deaths, group=2, color='Deaths'), size=1) + geom_bar(data=ndf, aes(date, cases, fill=name), stat="identity", width=0.5, position = 'dodge') + geom_bar(data=ndf, aes(date, cases, fill=name), stat = 'identity', width=0.5, position="dodge") 

  })
}

...