闪亮的应用程序UI闪烁然后消失没有ggplot图形

时间:2017-10-06 02:44:01

标签: r ggplot2 shiny

如果我的第一个闪亮的应用程序,我一直在尝试调试几天 - 我正在尝试创建一个闪亮的应用程序,使用折线图显示州的死亡率与全国平均值。每当我运行应用程序时,UI都会闪烁,页面会变为空白。我收到2条错误消息。任何建议将非常感谢

ui.R

library(shiny)
url < "https://raw.githubusercontent.com/indianspice/IS608/master/Assign3/cleaned-cdc-mortality-1999-2010-2.csv"
mdata <- read.csv(url, stringsAsFactors = FALSE)

#Extract unique states and disease
states <- unique(mdata$State)
causes <- unique(mdata$ICD.Chapter)

#Define UI for application that draws a histogram
shinyUI(fluidPage(

  #Application title
  titlePanel("Mortality Improvement by State"),

  #Dropdown state and cause
      sidebarLayout(
        sidebarPanel(selectInput("state_name", "State:", choices = states)
             , radioButtons("cause_name", "Select disease - cause of Death:",
                                c("Infectious & parasitic" = "Certain infectious and parasitic diseases"    , 
                              "Neoplasms" = "Neoplasms",
                              "Blood & blood-forming & immune mechanism disorders" = "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism",
                              "Endocrine, nutritional & metabolic" = "Endocrine, nutritional and metabolic diseases",
                              "Mental & behavioural disorders" = "Mental and behavioural disorders",
                              "Nervous system" = "Diseases of the nervous system",
                              "Ear & mastoid process" = "Diseases of the ear and mastoid process",
                              "Circulatory system"  = "Diseases of the circulatory system",
                              "Respiratory system" = "Diseases of the respiratory system",
                              "Digestive system" = "Diseases of the digestive system",
                              "Skin & subcutaneous tissue" = "Diseases of the skin and subcutaneous tissue",
                              "Musculoskeletal system & connective tissue" = "Diseases of the musculoskeletal system and connective tissue",
                              "Genitourinary system" = "Diseases of the genitourinary system",
                              "Pregnancy, childbirth & the puerperium" = "Pregnancy, childbirth and the puerperium",
                              "Conditions originating in the perinatal period" = "Certain conditions originating in the perinatal period",
                              "Congenital malformations, deformations & chromosomal abnormalities" = "Congenital malformations, deformations and chromosomal abnormalities",
                              "Symptoms, signs & abnormal clinical & laboratory findings, not classified" = "Symptoms, signs and abnormal clinical and laboratory findings, not elsewhere classified",
                              "External causes of morbidity & mortality" = "External causes of morbidity and mortality"))
   ),

# Show a plot of the generated distribution
    mainPanel(
       plotOutput("causev")
   )
  )
))

server.R

library(shiny)
library(ggplot2)
library(dplyr)

#Number per 50000
crude_rate <- 500000
url < "https://raw.githubusercontent.com/indianspice/IS608/master/Assign3/cleaned-cdc-mortality-1999-2010-2.csv"
mdata <- read.csv(url, stringsAsFactors = FALSE)

#Extract unique states and disease
#states <- unique(mdata$State) - remove
#causes <- unique(mdata$ICD.Chapter) remove

nat_cause <- aggregate(cbind(Deaths, Population) ~ ICD.Chapter + Year, mdata, FUN=sum)
#Add national average column
nat_cause$NatAvge <- round(nat_cause$Deaths / nat_cause$Population * crude_rate, 4)

