R Shiny - 反应式 selectInput 数据框列 [英] R Shiny - Reactive selectInput data frame column

查看:50
本文介绍了R Shiny - 反应式 selectInput 数据框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个仪表板,可以从 Google 分析或 CSV 上传中提取数据,然后计算转化率和平均订单价值(用于 AB 测试).我一直在尝试实现允许选择设备类别(例如(手机、平板电脑或台式机)和产品类别(例如(卡片、礼物或鲜花))的过滤器.过滤器应从数据框中动态提取,然后可在下拉列表中进行选择.我在这个论坛上看到了很多类似的例子,但在我的一生中,我一直无法让它发挥作用.我看到的案例似乎使用了observe ({}),但我的问题似乎来自这样一个事实,即我需要首先将选择从反应式函数中传递出去.

I have got a dashboard that pulls data from Google analytics or a CSV upload and then calculate conversion rate and average order value(for AB testing purpose). I have been trying to implement filters that allow selecting device category e.g (mobile, tablet or desktop) and product category e.g(card, gift or flowers). The filters should pulled from from the data frame dynamically and then be available for selection in the drop downs. I have seen a lot of similar example of this forum but for the life of me I haven't been able to make it work. The cases I have seen seem to be using observe ({}) but my issue seem to be coming from the fact that I need to pass the choices out of the reactive function first.

下面是一个可重现的简化示例,生成的数据框与它们显示的一样.我在 UI 中注释掉了 #choices= Results()$Devices,以便向您展示它在损坏之前的样子.

Below is a reproducible, simplified example, with data frame generated as they would appear. I have commented out #choices= Results()$Devices in the UI so to show you how it looks like before it breaks.

非常感谢G

require(shiny)
require(shinydashboard)
require(googleVis)
require(dplyr)


ui <- dashboardPage(  
  skin="blue",

  dashboardHeader(
    title="Dashboard",
    titleWidth = 250

  ),

  dashboardSidebar(


    sidebarMenu(
      menuItem("Calculator ", tabName = "calculator", icon = icon("calculator"))

    )
  ),
  #
  dashboardBody(
    tabItems(
      tabItem(tabName = "calculator",
              h1("Calculator"),


              fluidRow(
                column(width = 1,
                       selectInput("device","Device:",
                                   #choices= Results()$Devices,
                                   multiple=TRUE, selectize=TRUE)
                ),
                column(width = 1,
                       selectInput("product","Product:",
                                   #"choices= Results()$Products",
                                   multiple=TRUE, selectize=TRUE)
                )
              ),

              fluidRow(

                column(width = 6,
                       box(title="Overall Conversion rate %",status="primary",solidHeader = TRUE,
                           htmlOutput("CRABCalcl"),width = "100%",height=275)
                ),

                column(width = 6,
                       box(title="Overall AOV £",status="primary",solidHeader = TRUE,
                           htmlOutput("AOVABCalcl"),width = "100%",height=275)
                )
              ),
              fluidRow(

                column(width = 6,
                       box(title="Ecommerce Conversion rate %",status="primary",solidHeader = TRUE,
                           htmlOutput("CRABCalclEHC"),width = "100%",height=275)
                ),

                column(width = 6,
                       box(title="Ecoomerce AOV £",status="primary",solidHeader = TRUE,
                           htmlOutput("AOVABCalclEHC"),width = "100%",height=275)
                )
              )

          )

      )#End of tab Item
    ) #end of tabItems

  )#End of Dashboard body
)#End of dashboardPage


