2017-10-06 69 views
0

这如果我的第一个闪亮的应用程序,我一直在试图调试几天 - 我试图创建一个闪亮的应用程序,显示国家的平均死亡率对全国平均水平使用线图。每当我运行应用程序时,用户界面闪烁,页面变为空白。我确实收到2条错误消息。任何建议将不胜感激闪亮的应用程序用户界面闪烁然后消失没有ggplot图

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 
+0

我很确定'ggplot'的参数应该没有引号。你尝试过'ggplot(data = new_df(),aes(x = Year,y = st_nat))'吗? –

+0

另一个可能的问题是你可能有一个与'stats :: df'勾结的名字。见[这里](https://stackoverflow.com/questions/21367922/error-in-getas-characterfun-mode-function-envir-envir) –

+0

@GregordeCillia我删除了引号,但我得到相同的错误消息 –

回答

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)