2014-09-05 85 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

相關問題