1

I'm trying to build a shiny app, it is good to go but I am trying to put a column from my dataframe in selectinput, but so far didn't found a solution. I have a column with 505 factors, called AAPL, AAL, etc.. I want these factors in my selectinput, so that you can choose from these 505 factors, This is my code right now, and the column name that I'm trying to get in selectinput is bcl-data$Name.

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 = (bcl-data$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(bcl-data$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(bcl-data$Name == input$typeInput)
        filtered
    })
}



# Run the application 
shinyApp(ui = ui, server = server)
Erdem Akca
  • 41
  • 5
  • 1
    I think you're error is with `bcl-data$Name`. While `bcl-data.csv` is the file you loaded, you saved it as the object `bcl` - meaning it should simply be `bcl$Name`. `selectInput(inputId = "typeInput", label = "Name", choices = bcl$Name)` In your filters, you can also simply have `filter(Name == ` because you're already feeding the `bcl` data/object through the pipe. – TTS Oct 21 '20 at 16:05
  • When I try that it gives me the following error: Error in hasGroups(choices) : object 'bcldata' not found – Erdem Akca Oct 21 '20 at 16:09
  • Exactly. `bcldata` doesn't exist, but the object `bcl` does. You imported the `csv` and named it `bcl` : `bcl <- read.csv("bcl-data.csv", stringsAsFactors = FALSE)` – TTS Oct 21 '20 at 16:17
  • It does something, but column bcl-data$Name has 600.000 rows, I think it's grabbing all of them right now, however this column exists out of 500 factors, I want those factors in my selectinput. – Erdem Akca Oct 21 '20 at 16:21
  • You mean the Name column has many (600k) rows, but 500 unique values? – TTS Oct 21 '20 at 16:22
  • That's correct. My dataset is the stock prices from the past 5 years from 500 companies – Erdem Akca Oct 21 '20 at 16:24
  • We can grab only unique values with `unique(bcl$Name)`. You could also sort this by `sort(unique(bcl$Name))`. Changes added to answer below – TTS Oct 21 '20 at 16:27
  • You are amazing sir it worked! – Erdem Akca Oct 21 '20 at 16:28
  • Glad to help! Feel free to mark as answer :) – TTS Oct 21 '20 at 16:32

1 Answers1

1

Comment from above: I think you're error is with bcl-data$Name. While bcl-data.csv is the file you loaded, you saved it as the object bcl - meaning it should simply be bcl$Name. selectInput(inputId = "typeInput", label = "Name", choices = bcl$Name) In your filters, you can also simply have filter(Name == because you're already feeding the bcl data/object through the pipe.

To make sure we remove duplicate values, we can include unique.

Here's what I think should work (cannot test because no data).

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)
TTS
  • 1,818
  • 7
  • 16