R/generic_functions.R

Defines functions score_recovery create_numeric_input selectize_names selectize_count

Documented in create_numeric_input score_recovery selectize_count selectize_names

#'@title Computation of the amount of items selected in the selectizeInput
#'
#'@description Compute the amount of items selected in the selectizeInput
#'
#'@author Marie Bellier, Massimo Finini, Meri Likoska, Vania Rodrigues Telo Ramos, Xavier Renger
#'
#'@param id inputId of the selectizeInput
#'
#'@return return the number of values selected
#'
#'#let's say the user selected those anime in the app
#'selectize <- c("Naruto", "Death Note", "One Piece")
#'
#'#we will then recover the following with the function
#'t <- selectize_count(selectize)
#'
#'print(t)
#'
#'@export

selectize_count <- function(id){
  selectedcount <- length(unique(id))
}

#'@title Vector of names selected in the selectizeInput
#'
#'@description Get the vector of names selected in the selectizeInput
#'
#'@author Marie Bellier, Massimo Finini, Meri Likoska, Vania Rodrigues Telo Ramos, Xavier Renger
#'
#'@param id inputId of the selectizeInput
#'
#'@return return a vector of the item names selected
#'
#'@examples
#'
#'#let's say the user selected those anime in the app
#'selectize <- c("Naruto", "Death Note", "One Piece")
#'
#'#we will then recover the following with the function
#'u <- selectize_names(selectize)
#'
#'print(u)
#'
#'@export

selectize_names <- function(id){
  selectednames = id
}


#'@title Numeric inputs depending on the selection of the user in the selectizeInput
#'
#'@description Create numeric inputs depending on the selection of the user in the selectizeInput. You first need to run both `selectize_count()` and `selectize_names()` to run this.
#'
#'@author Marie Bellier, Massimo Finini, Meri Likoska, Vania Rodrigues Telo Ramos, Xavier Renger
#'
#'@param selectednames names recovered through `selectize_names()`
#'@param selectedcounts count recovered through `selectize_count()`
#'@param min_user minimum value of the numericInputs
#'@param max_user maximum value of the numericInputs
#'@param placeholder placeholder value in the numericInput
#'@param wanted_step step of the value in the numericInput
#'@param id id to add when we want to use the functions more than one time in the app
#'
#'@return return a vector containing the numbers the user selected from 1:10
#'
#'@examples
#'
#'#let's say the user selected those anime in the app
#'selectize <- c("Naruto", "Death Note", "One Piece")
#'
#'#we will use both selectize_names and selectize_count
#'names <- selectize_names(selectize)
#'count <- selectize_count(selectize)
#'
#'#we can then create the inputs
#'create_numeric_input(names, count, id = "example")
#'
#'
#'@export


create_numeric_input <- function(selectednames,
                                 selectedcounts,
                                 min_user = 1,
                                 max_user = 10,
                                 placeholder = 5,
                                 wanted_step = 0.5,
                                 id){

  # Controls
  if (!is.numeric(min_user)) {
    stop("Argument min_user is not valid. It must be a number.")
  }
  if (min_user <= 0) {
    stop("Argument min_user is not valid. It must be positive non null.")
  }
  if (!is.numeric(max_user)) {
    stop("Argument max_user is not valid. It must be a number.")
  }
  if (max_user <= 0) {
    stop("Argument max_user is not valid. It must be positive and non null.")
  }
  if (!is.numeric(placeholder)) {
    stop("Argument placeholder
         is not valid. It must be a number.")
  }
  if (!is.numeric(wanted_step)) {
    stop("Argument wanted_step is not valid. It must be a number.")
  }
  if (wanted_step <= 0) {
    stop("Argument wanted_step is not valid. It must be positive and non null.")
  }
  if (!is.character(selectednames)) {
    stop("Argument selectednames is not valid. It must be a character.")
  }
  if (!is.numeric(selectedcounts)) {
    stop("Argument selectedcounts is not valid. It must be a number.")
  }
  if (selectedcounts <= 0) {
    stop("Argument selectedcounts is not valid. It must be positive and non null.")
  }

  #Function

  count = 1
  box_list <- list()
  for (count in 1:selectedcounts) {
    box_list[[count]] <- numericInput(
      inputId = sprintf("%s_%d",
                        id,
                        count),
      label = sprintf("%s Score",selectednames[count]),
      min = min_user,
      max = max_user,
      value = placeholder,
      step = wanted_step
    )
    count = count + 1
  }
  tagList(box_list)
}


#'@title Recover the weights given in the boxes generated by `create_numeric_input()`
#'
#'@description Recover the weights that the user has put in the boxes generated by `create_numeric_input()`
#'
#'@author Marie Bellier, Massimo Finini, Meri Likoska, Vania Rodrigues Telo Ramos, Xavier Renger
#'
#'@param selectedcounts amount of variable selected in the selectize input
#'@param input input id from shiny
#'@param id id to add when we want to use the functions more than one time in the app
#'
#'@return return a vector containing the numbers the user selected from 1:10
#'
#'@examples
#'
#'#let's say the user selected those anime in the app
#'selectize <- c("Naruto", "Death Note", "One Piece")
#'
#'#we will use both selectize_names and selectize_count
#'names <- selectize_names(selectize)
#'count <- selectize_count(selectize)
#'
#'#we can then create the inputs
#'inputs <- create_numeric_input(names, count, id = "example")
#'
#'#to recover the weight entered by the user we then run
#'\dontrun{
#'x <- score_recovery(count, input, id = "example")
#'}
#'
#'#note that input are the input from shiny server
#'#and that this code cannot be ran outside the
#'#application therefore we do not provide any output here
#'
#'
#'@export



score_recovery <- function(selectedcounts, input, id){
  if (!is.character(id)) {
    stop("Argument id is not valid. It must be characters.")
  }
  if(!is.integer(selectedcounts)){
    stop("Argument selectedcounts is not valid. It must be a positive integer")
  }
  if(selectedcounts <= 0){
    stop("Argument selectedcounts is not valid. It must be a positive integer")
  }

  #Function

  FD=1
  weight_list <- vector()
  for(FD in 1:selectedcounts){
    weight_list[FD] <- input[[sprintf("%s_%d", id, FD)]]
    FD = FD+1
  }
  weight_list
}
ptds2021/project--G5 documentation built on Dec. 22, 2021, 9:59 a.m.