R script working locally not working on shinyapp.io

旧街凉风 提交于 2019-12-08 17:30:24

问题


I'm trying to put together a R + Shiny app that, at least initially, plots a histogram of date data. I have it working just fine on my local system in RStudio, but in shinyapps.io it doesn't work.

The app, at the moment, is very simple - the user interface doesn't really do anything, and the data is just a small sample of test data.

Works fine in RStudio (draws a nice little histogram). When loaded on shinyapps.io, the 'title panel' text and sidebar display but after a second or two either it - reports the error "'from' cannot be NA, NaN or infinite" or - the screen greys out and the script stops (?) in both cases without producing a histogram.

Has me baffled; would be interested to hear if anyone has any suggestions for where I've gone wrong. Perhaps it's to do with as.Date - a possibly similar problem is reported here, without any solution.

My two shiny files are:

# ui.R

shinyUI(fluidPage(
  titlePanel("title panel"),

  sidebarLayout(
    sidebarPanel(

      ),

    mainPanel(

      plotOutput("distPlot")

    )
  )
))

and

# server.R
library(shiny)

shinyServer(function(input, output){

  output$distPlot <- renderPlot({

      text_date <- c("9 March 2006", "31 October 2008", "24 September 2008", "27 February 2009", "19 May 2014", "11 December 2009", "7 August 2013", "8 December 2014", "2 February 2010", "22 December 2014", "20 December 2011", "4 September 2009", "19 December 2013", "10 October 2007", "19 September 2008")

      num_date <- as.Date(text_date, format = "%d %B %Y")

     #plot a histogram

      hist(num_date, 
         breaks = "years",
         format = "%Y",
         freq = TRUE)

  })

})

There are no errors (or anything other than versions, starting and listening on...) reported by showLogs():

2015-02-22T10:00:50.507273+00:00 shinyapps[32851]: R version: 3.1.2
2015-02-22T10:00:50.509043+00:00 shinyapps[32851]: rmarkdown version: 0.0.0
2015-02-22T10:00:50.507340+00:00 shinyapps[32851]: Shiny version: 0.11.1
2015-02-22T10:00:50.509508+00:00 shinyapps[32851]: knitr version: 0.0.0
2015-02-22T10:00:50.784283+00:00 shinyapps[32851]:
2015-02-22T10:00:50.784285+00:00 shinyapps[32851]: Starting Shiny with process ID: '14'
2015-02-22T10:00:50.792403+00:00 shinyapps[32851]:
2015-02-22T10:00:50.792405+00:00 shinyapps[32851]: Listening on http://0.0.0.0:57429


回答1:


You have two problems. First, you missed some quotes in your data. However, fixing that did not change the result. I copied your code, fixed the quotes, and deployed it and got the same results (worked locally but not on shinyapps.io).

The second (and more important problem) is with the way different operating systems handle dates. I changed the data portion of server.R to be the following:

text_date <- c("2006-03-09", "2008-10-31", "2008-09-24", "2009-02-27", "2014-05-19", "2013-08-07", "2014-12-08", "2010-02-02", "2014-12-22", "2011-12-20", "2009-09-04", "2013-12-19", "2007-10-10", "2008-09-19")

That worked both locally and on shinyapps.io. So the problem is not with your program, but how the dates are being processed.

I thought the problem might be related to leading zeroes being required for single digit dates, but altering that did not fix the problem. However, when I replaced the month names with two-digit numbers, it again worked both locally and on shinyapps.io. Therefore, it seems that shinyapps.io has some difficulty with converting month names into date values. However, I don't know why that would happen.

Update: Following a lead provided by Fereshteh Karimeddini, I modified the files by embedding the following code: In server.R:

output$dates = renderText({format(num_date, format = "%d %B %Y")})
output$location = renderText({Sys.getlocale(category = "LC_ALL")})

In ui.R:

textOutput("dates")
textOutput("location")

Interestingly, I got exactly the same results for the "dates" output whether I was running locally or on shinyapps.io. I had thought that I would get month names in French or something. However, for the "location" output, I got different results. Locally:

LC_COLLATE=English_United States.1252;
LC_CTYPE=English_United States.1252;
LC_MONETARY=English_United States.1252;
LC_NUMERIC=C;
LC_TIME=English_United States.1252

On shinyapps.io:

LC_CTYPE=C.UTF-8;
LC_NUMERIC=C;
LC_TIME=C.UTF-8;
LC_COLLATE=C.UTF-8;
LC_MONETARY=C.UTF-8;
LC_MESSAGES=C.UTF-8;
LC_PAPER=C.UTF-8;
LC_NAME=C;
LC_ADDRESS=C;
LC_TELEPHONE=C;
LC_MEASUREMENT=C.UTF-8;
LC_IDENTIFICATION=C

Note: carriage returns added for readability.

There was a bug report on a debian forum (I don't know the relationship to Ubuntu, which is what shinyapps.io uses) that noted that C.UTF-8 did not contain month names. However, that was from 2012, and the bug report said that it was fixed in a recent release. Also, if there really were not month names in the C.UTF-8 on shinyapps.io, then it shouldn't have been able to output month names (which it did perfectly). This got me wondering - if it can output month names, why can't it read month names? So I tried to get it to do just that. In server.R:

text_date = c("09 03 2006")
num_date <- as.Date(text_date, format = "%d %m %Y")
x = format(num_date, format = "%d %B %Y")
output$dates = renderText({x})
renum_date = as.Date(x, format = "%d %B %Y")
output$redates = renderText({format(renum_date, format = "%d %B %Y")})

In ui.R:

sidebarPanel(textOutput("dates")),
mainPanel(textOutput("redates"))

Locally, the sidebarPanel and mainPanel showed exactly the same thing: 09 March 2006. However, on shinyapps.io, the mainPanel showed NA. So, it would seem that that shinyapps.io can dish it out but can't take it, at least as far as month names go. How weird is that?




回答2:


I had the same problem. Seems that shinyapps.io uses different locale settings. I was trying to use %b in the as.Date() function and it was failing to convert the date properly, resulting in a null record. This totally messed me up, because it did not throw any kind of error, either. Debugging to find that 1 line was not fun!

I created the following inline code to resolve my month name because it was formatted that way in my datasource, so I had to convert it:

match((Month),c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"))

Month in the code above is the variable name holding the value of the month name. An example of a full line using this code:

df$Date <- with(df, paste(Year, match((Month),c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")), Day, sep="-"))

Enjoy!



来源:https://stackoverflow.com/questions/28656683/r-script-working-locally-not-working-on-shinyapp-io

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!