简体   繁体   English

在 Shiny 中更改图表中的值

[英]Changing values in the chart in Shiny

I have a simple shiny application and I don't know why the values on the chart change when I choose multiple items from a list.我有一个简单的 shiny 应用程序,我不知道为什么当我从列表中选择多个项目时图表上的值会发生变化。 Below my example and images with bad charts.下面是我的示例和带有不良图表的图像。

ui用户界面

library(shiny)
library(plotly)

shinyUI(fluidPage(

    titlePanel("App test"),

    sidebarPanel(
        h3(" "),
        selectizeInput("name",
                       label = "Code",
                       choices = unique(data$Product),
                       multiple = T,
                       options = list(maxItems = 5, placeholder = 'Select a code'),
                       selected = "46")
        ),

    mainPanel(
        plotOutput("trendPlot")
    )
)
)

server服务器

shinyServer(function(input, output, session) {

    output$trendPlot <- renderPlot({

            df_trend <- data[data$Product == input$name, ]

            ggplot(df_trend) +
                geom_line(aes(x = Month, y = Value, group = Product, colour = Product)) +
                theme(legend.position = "bottom")              

    })
})

Head my data:头我的数据:

> head(data)
# A tibble: 6 x 3
  Product Month Value
  <chr>   <chr> <dbl>
1 46      Jan     188
2 46      Feb     277
3 46      Mar     317
4 46      Apr     367
5 46      May     329
6 46      Jun     318

在此处输入图像描述

在此处输入图像描述

The data set above only includes '46' for Product so cannot reproduce.上面的数据集仅包括产品的“46”,因此无法复制。 However, I suspect the problem is how you are filtering data, allowing for multiple inputs with selectizeInput .但是,我怀疑问题在于您如何过滤数据,允许使用selectizeInput进行多个输入。

Right now you filter data:现在你过滤数据:

df_trend <- data[data$Product == input$name, ]

Which is fine if input$name is a single value.如果input$name是单个值,这很好。 However, with multiple inputs (eg, 46 and 92), then input$name contains those two values, and you need a different kind of comparison.但是,如果有多个输入(例如,46 和 92),那么input$name包含这两个值,您需要进行不同类型的比较。

To subset your data based on multi-value matching (like matching a vector), try instead:要基于多值匹配(例如匹配向量)对数据进行子集化,请尝试:

df_trend <- data[data$Product %in% input$name, ]

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

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