R/get_value_dic.R

Defines functions get_value_dic

Documented in get_value_dic

#' outputs a dictionary (excel utf8 csv) of the cases of specified variable of a given tibble
#' this is used with EXCEL for renaming values
#'
#' @param .data a tbl() to output its values
#' @param variable_name a name of variable in the table.
#'  the values of this variable will be exported to dictionary.
#' @param folder_name the folder name as *string* to which the dictionary will be saved in.
#' the default name of the folder is "data"
#' @param name the file name as *string* . please finish the file name with .csv
#' the default name of the file is "value_dic.csv"
#'
#' @return A tbl, with two identical variables valuewize, <variable_name> and new.
#' in addition, the function saves the file in the specified folder
#'
#' @examples
#' school <- tribble(
#'   ~NAME, ~CLASS, ~GRADE,
#'   yossy, first, 70,
#'   dina, second, 90,
#'   reut, third, 100,
#'   dvir, first, 30
#'   )
#'
#' get_value_dic(school,CLASS,folder_name="mydata",name="class_value_dic.csv")
#'
#'
#' @seealso get_var_names_dic, get_diffrent_var_names
#' @importFrom magrittr %>%
#'
#' @export



get_value_dic <- function(.data,variable_name,folder_name="data",name="value_dic.csv"){
  require(tidyverse,quietly = TRUE)

  vn <- enquo(variable_name)

  value_dic <- .data %>%
    select(!!vn) %>%
    distinct() %>%
    #rename(old_values=!!vn) %>%
    mutate(new=!!vn)


  readr::write_excel_csv(value_dic,path = paste0(folder_name,"/",name))

  return(value_dic)

}
sarid-ins/saridr documentation built on Nov. 10, 2020, 9:07 p.m.