简体   繁体   English

SelectInput 更改 Shiny 中的多个元素

[英]SelectInput to change multiple elements in Shiny

Is there a way to have a selectInput change two elements of a plot?有没有办法让 selectInput 更改 plot 的两个元素? For example below, I created a reactive block to manipulate the data I want to plot in geom_point for each selectInput choice and it works perfectly.例如下面,我为每个 selectInput 选项创建了一个反应块来操作我想要在 geom_point 中 plot 的数据,它工作得很好。 However, I also want the color of the points to change, a different color for each choice that is automatic, the user need not choose one themselves.但是,我也希望点的颜色发生变化,每次选择不同的颜色是自动的,用户不需要自己选择。 So for one input$case points I want what is written in geom_point "orangered2".因此,对于一个 input$case 点,我想要 geom_point "orangered2" 中写的内容。 But if they choose the other input$case option, I would like the points in geom_point to be "gold".但是,如果他们选择其他 input$case 选项,我希望 geom_point 中的点为“黄金”。

Maybe an if statement but I am not sure where to nest that if so.也许是一个 if 语句,但如果是这样,我不确定在哪里嵌套。

I posted a snippet of my UI and server bits.我发布了我的 UI 和服务器位的片段。

UI snippet from a tab选项卡中的 UI 片段

tabPanel("Analysis",
                  sidebarLayout(
                      sidebarPanel(width = 4,
                             selectInput(inputId = "case", 
                                         label="Locations",
                                         choices = c("TRUE", "FALSE")))

Server snippet服务器片段

server <- function(input, output){
data_use <- reactive({
        real_final[real_final$case %in% input$case,]
    })
    output$bathy <- renderPlot({
        autoplot.bathy(shelf, geom=c("raster", "contour")) +
            scale_fill_gradientn(name = "meters above\nsea level", colours = c("plum2", "steelblue4","steelblue3", "steelblue2", "steelblue1"),
                                 breaks = c(-6000,0),
                                 limits = c(-6000,0),
                                 labels = c("-6000m","0m"), 
                                 na.value = "black") +
            geom_point(data = data_use(), aes(long, lat), color = "orangered2", pch = ".") +
            xlab("Longitude") +
            ylab("Latitude") +
            ggtitle("Seal Locations") +
            theme(axis.text.x=element_text(size=6),
                  axis.text.y=element_text(size=6),
                  axis.title=element_text(size=10,face="bold"))

An option is to return a list with a reactive conductor:一个选项是返回一个带有反应导体的列表:

data_and_color <- reactive({
  list(
    data = real_final[real_final$case %in% input$case,],
    color = ifelse(input$case == "TRUE", "gold", "orangered2")
  )
})

Then in the renderPlot :然后在renderPlot

x <- data_and_color()
ggplot(data = x$data, ......)
color = x$color

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

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