【问题标题】:Selecting variables from columns in dataframe to plot in Shiny R从数据框中的列中选择变量以在 Shiny R 中绘制
【发布时间】:2021-04-29 09:23:40
【问题描述】:

我正在尝试为 R 中的 Shiny 应用程序创建一个 EDA 选项卡,但已经遇到了第一个障碍。在我的应用程序中,我希望用户可以从数据中的每一列中选择尽可能多或尽可能少的变量进行分析。这是一个模拟数据框和相关库:-

library(wakefield)#for generating the Status variable
library(dplyr)
library(shiny)
library(shinydashboard)
library(funModeling)
set.seed(1)
Date<-seq(as.Date("2015-01-01"), as.Date("2015-12-31"), by = "1 day")
Date<-sample(rep(Date,each=10),replace = T)


Shop<-r_sample_factor(x = c("Shop_A", "Shop_B", "Shop_C","Shop_D", "Shop_E","Shop_F","Shop_G"), n=length(Date))
Product<-r_sample_factor(x=c("Meat","Fruit","Vegetables","Toiletries","Kitchenware","CleaningProducts"), n=length(Date))
Profit<-sample(1:150, length(Date), replace=TRUE)
Profit


data<-data.frame(Date,Shop,Product,Profit)
levels(data$Shop)
#[1] "Shop_A" "Shop_B" "Shop_C" "Shop_D" "Shop_E" "Shop_F" "Shop_G"
levels(data$Product)
#[1] "Meat"             "Fruit"            "Vegetables"       "Toiletries"       "Kitchenware"      "CleaningProducts"
View(data)

这里有一些闪亮的代码:-

#UI
ui<-fluidPage(
  
  tabPanel("EDA",
           sidebarLayout(
             sidebarPanel(width = 4, 
                          dateRangeInput("eda_daterange","Select date range", format="yyyy-mm-dd",
                                         start=min(data$Date),
                                         end=max(data$Date)),
                          pickerInput("eda_col", "Select variable",
                                      choices = c("Shop",
                                                  "Product")),
                          varSelectInput("level_choice", "Select factors to include",
                                         input$eda_col, multiple = T),
                          
                          actionButton("run_eda", "Run analysis")),
             mainPanel(
               column(width = 8, box("Frequency plot", plotOutput("frequencyplot_eda"), width = "100%")),
               column(width = 8, box("Profit plot", plotOutput("density_eda"), width = "100%"))
               
             )          
             
           )
           
           
  ))

#SERVER
server<-function(input,output,session){
  
  #Calls_new_reac<-reactive(Calls_new)
  variables<-unique(input$eda_col)
  
  
  observeEvent(input$run_eda,{
    
    output$frequencyplot_eda<-renderPlot({
      
      if(input$eda_col=="Shop"){
        
        data<-data%>%
          filter(Date>=input$eda_daterange[1] & Date<=input$eda_daterange[2])%>%
          filter(variables %in% input$level_choice)
        
        
        freqplot<-freq(data = data, input =input$eda_col )
        
        return(freqplot)
        
      }else{
        
        if(input$eda_col=="Product"){
          
          data<-data%>%
            filter(Date>=input$eda_daterange[1] & Date<=input$eda_daterange[2])%>%
            filter(variables %in% input$level_choice)
          
          
          freqplot<-freq(data = data, input =input$eda_col )
          
          return(freqplot)
          
        }
        
      }
      
      
      
      
    })
    
    
    output$density_eda<-renderPlot({
      
      if(input$eda_col=="Shop"){
        
        data<-data%>%
          filter(Date>=input$eda_daterange[1] & Date<=input$eda_daterange[2])%>%
          filter(variables %in% input$level_choice)
        
        densplot<-ggplot(data, aes(x=Profit,group=input$eda_col,colour=input$eda_col))+geom_density()+scale_x_log10()
        
        
        
        return(densplot)
        
      }else{
        
        if(input$eda_col=="Product"){
          
          data<-data%>%
            filter(Date>=input$eda_daterange[1] & Date<=input$eda_daterange[2])%>%
            filter(variables %in% input$level_choice)
          
          densplot<-ggplot(data, aes(x=Profit,group=input$eda_col,colour=input$eda_col))+geom_density()+scale_x_log10()
          
          
          return(densplot)
          
        }
        
      }
      
    })
    
  })#end of observe event
  
}