server <- function(input, output,session) {
  Results <- reactive({

    myDataRAW<-data.frame(
      c("mobile","mobile","desktop","desktop","tablet","tablet"),
      c("Control","Challenger","Control","Challenger","Control","Challenger"),
      c(34355,34917,28577,29534,15337,13854),
      c(15011,15427,32190,32548,40299,40858),
      c(14636,14990,19609,19702,7214,7785),
      c(123273.70,20936.92,45179.05,46359.91,65765.27,92771.36),
      c(10370,13403,19241,26965,4468,8796)
    )

    myDataRAWEHC<-data.frame(
      c("desktop","desktop","mobile","mobile","tablet","tablet","desktop","desktop","mobile","mobile","desktop","desktop","mobile","mobile","tablet","tablet","tablet","tablet","desktop","desktop"),
      c("Card","Card","Card","Card","Card","Card","Card","Card","Gift","Gift","Gift","Card","Card","Card","Card","Card","Card","Card","Flower","Flower"),
      c("Standard","Standard","Standard","Standard","Standard","Standard","Large","Large","Large","Large","Square","Square","Square","Square","Large","Large","Square","Square","Flowers","Flowers"),
      c("Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger"),
      c(8767,18072,5729,13017,2908,7086,1655,2971,1008,2177,984,2369,599,1422,449,1052,402,1001,233,355),
      c(9055,18624,5908,13302,3015,7288,1691,3000,1013,2192,1009,2455,623,1450,455,1068,413,1017,233,356),
      c(21699.60,44480.95,14464.85,32590.30,7232.47,17483.35,8309.85,14684.68,5024.92,10844.67,2405.07,5826.83,1529.16,3556.38,2220.21,5192.92,992.14,2447.78,5196.08,8021.95)
    )

    names(myDataRAW)<-c("Device.Category","Segment","Users","Sessions","Transactions","Revenue","Quantity")
    names(myDataRAWEHC)<-c("Device.Category","Product.Category..Enhanced.Ecommerce.","Product.Variant","Segment","Unique.Purchases","Quantity","Product.Revenue")

    Devices<-myDataRAW$Device.Category
    Products<-unique(myDataRAWEHC$Product.Category..Enhanced.Ecommerce.)
#     DeviceFilter<-input$device
#     ProductFilter<-input$product
    #the below is replacing the above input to act as filters
    DeviceFilter<-c("desktop","mobile")
    ProductFilter<-c("Flower","Gift")
    myData<-myDataRAW %>% filter(Device.Category %in% DeviceFilter)
    myDataEHC<-myDataRAWEHC %>% filter(Device.Category %in% DeviceFilter) %>% filter(`Product.Category..Enhanced.Ecommerce.` %in% ProductFilter)

    myData<-bind_rows(myData,myData %>% group_by(Device.Category="All",Segment) %>% summarise(Users=sum(Users),Sessions=sum(Sessions),Transactions=sum(Transactions),Revenue=sum(Revenue),Quantity=sum(Quantity)))
    myDataEHC<-rbind(myDataEHC %>% group_by(Device.Category,Segment) %>% summarise(Transactions=sum(Unique.Purchases),Quantity=sum(Quantity),Revenue=sum(Product.Revenue)), 
                     myDataEHC %>% group_by(Device.Category="All",Segment) %>% summarise(Transactions=sum(Unique.Purchases),Quantity=sum(Quantity),Revenue=sum(Product.Revenue)) )
    myDataEHC<-left_join(myDataEHC,myData %>% select(Segment,Device.Category,Users,Sessions))
    myData$Analysis<-"Overall"
    myDataEHC$Analysis<-"Ecommerce"
    myDataForAnalysis<-rbind(as.data.frame(myData),as.data.frame(myDataEHC))


    myDataForAnalysis$CVR<-myDataForAnalysis$Transactions/myDataForAnalysis$Sessions
    myDataForAnalysis$AOV<-myDataForAnalysis$Revenue/myDataForAnalysis$Transactions

    DisplayResultsEHC<-myDataForAnalysis %>% filter(Analysis %in% "Ecommerce")
    DisplayResults<-myDataForAnalysis %>% filter(Analysis %in% "Overall")



    list(DisplayResultsEHC=DisplayResultsEHC,DisplayResults=DisplayResults,Devices=Devices,Products=Products)
  })



  output$CRABCalcl <- renderGvis({
    DataABCalcl<-Results()$DisplayResults
    F<-cast(DataABCalcl, Device.Category~Segment, value = 'CVR')
    X<-gvisColumnChart(F,options=list(legend="{position:'top'}",width="100%"))

  })

  output$AOVABCalcl <- renderGvis({
    DataABCalcl<-Results()$DisplayResults
    F<-cast(DataABCalcl, Device.Category~Segment, value = 'AOV')
    X<-gvisColumnChart(F,options=list(legend="{position:'top'}",width="100%"))
  })

  output$CRABCalclEHC <- renderGvis({
    DataABCalcl<-Results()$DisplayResultsEHC
    F<-cast(DataABCalcl, Device.Category~Segment, value = 'CVR')
    X<-gvisColumnChart(F,options=list(legend="{position:'top'}",width="100%"))

  })

  output$AOVABCalclEHC <- renderGvis({
    DataABCalcl<-Results()$DisplayResultsEHC
    F<-cast(DataABCalcl, Device.Category~Segment, value = 'AOV')
    X<-gvisColumnChart(F,options=list(legend="{position:'top'}",width="100%"))
  })


}

