r - R Shiny 应用程序的本地版本和shinyapps.io 版本之间的颜色不同

标签 r colors shiny

我是 R Shiny 的新手,正在开发我的第一个应用程序。我在本地看起来就像我想要的那样,但是当我将其部署/发布到shinyapps.io 时,颜色会发生变化。我在互联网上没有找到任何讨论此问题的内容。我创建了应用程序的简化版本,可以通过部署重新创建颜色更改。

在本地,颜色看起来像我想要的那样(加热颜色): enter image description here

在shinyapps.io上,颜色如下所示: enter image description here

我在 Windows 7 上使用 RStudio 版本 0.98.1049 和 64 位 R 版本 3.1.0。

我使用的三个文件是 Server.rui.rhelper.r,每个文件如下所示。 (我不知道如何在shinyapps.io 上以展示模式显示应用程序。)我的“真实”应用程序比这更复杂,但我想保留一些复杂性,以防万一这导致了颜色变化问题。这就是为什么在这个示例中,使用三个不同的函数来创建一个非常简单的绘图。

服务器.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])
    }

最佳答案

plot(dat$x[ord], dat$y[ord], bg=as.character(ds$colz[ord]), pch=21, cex=ds$sizes[ord])

我在绘图语句之前添加了一行 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...等。而不是它的值“#FFB600FF”、“#FFB600F”...,这也解释了为什么大多数颜色看起来“熟悉”。

以下是您的代码在我的帐户 shinyappio 上的显示效果

enter image description here

最后,我认为这可能与运行环境无关,而主要是因为代码中的因素问题,您可能正在处理一些本地数据帧,而该数据框恰好是有正确的类型。

(注意:我很难将 helper.R 作为单独的文件进行部署,我将内容从 helper.R 复制到了 server.R,最后它在shinyappsio 上运行。)

关于r - R Shiny 应用程序的本地版本和shinyapps.io 版本之间的颜色不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25677463/

相关文章:

r - 从终端获取有效,但 httr::GET 无效

java - 用额外的颜色按顺序给形状着色

swift - 如何在 Swift 中比较 UIColors

r - Shiny 的 selectInput 不会对取消选择所有元素使用react

r - 使用 tidytext 将字数大小作为图层添加到共现网络图表上的节点大小

r - R 中的变量名称限制

css - Shinydashboard:使 sidebarPanel 覆盖在 mainPanel 上

r - 包含 Markdown 时 Shiny 的主面板宽度

r - 过滤包含特定字符串的行

C# - 使用 RGB 值在颜色对话框中设置自定义颜色