shinyApp(ui, server)

第一个 pickerInput 允许用户选择要分析的列。 varSelectInput 是我尝试允许用户从所选列中选择要分析的变量。但是,错误信息(由此引起):-

varSelectInput("level_choice", "Select factors to include",
                                         input$eda_col, multiple = T)

这是:-

Error in is.data.frame(x) : object 'input' not found

如您所见,我的 Shiny 专长并不出色。 我该如何整理它,以便我可以选择列并选择我想要分析的相关变量?

【问题讨论】:

    标签: r shiny visualization shinydashboard shinyapps


    【解决方案1】:

    一种方法是使用renderUI() 选择因子。试试这个

    data<-data.frame(Date,Shop,Product,Profit)
    
    ui<-fluidPage(
      
      tabPanel("EDA",
               sidebarLayout(
                 sidebarPanel(width = 4, 
                              dateRangeInput("eda_daterange","Select date range", format="yyyy-mm-dd",
                                             start=min(data$Date),
                                             end=max(data$Date)),
                              pickerInput("eda_col", "Select variable",
                                          choices = c("Shop", "Product")),
                              uiOutput("varselect"),
                              
                              actionButton("run_eda", "Run analysis")),
                 mainPanel(
                   # DTOutput("t1"),
                   column(width = 8, box("Frequency plot", plotOutput("frequencyplot_eda"), width = "100%")),
                   column(width = 8, box("Profit plot", plotOutput("density_eda"), width = "100%"))
                   
                 )          
                 
               )
               
               
      ))
    
    #SERVER
    server<-function(input,output,session){
      
      output$varselect <- renderUI({
        vars <- data[[as.name(input$eda_col)]]
        selectInput("level_choice", "Select factors to include", unique(vars) , multiple = T)
      })
      
      output$t1 <- renderDT({
        req(input$level_choice)
        data %>%
          filter(Date>=input$eda_daterange[1] & Date<=input$eda_daterange[2]) %>%
          filter(.data[[input$eda_col]] %in% input$level_choice)
        
      })
      
      observeEvent(input$run_eda,{
        req(input$level_choice)
        
        output$frequencyplot_eda<-renderPlot({
          
          data1<-data%>%
            filter(Date>=input$eda_daterange[1] & Date<=input$eda_daterange[2])%>%
            filter(.data[[input$eda_col]] %in% input$level_choice)
          
          freqplot<-freq(data = data1, input = data1[[input$eda_col]] )
          
          return(freqplot)
          
        })
        
        output$density_eda<-renderPlot({
          
          data2 <- data %>%
            filter(Date>=input$eda_daterange[1] & Date<=input$eda_daterange[2])%>%
            filter(.data[[input$eda_col]] %in% input$level_choice)
          
          densplot<-ggplot(data2, aes(x=Profit,group=.data[[input$eda_col]],colour=input$eda_col))+
                      geom_density()+scale_x_log10()
          
          return(densplot)
          
        })
        
      })#end of observe event
      
    }
    
    
    shinyApp(ui, server)
    

    【讨论】:

    • 这太好了,谢谢!我有一个额外的查询。我将如何为每个因素着色?例如,在上图中的密度图中,我们有 2 个变量。我如何使用 ggplot 将它们编码为不同的颜色(aes 中的常用颜色参数不起作用)?
    • 其实没关系,我把它们包装在这个:colour=.data[[input$eda_col]]。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 2021-04-27
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多