2017-10-11 39 views
1

我寫了一個簡單的閃亮應用程序來說明我從網站獲取數據的問題。下面是UI代碼:通過URL獲取數據的閃亮應用程序在本地工作,但不在shinyapps.io

library(shiny) 

shinyUI(fluidPage(

# Application title 
titlePanel("Test App"), 

# Sidebar with slider inputs 
sidebarLayout(
    sidebarPanel(
     actionButton("button", "Fetch Data") 
       ), 


    mainPanel(
     tabsetPanel(
      tabPanel("Data", textOutput("crimelist")) 
     ) 
    ) 
) 
) 
) 

這裏是服務器代碼:

library(shiny) 

# Define server logic 
shinyServer(function(input, output, session) { 

    # Get a list of the different types of crime 
    observeEvent(input$button, { 
     url <- "https://phl.carto.com/api/v2/sql?format=csv&q=SELECT 
       distinct rtrim(text_general_code) as text_general_code 
          FROM incidents_part1_part2 
          order by text_general_code" 
    crimelist <- read.csv(url(url), stringsAsFactors = FALSE)$text_general_code 
    output$crimelist <- renderText({crimelist}) 
    }) 
}) 

獲取的數據在https://cityofphiladelphia.github.io/carto-api-explorer/#incidents_part1_part2描述。

當我在我的本地環境中運行這個閃亮的應用程序,它完美的作品。當將它發佈到shinyapps.io並運行它時,當我單擊按鈕來獲取數據時,應用程序將失敗。在閃亮的日誌中,它報告以下內容:

2017-10-11T14:46:31.242058+00:00 shinyapps[224106]: Warning in 
open.connection(file, "rt") : 
2017-10-11T14:46:31.242061+00:00 shinyapps[224106]: URL 'https://phl.carto.com/api/v2/sql?format=csv&q=SELECT distinct 
rtrim(text_general_code) as text_general_code 
2017-10-11T14:46:31.242062+00:00 shinyapps[224106]: FROM incidents_part1_part2 
2017-10-11T14:46:31.242063+00:00 shinyapps[224106]: order by text_general_code': status was 'URL using bad/illegal format or missing URL' 
2017-10-11T14:46:31.243062+00:00 shinyapps[224106]: Warning: Error in open.connection: cannot open connection 

我真的難住 - 這是一種閃亮的服務器問題嗎?如果有人有線索,我全都是耳朵!

回答

1

我張貼在多個板這個問題,約書亞Spiewak在Shinyapps.io谷歌的用戶羣解決我的問題:

您需要的URL編碼,以便它是有效的,例如https://phl.carto.com/api/v2/sql?format=csv&q=SELECT%20distinct%20rtrim(text_general_code)%20as%20text_general_code%20FROM%20incidents_part1_part2%20order%20by%20text_general_code

shinyapps.io在Linux上運行,所以很可能使用curl來獲取此URL。我的猜測是你在本地使用Windows,它使用一個不太嚴格的機制,它可以容忍URL中的空格和換行符。

在提出他的建議更改後,它現在按預期工作。謝謝,約書亞!

相關問題