簡體   English   中英

從 dataframe 到 plot 中 Shiny ZE1E1D3D40573127E9ZEE0480D 中的列中選擇變量

[英]Selecting variables from columns in dataframe to plot in Shiny R

我正在嘗試為 R 中的 Shiny 應用程序創建一個 EDA 選項卡,但已經遇到了第一個障礙。 在我的應用程序中,我希望用戶可以 select 數據中每一列中盡可能多或盡可能少的變量進行分析。 這是一個模擬 dataframe 和相關庫:-

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)

這是一些 Shiny 代碼:-

#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 允許用戶對 select 列進行分析。 varSelectInput 是我嘗試允許用戶從所選列中選擇要分析的變量。 但是,錯誤消息(由此引起):-

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

這是:-

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

如您所見,我的 Shiny 專業知識並不出色。 我該如何整理它,以便我可以選擇列並選擇我想要分析的相關變量?

一種方法是使用renderUI()到 select 因子。 嘗試這個

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)

輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM