简体   繁体   English

R:带库(DT)的库(闪亮):创建<selectinput>对于 Plot 上的不同 colors</selectinput>

[英]R: Library(Shiny) with Library (DT): Creation of <selectInput> for different colors on Plot

I am so sorry to trouble you.很抱歉给你添麻烦了。 I am new to R programming and programming in general.我是 R 编程和一般编程的新手。 95% of the time, when search and read online examples, I fail to understand them. 95% 的时间,当搜索和阅读在线示例时,我无法理解它们。

I have the codes for a shiny app.我有 shiny 应用程序的代码。 I did not write these codes.这些代码不是我写的。 These codes were given as default.这些代码是默认给出的。 When you run it, the bottom table comes up with a plot of "dots" that has no color.当您运行它时,底部表格会出现一个没有颜色的“点” plot。 When you click on a specific row, the "dot" it represents turns to red.当您单击特定行时,它所代表的“点”会变为红色。

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)

The question is, how do I create selectInput for different color names, so that the user can decide which color to use and not just use the default RED color?问题是,如何为不同的颜色名称创建 selectInput,以便用户可以决定使用哪种颜色,而不仅仅是使用默认的 RED 颜色?

From my own research, I think I need to add 2 lines of code within UI and edit 1 line of code within server.根据我自己的研究,我认为我需要在 UI 中添加 2 行代码并在服务器中编辑 1 行代码。 Unfortunately these methods does not work though.不幸的是,这些方法虽然不起作用。 Within UI用户界面内

selectInput("color", "What is your preferred color?", choices = c("blue", "gold", "green", "pink", "red", "yellow"))
textOutput("color")

Within server changing the "red" in this code在服务器中更改此代码中的“红色”

points(iris[s, c("Sepal.Length", "Sepal.Width"), drop = F],
                       pch = 19, cex = 1, col = "red")

to "input$color"到“输入$颜色”

points(iris[s, c("Sepal.Length", "Sepal.Width"), drop = F],
                           pch = 19, cex = 1, col = "input$color")

I got the error message below when I select the colors I want and attempt to color my plots (by selecting the rows in the table dt2)当我想给我的图上色时(通过选择表 dt2 中的行)

invalid color name 'input$color'

So sorry to trouble all of you.很抱歉给大家添麻烦了。 I understand that most of you are busy with your own lives and I really appreciate any help or advice given.我知道你们中的大多数人都忙于自己的生活,我非常感谢所提供的任何帮助或建议。

The issue was that "input$color" should be input$color (no quotes).问题是"input$color"应该是input$color (没有引号)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM