在本地工作的R脚本无法在Shinyapp.io上运行 [英] R script working locally not working on shinyapp.io

查看:122
本文介绍了在本地工作的R脚本无法在Shinyapp.io上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将至少在最初就绘制日期数据直方图的R + Shiny应用程序组合在一起.我在RStudio的本地系统上运行得很好,但是在shinyapps.io中却无法正常工作.

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.

在RStudio中效果很好(绘制了一个不错的直方图).加载到shinyapps.io上时,会显示标题面板"文本和侧边栏,但是会在一两秒钟后显示 -报告错误'from'不能为NA,NaN或无限"或 -屏幕变灰并且脚本停止(?) 在两种情况下都不会产生直方图.

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.

让我感到困惑;很想知道是否有人对我出问题的地方有任何建议.可能与as.Date有关-在此处报告了一个可能类似的问题,没有任何解决办法.

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.

我的两个闪亮文件是:

# ui.R

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

  sidebarLayout(
    sidebarPanel(

      ),

    mainPanel(

      plotOutput("distPlot")

    )
  )
))

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

  })

})

showLogs()没有报告任何错误(或版本,启动和侦听中的任何错误):

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版本:3.1.2
2015-02-22T10:00:50.509043 + 00:00 Shinyapps [32851]:rmarkdown版本:0.0.0
2015-02-22T10:00:50.507340 + 00:00 Shinyapps [32851]:光泽版本:0.11.1
2015-02-22T10:00:50.509508 + 00:00 Shinyapps [32851]:编织程序版本:0.0.0
2015-02-22T10:00:50.784283 + 00:00 Shinyapps [32851]:
2015-02-22T10:00:50.784285 + 00:00 Shinyapps [32851]:使用进程ID:"14"启动Shiny
2015-02-22T10:00:50.792403 + 00:00 Shinyapps [32851]:
2015-02-22T10:00:50.792405 + 00:00 Shinyapps [32851]:在 http://0.0.0.0上监听: 57429

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

推荐答案

您有两个问题.首先,您错过了数据中的一些引号.但是,修复该问题并不会改变结果.我复制了您的代码,固定了引号,并将其部署并得到了相同的结果(在本地工作,但在shinyapps.io上却没有).

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

第二个(也是更重要的问题)是不同操作系统处理日期的方式.我将server.R的数据部分更改为以下内容:

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

这在本地和shinyapps.io上都有效.因此,问题不在于您的程序,而在于日期的处理方式.

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

我认为该问题可能与一位数字日期所需的前导零有关,但进行更改并不能解决问题.但是,当我用两位数字替换月份名称时,它再次在本地和shinyapps.io上都可以使用.因此,shinyapps.io似乎很难将月份名称转换为日期值.但是,我不知道为什么会发生这种情况.

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.

更新: 跟随由Fereshteh Karimeddini提供的线索,我通过嵌入以下代码修改了文件: 在server.R中:

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

在ui.R中:

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

有趣的是,无论是本地运行还是在Shinyapps.io上运行,"dates"输出的结果都完全相同.我曾以为我会用法语或类似的名称来获得月份名称.但是,对于位置"输出,我得到了不同的结果. 本地:

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 =英语_美国.1252;
LC_CTYPE = English_United States.1252;
LC_MONETARY = English_United States.1252;
LC_NUMERIC = C;
LC_TIME =英语_美国.1252

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

在Shinyapps.io上:

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

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

注意:为便于阅读,添加了回车符.

在debian论坛上有错误报告(我不知道与Ubuntu的关系(shinyapps.io使用的关系)指出C.UTF-8不包含月份名称.但是,那是从2012年开始的,并且该错误报告指出,该错误已在最近的发行版中修复.另外,如果Shinyapps.io上的C.UTF-8中确实没有月份名称,则它应该不能输出月份名称(这确实做到了).这让我想知道-如果它可以输出月份名称,为什么它不能读取月份名称?所以我试图让它做到这一点. 在server.R中:

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

在ui.R中:

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

在本地,sidebarPanel和mainPanel显示的是完全相同的内容:2006年3月9日.但是,在shinyapps.io上,mainPanel显示的是NA.因此,shinyapps.io似乎可以提出但不能接受,至少就月份名称而言.这有多奇怪?

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?

这篇关于在本地工作的R脚本无法在Shinyapp.io上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