【发布时间】:2020-01-28 19:36:19
【问题描述】:
很抱歉给您添麻烦了。我是 R 编程和一般编程的新手。 95% 的时间,在搜索和阅读在线示例时,我无法理解它们。
我有一个闪亮的应用程序的代码。这些代码不是我写的。这些代码是默认给出的。当你运行它时,底部的表格会出现一个没有颜色的“点”图。当您单击特定行时,它所代表的“点”会变为红色。
library(shiny)
library(DT)
ui <- fluidPage(
h3("t1"),
tableOutput("t1"),
hr(),
fluidRow(
column(9, h3("dt1"),
dataTableOutput("dt1")),
column(3, h3("x4"),
verbatimTextOutput("x4"))),
hr(),
fluidRow(
column(8, h3("dt2"),
dataTableOutput("dt2")),
column(4, h3("p5"),
plotOutput("p5")))
)
options(error = function() traceback(2))
server <- function(input, output, session) {
output$t1 <- renderTable(iris[1:10,], striped = TRUE, hover = TRUE)
output$dt1 <- renderDataTable(iris, options = list( pageLength = 5))
output$x4 <- renderPrint({
s = input$dt1_rows_selected
if (length(s)) {
cat('These rows were selected:\n\n')
cat(s, sep = ', ')
}
})
output$dt2 <- renderDataTable(iris,
options = list(pageLength = 5),
server = FALSE)
output$p5 <- renderPlot({
s <- input$dt2_rows_selected
plot(iris$Sepal.Length, iris$Sepal.Width)
if (length(s)) {
points(iris[s, c("Sepal.Length", "Sepal.Width"), drop = F],
pch = 19, cex = 1, col = "red")
}
})
}
shinyApp(ui, server)
问题是,我如何为不同的颜色名称创建 selectInput,以便用户可以决定使用哪种颜色,而不仅仅是使用默认的 RED 颜色?
根据我自己的研究,我认为我需要在 UI 中添加 2 行代码并在服务器中编辑 1 行代码。不幸的是,这些方法虽然不起作用。 用户界面内
selectInput("color", "What is your preferred color?", choices = c("blue", "gold", "green", "pink", "red", "yellow"))
textOutput("color")
在服务器内更改此代码中的“红色”
points(iris[s, c("Sepal.Length", "Sepal.Width"), drop = F],
pch = 19, cex = 1, col = "red")
到“输入$颜色”
points(iris[s, c("Sepal.Length", "Sepal.Width"), drop = F],
pch = 19, cex = 1, col = "input$color")
当我选择我想要的颜色并尝试为我的图着色(通过选择表 dt2 中的行)时,我收到以下错误消息
invalid color name 'input$color'
很抱歉给大家添麻烦了。我知道你们中的大多数人都忙于自己的生活,我非常感谢所提供的任何帮助或建议。
【问题讨论】: