代码之家  ›  专栏  ›  技术社区  ›  sectechguy

闪亮的应用程序使用较少的if-else语句

  •  0
  • sectechguy  · 技术社区  · 6 年前

    我有一个闪亮的应用程序,我建立了显示代理排名的用户随着时间的推移。闪亮的应用程序有一个下拉列表,您可以在其中选择一个用户,然后一个新的线图与该用户的数据将被绘制。然而,下面的代码只在5个用户身上进行了测试,这对编写代码来说并不痛苦。但我希望有成千上万的用户。有没有更好的方法来调整这个代码,这样我就不必为每个用户在代码中添加单独的Plotly图了?

    数据样本:

        devicereceipttime           sourceusername  GlobalRank
    0   2018-12-04T09:26:54.000Z    1111            8507790
    1   2018-12-04T09:27:05.000Z    2222            2648
    2   2018-12-04T09:27:05.000Z    3333            156433
    3   2018-12-04T09:27:10.000Z    5555            295
    4   2018-12-04T17:14:51.000Z    1111            1
    5   2018-12-04T17:14:51.000Z    4444            1
    6   2018-12-04T17:15:11.000Z    2222            373436
    7   2018-12-04T17:15:20.000Z    1111            250639
    8   2018-12-04T17:15:32.000Z    1111            1
    9   2018-12-04T17:15:38.000Z    2444            2965900
    10  2018-12-04T17:16:00.000Z    2222            250639
    11  2018-12-04T10:52:17.000Z    1111            799963
    12  2018-12-04T10:52:26.000Z    3333            1
    13  2018-12-04T10:52:26.000Z    1111            799963
    14  2018-12-04T17:16:20.000Z    2222            250639
    15  2018-12-04T10:52:26.000Z    4444            1
    16  2018-12-04T10:52:27.000Z    4444            1
    17  2018-12-04T10:52:26.000Z    1111            2648
    

    R代码:

    #Import libraries
    library(shiny)
    library(plotly)
    library(tidyr)
    library(bindrcpp)
    
    # Define UI for application that draws a histogram
    ui <- bootstrapPage(
    
      selectInput(inputId = "n_breaks",
                  label = "Select Data:",
                  choices = c("User1","User2","User3","User4","User5"),
                  selected = "User1"),
    
      plotlyOutput(outputId = "main_plot", height = "500px")
    )
    
    # Define server logic required to draw a Line chart
    server <- function(input, output) {
    
      #Import data
      df1 = read.csv("Data/all_5_test_users.csv")
      #Do cool things with the data
      df2 <- df1 %>% 
        gather(variable, value, -(devicereceipttime:sourceusername)) %>% 
        unite(temp, sourceusername, variable) %>% 
        group_by(temp) %>% 
        mutate(id=1:n()) %>% 
        spread(temp, value) 
    
      #Remove the id column
      df2 <- subset(df2, select = -c(id))
      #Remove "_GlobalRank" from titles
      names(df2) = gsub(pattern = "_GlobalRank*", replacement = "", x = names(df2)) 
    
      output$main_plot <- renderPlotly({
    
        if (input$n_breaks == "User1") {
          #Create variable X 
          x <- df2$devicereceipttime[!is.na(df2$`1111`)]
          #Create variable Y
          y <- df2$`1111`[!is.na(df2$`1111`)]
          #Plotly plot
          plot_ly(x = x, y = y, type = 'scatter', mode = 'lines') %>%
            layout(
              margin = list(b = 190, l = 50)) # to fully display the x and y axis labels
        }
        else if (input$n_breaks == "User2") {
          #Create variable X 
          x <- df2$devicereceipttime[!is.na(df2$`2222`)]
          #Create variable Y 
          y <- df2$`2222`[!is.na(df2$`2222`)]
          #Plotly plot
          plot_ly(x = x, y = y, type = 'scatter', mode = 'lines') %>%
            layout(
              margin = list(b = 190, l = 50)) # to fully display the x and y axis labels 
        }
        else if (input$n_breaks == "User3") {
          #Create variable X 
          x <- df2$devicereceipttime[!is.na(df2$`3333`)]
          #Create variable Y 
          y <- df2$`3333`[!is.na(df2$`3333`)]
          #Plotly plot
          plot_ly(x = x, y = y, type = 'scatter', mode = 'lines') %>%
            layout(
              margin = list(b = 190, l = 50)) # to fully display the x and y axis labels  
        } 
        else if (input$n_breaks == "User4") {
          #Create variable X 
          x <- df2$devicereceipttime[!is.na(df2$`4444`)]
          #Create variable Y 
          y <- df2$`4444`[!is.na(df2$`4444`)]
          #Plotly plot
          plot_ly(x = x, y = y, type = 'scatter', mode = 'lines') %>%
            layout(
              margin = list(b = 190, l = 50)) # to fully display the x and y axis labels  
        }
        else if (input$n_breaks == "User5") {
          #Create variable X 
          x <- df2$devicereceipttime[!is.na(df2$`5555`)]
          #Create variable Y 
          y <- df2$`5555`[!is.na(df2$`5555`)]
          #Plotly plot
          plot_ly(x = x, y = y, type = 'scatter', mode = 'lines') %>%
            layout(
              margin = list(b = 190, l = 50)) # to fully display the x and y axis labels  
        }
      })   
    }
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    Image of Shiny App

    编辑:我尝试了以下方法:

    #Import libraries
    
    library(shiny)
    library(plotly)
    library(tidyr)
    library(bindrcpp)
    
    # Define UI for application that draws a histogram
    ui <- bootstrapPage(
    
      selectInput(choices = c("User1" = "1111", "User2" = "2222", "User3" = "3333", "User4" = "4444", "User5" = "5555")),
    
      plotlyOutput(outputId = "main_plot", height = "500px")
    )
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
    
      #Import data
      df1 = read.csv("Data/all_5_test_users.csv")
      #Do cool things with the data
      df2 <- df1 %>% 
        gather(variable, value, -(devicereceipttime:sourceusername)) %>% 
        unite(temp, sourceusername, variable) %>% 
        group_by(temp) %>% 
        mutate(id=1:n()) %>% 
        spread(temp, value) 
    
      #Remove the id column
      df2 <- subset(df2, select = -c(id))
      #Remove "_event_count_slc" from titles
      names(df2) = gsub(pattern = "_GlobalRank*", replacement = "", x = names(df2)) 
    
      output$main_plot <- renderPlotly({
    
        observeEvent(input$n_breaks,{
          #Create variable X 
          x <- df2$devicereceipttime[!is.na(df[[input$n_breaks]])]
          #Create variable Y
          y <- df[[input$n_breaks]][!is.na(df[[input$n_breaks]])]
          #Plotly plot
          plot_ly(x = x, y = y, type = 'scatter', mode = 'lines') %>%
            layout(
              margin = list(b = 190, l = 50)) # to fully display the x and y axis labels  
          updatePlotly("your plot id")
          })
        })
      }  
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    Listening on http://127.0.0.1:7673
    Warning: Error in UseMethod: no applicable method for 'ggplotly' applied to an object of class "c('Observer', 'R6')"
    Stack trace (innermost first):
        82: "plotly"::"ggplotly"
        81: func
        80: origRenderFunc
        79: output$main_plot
         4: <Anonymous>
         3: do.call
         2: print.shiny.appobj
         1: <Promise>
    Warning: Error in [[: object of type 'closure' is not subsettable
    
    
    
    df2
    # A tibble: 55,296 x 6
       devicereceipttime          `1111`  `2222`  `3333`  `4444`  `5555`
       <fct>                      <int>   <int>   <int>   <int>   <int>
     1 2018-12-04T00:00:00.000Z      0       1      0       0       0
     2 2018-12-04T00:00:05.000Z      0       1      0       0       0
     3 2018-12-04T00:00:24.000Z      0       1      0       0       0
     4 2018-12-04T00:00:26.000Z      0       1      0       0       0
     5 2018-12-04T00:00:45.000Z      0       1      0       0       0
     6 2018-12-04T00:00:50.000Z      0       1      0       0       0
     7 2018-12-04T00:01:00.000Z      0       1      0       0       0
     8 2018-12-04T00:01:26.000Z      0       1      0       0       0
     9 2018-12-04T00:01:45.000Z      0       1      0       0       0
    10 2018-12-04T00:01:46.000Z      0       1      0       0       0
    # ... with 55,286 more rows
    

    x轴是日期时间,y轴是所选用户的计数。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Mohammed Ali    6 年前

    首先,除非你希望每次有人加载你的应用程序时都加载你的数据集,否则要在两个应用程序之外加载数据 server Ui 功能。

    回答您的问题:

    首先,让你的选择 selectInput

    choices = c("User1" = "1111","User2" = "2222","User3" = "3333")
    

    在您的情况下,您必须从您的数据帧中获取键/值

    然后,你可以使用 obserEvent 在你的 选择输入 要使用新选定的输入更新绘图,请执行以下操作:

    observeEvent(input$n_breaks,{
        #Create variable X 
      x <- df2$devicereceipttime[!is.na(df[[input$n_breaks]])]
      #Create variable Y
      y <- df[[input$n_breaks]][!is.na(df[[input$n_breaks]])]
        updatePlotly("your plot id")
        })
    
    推荐文章