2014-04-28 1 views
0

라이브러리 (RJSONIO)를 사용하여 BLS 데이터를 가져 오려고합니다. 가져온 데이터는 데이터 프레임에 있으며 ggplot을 사용하여 그릴 수 있습니다.BLS API를 사용하는 Shiny Apps

코드를 반짝이는 앱으로 변환 할 수 없습니다.

안내해주세요.

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, label 및 choices의 세 가지 필수 요소가 있습니다. 거기에 어떤 선택도하지 않았습니다. selectInput ("dataset", "Commodity :", ...) 여야합니다. 여기서 ...는 선택 목록입니다. 나는 당신이 여기에서 사용하고있는 데이터에 익숙하지 않아서 정확한 선택 목록을 훑어 볼 시간이 없었다. 선택 목록이 고정되어 있다면, 내가 한 형식, c (a, b, c, ...)을 사용하십시오 - 데이터에서 가져온 것이면 global.R (가질 수있는 다른 파일)에로드 된 것을 참조 할 수 있습니다. bls.df $ Commodities와 비슷합니다.
  • 사이드 바 패널 뒤에 너무 많은 괄호가있었습니다. 메인 패널에 오기 전에 전체 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  
        }) 
    }) 
    

    주요 문제는 여기에 주변에 있었다 너. 당신은 당신의 plotOutput "CommodityPlot"이라는 이름을 붙 였지만 그래픽을 변위 시키려고했다. 출력은 입력에 바인딩되어야합니다.

  • reactPlot이 사용되지 않음 - renderPlot으로 전환했습니다.
  • renderPlot 내에서 ggplot의 각 행 다음에 쉼표가있었습니다. 각 줄은 이미 완전한 문장 이었으므로 쉼표가 코드를 위반했습니다. 나는 그들을 제거했다. R에서 코드가 {} 안에있을 때마다 줄을 끝낼 필요가 없다는 것을 기억하십시오. 라인은 a로 끝나지 않습니다.

이제 코드를 디버깅했습니다. 그리고 그것은 나를 위해 작동합니다 (저는 0.11.1 버전의 RStudio 0.98.978을 실행하고 있습니다). 귀하의 앱은 여전히 ​​매우 불완전합니다. 귀하의 verbatimTextOutput "CommodityTable"또는 textOutput "commSelected"에 대한 출력 결과가 표시되고 데이터에 상품이없는 것 같아서 귀하가 계획하고있는 내용을 모릅니다. 선택.반짝이는 설명서가 상당히 좋습니다. the Shiny gallery을 방문하면 반짝이는 앱의 작동 예제와이를 구동하는 코드를 확인하는 것이 좋습니다.

관련 문제