3

I wrote a simple shiny app to illustrate a problem I am having fetching data from a website. Here is the UI code:

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"))
        )
    )
  )
 )
)

And here is the Server code:

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})
    })
})

The data being fetched is described at https://cityofphiladelphia.github.io/carto-api-explorer/#incidents_part1_part2.

When I run this shiny app in my local environment, it works perfectly. When publish it to shinyapps.io and run it, the application fails when I click the button to fetch the data. In the shiny log, it reports the following:

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

I am really stumped - is this a shiny server issue of some kind? If anyone out there has a clue, I am all ears!

zx8754
  • 52,746
  • 12
  • 114
  • 209

1 Answers1

3

I posted this question on multiple boards, and Joshua Spiewak on the Shinyapps.io Users Google group solved my problem:

You need to encode the URL in order for it to be valid, e.g. 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 runs on Linux, so it is likely that curl is being used to fetch this URL. My guess is that you are using Windows locally, which uses a less strict mechanism that is tolerant of the spaces and newlines you have in the URL.

After making his suggested change, it works as expected now. Thanks, Joshua!

Community
  • 1
  • 1