shinyApp(ui, server)

推荐答案

从我看来,一个好的开始是创建一个 global.R 文件,其中包含(并从 server.R 中删除):

From what I see a good start would be to create a global.R file containing (and remove from server.R):

全局.R

myDataRAW<-data.frame(
            c("mobile","mobile","desktop","desktop","tablet","tablet"),
            c("Control","Challenger","Control","Challenger","Control","Challenger"),
            c(34355,34917,28577,29534,15337,13854),
            c(15011,15427,32190,32548,40299,40858),
            c(14636,14990,19609,19702,7214,7785),
            c(123273.70,20936.92,45179.05,46359.91,65765.27,92771.36),
            c(10370,13403,19241,26965,4468,8796)
    )

    myDataRAWEHC<-data.frame(
            c("desktop","desktop","mobile","mobile","tablet","tablet","desktop","desktop","mobile","mobile","desktop","desktop","mobile","mobile","tablet","tablet","tablet","tablet","desktop","desktop"),
            c("Card","Card","Card","Card","Card","Card","Card","Card","Gift","Gift","Gift","Card","Card","Card","Card","Card","Card","Card","Flower","Flower"),
            c("Standard","Standard","Standard","Standard","Standard","Standard","Large","Large","Large","Large","Square","Square","Square","Square","Large","Large","Square","Square","Flowers","Flowers"),
            c("Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger"),
            c(8767,18072,5729,13017,2908,7086,1655,2971,1008,2177,984,2369,599,1422,449,1052,402,1001,233,355),
            c(9055,18624,5908,13302,3015,7288,1691,3000,1013,2192,1009,2455,623,1450,455,1068,413,1017,233,356),
            c(21699.60,44480.95,14464.85,32590.30,7232.47,17483.35,8309.85,14684.68,5024.92,10844.67,2405.07,5826.83,1529.16,3556.38,2220.21,5192.92,992.14,2447.78,5196.08,8021.95)
    )

    names(myDataRAW)<-c("Device.Category","Segment","Users","Sessions","Transactions","Revenue","Quantity")
    names(myDataRAWEHC)<-c("Device.Category","Product.Category..Enhanced.Ecommerce.","Product.Variant","Segment","Unique.Purchases","Quantity","Product.Revenue")

这允许您从 ui.R 访问 myDataRAW 和 myDataRAWEHC.相应地修改 ui.R:

This allows you to access myDataRAW and myDataRAWEHC from ui.R. Modify the ui.R accordingly:

fluidRow(
                                    column(width = 3,
                                           selectInput("device","Device:",
                                                       choices= levels(myDataRAW$Device.Category),
                                                       multiple=TRUE, selectize=TRUE)
                                    ),
                                    column(width = 3,
                                           selectInput("product","Product:",
                                                       choices= unique(levels(myDataRAWEHC$Product.Category..Enhanced.Ecommerce.)),
                                                       multiple=TRUE, selectize=TRUE)
                                    )
                            ),

在那之后,您还有一些工作需要重新安排 server.R 部分.

after that you still have some work left to rearrange the server.R part.

这篇关于R Shiny - 反应式 selectInput 数据框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