#Define server logic required to visulaize the data
shinyServer(function(input, output, session) {

sub_mdata <- reactive({
        paste("Cause of Death: ", input$causes)


    output$caption <- renderText({
        print(input$causes)
        sub_mdata()
    })

sub_mdata <- reactive({
    subset(mdata, ICD.Chapter == input$causes & State == input$states,
           select=c(Year, Crude.Rate, ICD.Chapter))
})

new_df <- reactive({
    df <- sub_mdata()
    colnames(df) <- c("Year", "Rate_State", "ICD.Chapter")
    df <- merge(df, nat_cause, by=c("Year", "ICD.Chapter"))
    df = dplyr::mutate(df, state_diff = lag(Rate_State) - Rate_State)
    df = dplyr::mutate(df, nat_diff = lag(NatAvge) - NatAvge)
    df = dplyr::mutate(df, st_nat = (state_diff - nat_diff))
    return(df)
})

})

output$causev <- renderPlot({

    p <- ggplot(data=new_df(), aes(x="Year", y="st_nat")) +
        geom_line()
    print(p)
})
})

错误

Warning: Error in get: object 'FUN' of mode 'function' was not found
Stack trace (innermost first):
    46: get
    45: match.fun
    44: aggregate.data.frame
    43: aggregate.formula
    42: aggregate
     1: runApp
Error in get(as.character(FUN), mode = "function", envir = envir) : 
object 'FUN' of mode 'function' was not found

数据

                           ICD.Chapter        State Year Deaths Population Crude.Rate
1 Certain infectious and parasitic diseases    AL 1999   1092    4430141       24.6
2 Certain infectious and parasitic diseases    AL 2000   1188    4447100       26.7
3 Certain infectious and parasitic diseases    AL 2001   1211    4467634       27.1
4 Certain infectious and parasitic diseases    AL 2002   1215    4480089       27.1
5 Certain infectious and parasitic diseases    AL 2003   1350    4503491       30.0
6 Certain infectious and parasitic diseases    AL 2004   1251    4530729       27.6

1 个答案:

答案 0 :(得分:0)

以下代码是否适合您?我基本上只是拿了你给出的例子并删除了一些小错误。永远不会让Warning: Error in get: ...消息变得艰难。

注意:确保在干净的会话中运行此代码。您的错误原因可能是名称冲突。请参阅here

library(shiny)
library(ggplot2)
library(dplyr)

url <- paste0("https://raw.githubusercontent.com/indianspice/IS608/",
              "master/Assign3/cleaned-cdc-mortality-1999-2010-2.csv")
mdata <- read.csv(url, stringsAsFactors = FALSE)

crude_rate <- 500000

nat_cause <- aggregate(cbind(Deaths, Population) ~ ICD.Chapter + Year, mdata, FUN = sum)
nat_cause$NatAvge <- round(nat_cause$Deaths / nat_cause$Population * crude_rate, 4)

server <- function(input, output, session) {
  sub_mdata <- reactive({subset(
    mdata, ICD.Chapter == input$causes & State == input$state_name,
    select = c(Year, Crude.Rate, ICD.Chapter)
  )})

  new_df <- reactive({
    df <- sub_mdata()
    colnames(df) <- c("Year", "Rate_State", "ICD.Chapter")
    merge(df, nat_cause, by=c("Year", "ICD.Chapter")) %>%
      dplyr::mutate(state_diff = lag(Rate_State) - Rate_State) %>%
      dplyr::mutate(nat_diff = lag(NatAvge) - NatAvge) %>%
      dplyr::mutate(st_nat = (state_diff - nat_diff))
  })

  output$causev <- renderPlot({
    ggplot(data=new_df(), aes(x=Year, y=st_nat)) + geom_line()
  })
}

ui <- fluidPage(

  #Application title
  titlePanel("Mortality Improvement by State"),

  #Dropdown state and cause
  sidebarLayout(
    sidebarPanel(
      selectInput("state_name", "State:", choices = unique(mdata$State)),
      radioButtons("causes", "Select disease - cause of Death:",
                   unique(mdata$ICD.Chapter))
    ),
    # Show a plot of the generated distribution
    mainPanel(plotOutput("causev"))
  )
)

shinyApp(ui, server)
相关问题