0

The provided code defines the server function for a Shiny application. This application appears to be part of a dashboard aimed at analyzing competitive positioning based on intellectual property in the optics industry.

Here's a breakdown of what the code is doing:

  1. Reactivity and Observers: It sets up reactive values and observers to update the UI based on user inputs. For example, r_list is a reactiveValues object that stores text inputs from the user interface.

  2. Loading Dataset: When the user clicks the "Load" button, the app loads various datasets related to patents, assignees, CPC codes, inventors, and locations. These datasets are read from CSV files and stored in reactiveVal objects (assignee, cpc, inventor, location, patent).

  3. Flagging Patents Based on Segments: The loaded CPC codes are used to flag patents based on specific market segments defined in a separate CSV file. This is done by checking whether the CPC codes associated with each patent match the CPC codes corresponding to the market segments.

  4. Generating Competitive Positioning: When the user clicks the "Generate" button, the app generates a competitive positioning analysis based on the selected market segments. It identifies the top 10 assignees with the highest number of unique patents and calculates the number of patents each assignee has in the selected market segments.

  5. Rendering Output: The output includes text labels (text_labels_output) and a heatmap (competitive_landscape_heatmap). The text labels display user-selected market segments and additional information, while the heatmap visualizes the competitive positioning data using Plotly.

Overall, this server function sets up the backend logic for the competitive positioning analysis within the Shiny application, handling data loading, processing, and visualization. I have used chatgpt like 50 times and I have degugged a couple of times and have no idea why it isn't working. Any suggestions?:

# define the server function
server <- function(input,output,session) {
  
  # Reactivity:
  # As a general rule, use reactive() when you just want to update something based on a new value (e.g. a user input)
  # and use reactiveVal or reactiveValues when you have an object that you want to maintain a state
  
  # Reactive values and observers
  r_list <- reactiveValues(txt = 'You selected: ',txt2 = 'You typed: ')
  
  observe({
    output$text_labels_output <- renderText({
      paste(r_list$txt, paste(input$markets_input, sep = '', collapse = ', '), sep = '', collapse = '')
    })
  })
  
  assignee <- reactiveVal()
  cpc <- reactiveVal()
  inventor <- reactiveVal()
  location <- reactiveVal()
  patent <- reactiveVal()
  
  # ObserveEvent
  observeEvent(input$load_dataset, {
    
    assignee(fread('/Users/dallanclarke/Desktop/Advanced Strat and Analytics/Optics Case/Data/lasers/assignee.csv'))
    cpc(fread('/Users/dallanclarke/Desktop/Advanced Strat and Analytics/Optics Case/Data/lasers/cpc.csv'))
    inventor(fread('/Users/dallanclarke/Desktop/Advanced Strat and Analytics/Optics Case/Data/lasers/inventor.csv'))
    location(fread('/Users/dallanclarke/Desktop/Advanced Strat and Analytics/Optics Case/Data/lasers/location.csv'))
    patent(fread('/Users/dallanclarke/Desktop/Advanced Strat and Analytics/Optics Case/Data/lasers/patent.csv'))
    
    # flag patents based on segments
    
    for (segment in unique(segments$segment_name)) {
      temp_values <- ifelse(
        grepl(pattern = paste(segments %>% filter(segment_name == segment) %>% pull(segment_cpc), collapse = '|'),
              x = cpc()$cpc_group,
              ignore.case = TRUE),
        TRUE,
        FALSE
      )
      temp_cpc <- cpc()
      temp_cpc[[segment]] <- temp_values
      cpc(temp_cpc)
    }
    
    showModal(
      modalDialog(
        paste('All done loading ', input$dataset, ' data!')
      )
    )
    
  })
  
  # Create a heatmap
  Plot_data <- reactiveVal()
  
  observeEvent(input$generate_competitive_positioning, {
    req(input$markets_input_select)
    
    peers <- assignee() %>%
      group_by(disambig_assignee_organization) %>%
      summarise(n = uniqueN(patent_id)) %>%
      arrange(desc(n)) %>%
      slice(1:10) %>%
      pull(disambig_assignee_organization)
    
    dt <- merge(cpc(), assignee(), by = 'patent_id')
    Plot_data(
      dt %>%
        filter(disambig_assignee_organization %in% peers) %>%
        select(patent_id, disambig_assignee_organization, input$markets_input_select) %>%
        pivot_longer(cols = input$markets_input_select, names_to = 'segment') %>%
        filter(value == TRUE) %>%
        group_by(disambig_assignee_organization, segment) %>%
        summarise(n = uniqueN(patent_id)) %>%
        pivot_wider(id_cols = disambig_assignee_organization, names_from = segment, values_from = n, values_fill = 0)
    )
    
  })
  
  output$competitive_landscape_heatmap <- renderPlotly({
    req(Plot_data())
    
    selected_segment <- input$markets_input
    print(selected_segment)
    
    plot_ly(data = Plot_data(), x = ~disambig_assignee_organization, y = ~.data[[selected_segment]], z = ~.data[[selected_segment]], type = 'heatmap')
  })
  
}

I am trying to output a plotly chart on this dashboard

0

Browse other questions tagged or ask your own question.