【问题标题】:Shiny app that fetches data via URL works locally but not on shinyapps.io通过 URL 获取数据的闪亮应用程序在本地工作,但不在 shinyapps.io 上
【发布时间】:2018-03-23 08:47:12
【问题描述】:

我编写了一个简单的闪亮应用程序来说明我从网站获取数据时遇到的问题。这是用户界面代码:

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

我真的很难过 - 这是某种闪亮的服务器问题吗?如果有人有线索,我全神贯注!

【问题讨论】:

    标签: r shiny cartodb


    【解决方案1】:

    我在多个板上发布了这个问题,Shinyapps.io 用户 Google 组的 Joshua Spiewak 解决了我的问题:

    您需要对 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 中的空格和换行符。

    在做出他建议的更改后,它现在按预期工作。谢谢,约书亚!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-29
      • 2020-03-12
      • 2020-07-02
      • 2019-04-24
      • 2016-06-15
      • 2020-09-30
      • 2018-06-11
      相关资源
      最近更新 更多