2014-04-28 52 views
0

我想使用庫(RJSONIO)導入BLS數據。 導入的數據位於數據框中,我可以使用ggplot繪圖。關於使用BLS API的閃亮應用程序

我無法將代碼轉換爲閃亮的應用程序。

請指導我。

Server.R

library(shiny)  
library(RCurl) 
library(RJSONIO) 
library(ggplot2) 

bls.content <- getURLContent("http://api.bls.gov/publicAPI/v1/timeseries/data/CES6056132001") 




bls.json <- fromJSON(bls.content, simplify=TRUE) 
tmp <-bls.json$Results[[1]][[1]] 
bls.df <- data.frame(year=sapply(tmp$data,"[[","year"), 
        period=sapply(tmp$data,"[[","period"), 
        periodName=sapply(tmp$data,"[[","periodName"), 
        value=as.numeric(sapply(tmp$data,"[[","value")), 
        stringsAsFactors=FALSE) 





bls.df[bls.df$periodName!="Annual", ] 

shinyServer(function(input, output) { 

    output$displot <- reactiveplot({ 
     ggplot(data=bls.df, aes(x=year, y=value, group=period)), 
     gg <- ggplot(data=bls.df, aes(x=year, y=value, group=period)), 
     gg <- gg + geom_bar(stat="identity", position="dodge", aes(fill=period)), 
     gg 


    }) 

}) 

** ** UI.R

library(shiny) 

shinyUI(pageWithSidebar(

    headerPanel("BLS data"), 
    sidebarPanel(
    selectInput("dataset", "Commodity:", 
       ))), 


mainPanel(
    h3(textOutput("commSelected")), 

    verbatimTextOutput("CommodityTable"), 

    plotOutput("CommodityPlot") 
)) 
+0

你在什麼發光的版本?在過去幾個版本的Shiny中創建一個繪圖的函數是'renderPlot'。你可能只需要更新Shiny。 –

+0

我使用Shiny Server最新版本0.98.693。 – user2733412

回答

2

有幾件事情你的代碼錯誤它的編寫方式,讓我再試試解釋最嚴重的錯誤在哪裏。

ui.R

shinyUI(pageWithSidebar(

    headerPanel("BLS data"), 
    sidebarPanel(
    selectInput("dataset", "Commodity:",c(1,2) 
    )), 


    mainPanel(
    h3(textOutput("commSelected")), 

    verbatimTextOutput("CommodityTable"), 

    plotOutput("CommodityPlot") 
)) 
) 

關鍵問題是圍繞:

  • pageWithSidebar已經過時 - 儘管它的工作原理。
  • selectInput有三個必需的元素,inputId,標籤和選項。您沒有提供任何選擇。它應該是selectInput(「dataset」,「Commodity:」,...),其中...是選項列表。我不熟悉你在這裏使用的數據,所以我沒有花時間在正確的選擇列表中填寫。如果選項列表是固定的,那麼使用我所做的格式,c(a,b,c,...) - 如果是來自您的數據,您可以引用加載到global.R(可能有的另一個文件)類似於bls.df $商品。
  • 你在sidebarPanel後面有太多括號 - 在你進入mainPanel之前關閉了整個shinyUI。我把違規的括號移到了最後。這裏

server.R

library(shiny)  
library(RCurl) 
library(RJSONIO) 
library(ggplot2) 

bls.content <- getURLContent("http://api.bls.gov/publicAPI/v1/timeseries/data/CES6056132001") 

bls.json <- fromJSON(bls.content, simplify=TRUE) 
tmp <-bls.json$Results[[1]][[1]] 
bls.df <- data.frame(year=sapply(tmp$data,"[[","year"), 
       period=sapply(tmp$data,"[[","period"), 
       periodName=sapply(tmp$data,"[[","periodName"), 
       value=as.numeric(sapply(tmp$data,"[[","value")), 
       stringsAsFactors=FALSE) 

shinyServer(function(input, output) { 

    output$CommodityPlot <- renderPlot({ 
    ggplot(data=bls.df, aes(x=year, y=value, group=period)) 
    gg <- ggplot(data=bls.df, aes(x=year, y=value, group=period)) 
    gg <- gg + geom_bar(stat="identity", position="dodge", aes(fill=period)) 
    gg  
    }) 
}) 

關鍵問題是圍繞:

  • 首先 - 你沒有你的圖形輸出綁定到你指定的一個輸入端你的ui.R.您將plotOutput命名爲「CommodityPlot」,但您將圖形限定爲displot。請記住,您的輸出必須綁定到您的輸入。
  • reactivePlot已棄用 - 我將它切換到了渲染圖
  • 在renderPlot中,在ggplot的每一行後面都有逗號。你的每一行都已經是完整的語句,所以逗號打破了代碼。我刪除了它們。請記住,在R中,只要代碼在{}內,就不需要使用a終止行;並且行不會以a,...結尾。

現在,我調試了你的代碼,它適用於我(我使用閃亮的版本0.10.1運行RStudio 0.98.978)。您的應用程序仍然非常不完整 - 您的verbatimTextOutput「CommodityTable」或textOutput「commSelected」綁定了輸出,而且您的數據甚至似乎沒有商品,因此我不知道您計劃的是什麼選擇。閃亮的文檔相當不錯 - 我強烈建議訪問the Shiny gallery查看閃亮應用的工作示例以及驅動它們的代碼。

相關問題