2014-09-05 86 views
2

我是R Shiny的新手,正在开发我的第一个应用程序。我已经看到了我在本地如何使用它,但是当我将其部署/发布到shinyapps.io时,颜色会发生变化。我还没有在互联网上发现任何讨论这件事。我创建了一个简化版本的应用程序,通过部署重新创建颜色更改。本地和shinyapps.io版本的R之间的颜色不同Shiny应用程序

在当地,颜色看看(热色)我想要的方式: enter image description here

在shinyapps.io,色彩看起来是这样的: enter image description here

我使用RStudio版本0.98。 1049与64位R版本3.1.0在Windows 7上。

我使用的三个文件是Server.r,ui.rhelper.r,每个文件如下所示。 (我无法弄清楚如何在shinyapps.io上以showcase模式显示应用程序)我的“真实”应用程序比这更复杂,但我想保留一些复杂的情况,改变问题。这就是为什么在这个例子中,有三种不同的功能被用来创建一个非常简单的情节。

Server.r

source("helper.r") 

mydf <- data.frame(x=rnorm(100), y=rnorm(100), z1=rnorm(100), z2=rnorm(100)) 

shinyServer(function(input, output) { 

    # get metric index number 
    mIndex <- reactive({ 
    match(input$selmetric, metrix) 
    }) 

    # plot the selected metric 
    output$plot <- renderPlot({ 
    bigdf <- mydf 
    i <- mIndex() 
    drawplot(dat=bigdf, metric=input$selmetric) 
    }) 

}) 

ui.r

shinyUI(fluidPage(
    titlePanel("Color Test"), 

    fluidRow(
    column(4, 
     # select box 
     selectInput(
     "selmetric", 
     label = h4("Select metric to plot"), 
     choices = list(
      "First" = "z1", 
      "Second" = "z2" 
     ), 
     selected = "z1" 
     ) 
    ) 
), 

    fluidRow(
    column(8, 
     plotOutput("plot") 
    ) 
) 
)) 

helper.r

# metric names 
metrix <- c("y1", "y2") 

# assign a specified number of heat colors to a collection of values 
colorn <- function(x, n) { 
    group <- cut(x, breaks=n, labels=FALSE) 
    colr <- heat.colors(n)[group] 
    data.frame(group=group, colr=colr) 
    } 

# define symbol color and size categories for numeric data 
def.sym <- function(x, ngrps=10) { 
    gc <- colorn(x, ngrps) 
    data.frame(colz=gc$colr, sizes=5*(gc$group/ngrps)) 
    } 

# draw the plot 
drawplot <- function(dat, metric) { 
    z <- dat[, metric] 
    ds <- def.sym(z) 
    ord <- order(-ds$sizes) 
    par(mar=c(4, 4, 1, 1), family="mono", xpd=NA, cex=1.5) 
    plot(dat$x[ord], dat$y[ord], bg=ds$colz[ord], pch=21, cex=ds$sizes[ord]) 
    } 
+1

灿”你还可以添加一个rednerTable()或者什么来查看'colorn'或者'def.sym'的colz'的颜色列中设置了什么值?你确定这些值设置正确吗?如果问题出现在您正在绘制的颜色值或渲染这些颜色的问题上,我不确定。 – MrFlick 2014-09-05 02:50:49

回答

3

我添加了一行print(str(ds))你的情节语句之前,这里是输出:

> runApp() 
Listening on http://127.0.0.1:3643 
'data.frame': 100 obs. of 2 variables: 
$ colz : Factor w/ 9 levels "#FF0000FF","#FF2400FF",..: 4 6 6 4 3 5 4 8 3 5 ... 
$ sizes: num 2 3 3 2 1.5 2.5 2 4 1.5 2.5 ... 
NULL 

显然,你传递给bg值实际上是色彩层次1,2,3..etc。而不是其值“#FFB600FF”,“#FFB600F”...,这也解释了为什么大多数颜色看起来“熟悉”。

这里是你的代码看起来在我的帐户上shinyappio

enter image description here

最后,我想这大概是你的代码什么的factor issue,而不是主要是因为做的运行环境, ,你可能正在研究一些恰好具有正确类型的本地数据框。

(注:我有一个很难与helper.R部署为一个单独的文件,我复制从helper.R内容以server.R并最终制作shinyappsio。)

+1

谢谢。将一行代码添加到'Server.r'文件的顶部,这个技巧就是:'options(stringsAsFactors = FALSE)'。我在我的本地版本的R中设置了这个选项。 – 2014-09-05 11:15:39

相关问题