我试图创建一个应用程序使用闪亮,在那里,我希望用户能够选择每一行的颜色在一个情节。总的思想是导入应用程序中的数据,然后绘制数据中的每个变量。我尝试使用shinysky包中的色选择器'jscolorInput‘,当将它放在ui.r文件中时,它工作得很好,但是由于我希望我的应用程序对上传的每个数据集都是动态的,所以我需要使用一个反应性函数将色素选择器放到server.R中。当放置在服务器中时,“jscolorInput”无法工作。
我想做的是:
我是一个非常新的发展和堆叠溢出,所以请原谅我的错误。
这里有一个可重复的例子,它不起作用。
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])
})
}
))发布于 2015-06-22 23:40:47
这是你想要做的事情的一个工作版本。看看我们的代码之间的差异,您的代码有一些问题。另外,请注意,我没有使用shinysky,因为它已经没有颜色选择器了(它移到了另一个不活动的包中),所以我使用的是来自shinyjs的inputColour。
library(shiny)
library(shinyjs)
dat <- data.frame(matrix(rnorm(120, 2, 3), ncol=3))
runApp(shinyApp(
ui = fluidPage(
uiOutput('myPanel'),
plotOutput("plot")
),
server = function(input, output, session) {
cols <- reactive({
lapply(seq_along(dat), function(i) {
colourInput(paste("col", i, sep="_"), "Choose colour:", "black")
})
})
output$myPanel <- renderUI({cols()})
# Put all the input in a vector
colors <- reactive({
lapply(seq_along(dat), function(i) {
input[[paste("col", i, sep="_")]]
})
})
output$plot <- renderPlot({
if (is.null(input$col_1)) {
cols <- rep("#000000", ncol(dat))
} else {
cols <- unlist(colors())
}
plot(dat[,1], col = cols[1])
for(i in 2:ncol(dat)) lines(dat[,i], col = cols[i])
})
}
))免责声明:我是shinyjs的作者
https://stackoverflow.com/questions/26594027
复制相似问题