【问题标题】:Select columname through selectinput in shiny appliaction在闪亮的应用程序中通过 selectinput 选择列名
【发布时间】:2021-02-04 14:24:08
【问题描述】:

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

【问题讨论】:

  • 我认为你的错误在于bcl-data$Name。虽然bcl-data.csv 是您加载的文件,但您将其保存为对象bcl - 这意味着它应该只是bcl$NameselectInput(inputId = "typeInput", label = "Name", choices = bcl$Name) 在您的过滤器中,您也可以简单地使用 filter(Name == ,因为您已经通过管道提供了 bcl 数据/对象。
  • 当我尝试它给我以下错误: hasGroups(choices) 中的错误:找不到对象'bcldata'
  • 没错。 bcldata 不存在,但对象 bcl 存在。您导入了 csv 并将其命名为 bclbcl &lt;- read.csv("bcl-data.csv", stringsAsFactors = FALSE)
  • 它做了一些事情,但列 bcl-data$Name 有 600.000 行,我认为它现在正在抓取所有行,但是该列存在于 500 个因素中,我希望这些因素在我的 selectinput 中。
  • 您的意思是 Name 列有很多 (600k) 行,但有 500 个唯一值?

标签: r shiny selectinput


【解决方案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)

【讨论】:

    猜你喜欢
    • 2014-12-29
    • 2018-10-25
    • 1970-01-01
    • 2018-10-17
    • 2019-01-29
    • 2018-10-07
    • 2018-02-21
    • 1970-01-01
    • 2014-04-11
    相关资源
    最近更新 更多