通过selectinput在闪亮的应用程序中选择列

时间:2020-10-21 16:01:42

标签: r shiny selectinput

我正在尝试构建一个闪亮的应用程序,这很不错,但是我试图将我的数据框中的一列放入selectinput中,但是到目前为止,还没有找到解决方案。我有一列包含505个因数的列,称为AAPL,AAL等。我希望在selectinput中包含这些因数,以便您可以从这505个因数中进行选择,这是我现在的代码,以及我正在尝试的列名进入selectinput的是bcl-data $ Name。

var myArray = [
      {
      "id": "123",
      "station": {
        "id": 5,
        "name": "Teststation"
      },
      "values": [
        {
          "id": "way",
          "values": [ 339, 340, 341 ]
        },
        {
          "id": "time",
          "values": [ 1, 2, 3 ]
        },
        {
          "name": "element_1",
          "type": "line",
          "result": "nok"
        },
        {
          "name": "element_2",
          "type": "rect",
          "result": "nok"
        },
        {
          "name": "element_3",
          "type": "line",
          "result": "ok"
        }
      ]
    }
  ];

function addColor(myArray) {
    const x =  myArray.map(obj => {

      for (const prop in obj) {
        if (obj.hasOwnProperty(prop) && Array.isArray(obj[prop])) {
          for (const item in obj[prop]) {
            if (obj[prop][item].hasOwnProperty('result') && (obj[prop][item].type === 'line')) {
              obj[prop][item].result === 'nok' ? obj[prop][item].line = { color: 'red' } : obj[prop][item].line = { color: 'green' };
          } else if (obj[prop][item].hasOwnProperty('result') && (obj[prop][item].type === 'rect')) {
              obj[prop][item].opacity = 0.2;
              obj[prop][item].line = { color: 'gray', width: 0 };
              obj[prop][item].result === 'nok' ? (obj[prop][item].fillcolor = 'red') : (obj[prop][item].fillcolor = 'green');
          }
          }
        }
    }

    return obj; //<---- Check here
  });

  return x;
 }

 addColor(myArray);

1 个答案:

答案 0 :(得分:1)

来自上方的评论:我认为您是错误的bcl-data$Name。尽管bcl-data.csv是您加载的文件,但已将其另存为对象bcl-意味着它应该只是bcl$NameselectInput(inputId = "typeInput", label = "Name", choices = bcl$Name)在过滤器中,您也可以简单地拥有filter(Name ==,因为您已经通过管道提供了bcl数据/对象。

为确保我们删除重复的值,我们可以添加unique

这是我认为应该起作用的(无法测试,因为没有数据)。

library(shiny)
library(tidyverse)
library(shinythemes)
library(ggplot2)
library(dplyr)



bcl <- read.csv("bcl-data.csv", stringsAsFactors = FALSE)



# Define UI for application that draws a histogram
ui <- fluidPage(theme = shinytheme("darkly"),
                
                
                
# Application title
titlePanel("Overzicht S&P 500 Aandelen"),
                
                
                
# Sidebar with a slider input for number of bins 
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "priceInput", label = "close", min = 0, max = 2050, value = c(0,300), pre = "$"),
selectInput(inputId = "typeInput", label = "Name", choices = unique(bcl$Name)), 
dateRangeInput(inputId = "dateInput", 
label = "date", 
start = "2013/02/08", 
end = "2013/03/08", 
 format = "yy/mm/dd")
),
                    
                    
                    
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(
tabPanel("Plot", plotOutput("Plot")),
tabPanel("Datatable", tableOutput("Datatable"))
            )
        )
    )
)



# Define server logic required to draw a histogram
server <- function(input, output) { 
    output$Plot <- renderPlot({ 
        filtered <- bcl %>%
            filter(close >= input$priceInput[1]) %>%
            filter(close <= input$priceInput[2]) %>%
            filter(date >= input$dateInput[1] & date <= input$dateInput[2]) %>%
            filter(Name == input$typeInput)
        filtered
        ggplot(filtered, aes(x = date, y = close, color = Name)) +
            geom_point()
    })
    output$Datatable <- renderTable({
        filtered <-
            bcl %>%
            filter(close >= input$priceInput[1]) %>%
            filter(close <= input$priceInput[2]) %>%
            filter(date >= input$dateInput[1] & date <= input$dateInput[2]) %>%
            filter(Name == input$typeInput)
        filtered
    })
}



# Run the application 
shinyApp(ui = ui, server = server)