微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从数据框中的列中选择变量以在 Shiny R 中绘图

如何解决从数据框中的列中选择变量以在 Shiny R 中绘图

我正在尝试为 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,colour=input$eda_col))+geom_density()+scale_x_log10()
          
          
          return(densplot)
          
        }
        
      }
      
    })
    
  })#end of observe event
  
}


shinyApp(ui,server)

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

varSelectInput("level_choice",multiple = T)

这是:-

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

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

解决方法

一种方法是使用 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%")),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)

output

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。