【发布时间】:2014-10-27 18:09:31
【问题描述】:
我正在尝试使用 Shiny 创建一个应用程序,我希望用户能够在其中选择绘图中每条线的颜色。总体思路是在应用程序中导入数据,然后绘制数据中的每个变量。我尝试使用来自 shinysky 包的颜色选择器“jscolorInput”,它在放置在 ui.r 文件中时工作正常,但由于我希望我的应用程序对于上传的每个数据集都是动态的,所以我需要将颜色选择器放在服务器中。 R,使用反应函数。 当放置在服务器中时,“jscolorInput”不起作用。
我想做的是:
- 复制颜色选择器的次数与 数据中的变量
- 从颜色中获取输入并传递它 作为情节中的颜色参数
我在闪亮的开发和 stackoverflow 方面都是新手,所以请原谅我的错误。
这是一个无法重现的可重现示例。
require(shinysky)
require(shiny)
dat <- data.frame(matrix(rnorm(120, 2, 3), ncol=3))
runApp(list(
ui = bootstrapPage(
# The reactive colorpicker
uiOutput('myPanel'),
# The plot
plotOutput('plot')
),
server = function(input, output) {
# Print as many colorpickers as the columns in the dataset
cols <- reactive({
n <- ncol(dat)
for(i in 1:n){
print(jscolorInput(paste("col", i, sep="_")))
}
})
output$myPanel <- renderPrint({cols()})
# Put all the input in a vector
colors <- reactive({
n <- ncol(dat)
lapply(1:n, function(i) {
input[[paste("col", i, sep="_")]]
})
})
output$plot <- renderPlot({
cols <- ifelse(is.null(input$col_1), rep("000000 ", n), colors())
plot(dat[,1], col= paste0("#", cols[1], ""))
for(i in 2:ncol(dat))lines(dat[,i], col=cols[i])
})
}
))
【问题讨论】: