我正在尝试使用 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])
})
}
))
Run Code Online (Sandbox Code Playgroud)
这是您正在尝试执行的操作的工作版本。看看我们的代码之间的差异,您的代码存在一些问题。另外,请注意,我没有使用它shinysky,因为它不再具有颜色选择器(它已移至另一个不活动的包),所以我使用的是inputColourfrom shinyjs。
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])
})
}
))
Run Code Online (Sandbox Code Playgroud)
免责声明:我是以下内容的作者shinyjs