Skip to content

Commit

Permalink
feat: fuctions: adjust_tableau_size, adjust_ft, check_db_names
Browse files Browse the repository at this point in the history
  • Loading branch information
rks263 committed Aug 29, 2023
1 parent 34ba9fb commit 5622781
Show file tree
Hide file tree
Showing 20 changed files with 345 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(adjust_tableau_font_style)
export(adjust_tableau_size)
export(authenticate_server)
export(check_dashboard_names)
export(download_filtered_tableau_image)
export(download_tableau_crosstab_excel)
export(download_tableau_data)
Expand Down
63 changes: 63 additions & 0 deletions R/adjust_tableau_font_style.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#' adjust_tableau_font_style
#'
#' Adjust the font style of a tableau file
#'
#' @param import_files selected tableau file or folder, to change its font style. If
#' it is only a file, make sure it is a twb file.
#' @param font_style the name of the font style you want to use (in quotation marks "")
#' @param save_location The location to which the adjusted tableau file will be
#' saved (this needs to be a .twb file). If empty, the adjusted tableau file will be overwritten.
#'
#' @return tableau file with the correct font style.
#' @export
#'
adjust_tableau_font_style <- function (import_files, font_style = "Tableau Regular", save_location = NULL)
{
# if the path gives a whole map instead of one file, make a file list.
if (tools::file_ext(import_files) != "twb") {
import_files = list.files(import_files, full.names = TRUE)
}

for (file in import_files) {
#read xml file('s)
data <- xml2::read_xml(file)

# Find the font-style part
style_part <- xml2::xml_find_all(data, "//formatted-text//run")

for (type_section in style_part) {
# change the style
xml2::xml_set_attr(type_section, "fontname", font_style)
}

Ans_1 <- readline(prompt = "Do you want to save the adjustments in a new file? y/n: ")

if (substr(Ans_1, 1, 1) == "y") {
Ans_2 <- readline(prompt = "Yes, so did you give a new save file as input? y/n: ")
if (substr(Ans_2, 1, 1) == "y") {
cat("a new save file is given so this file will be used for saving \n")

# check if the save_location file is .twb
if (tools::file_ext(save_location) != "twb") {
stop("save_location file is no .twb file")
}
# update and save the new file
data <- XML::xmlParse(data)
XML::saveXML(doc = data, file = save_location)
}

else {
stop("stop the process, the user wants his file to be saved at onther file,
however no new file as input is given.")
}
}

else {
cat("file can be overwritten \n")

data <- XML::xmlParse(data)
XML::saveXML(doc = data, file = file)

}
}
}
67 changes: 67 additions & 0 deletions R/adjust_tableau_size.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#' adjust_tableau_size
#'
#' Adjust the size of selected tableau file/files.
#'
#' @param import_files selected tableau file or folder, to change its size. If
#' it is only one file, make sure it is a twb file.
#' @param save_location The location to which the adjusted tableau file will be
#' saved (this needs to be a .twb file). If empty, the adjusted tableau file will be overwritten.
#' @param maxheight Max height for tableau file, if NULL use system variables.
#' @param maxwidth Max width for tableau file, if NULL use system variables.
#' @param minheight Min height for tableau file, if NULL use system variables.
#' @param minwidth Min width for tableau file, if NULL use system variables.
#'
#' @return tableau file with the correct size.
#' @export
#' @family xml
adjust_tableau_size <- function(import_files, save_location = NULL, maxheight = NULL,
maxwidth = NULL, minheight = NULL, minwidth = NULL) {
if (tools::file_ext(import_files) != "twb") {
import_files = list.files(import_files, full.names = TRUE)
}

for (file in import_files) {
data <- xml2::read_xml(file)

# Find the size part
style_part <- xml2::xml_find_all(data, "//style ")
size_part <- xml2::xml_find_all(style_part, "//size[@minwidth]")

## adjust the size part
if (is.null(c(maxheight, maxwidth, minheight, minwidth))) {
minheight <- maxheight <- Sys.getenv("TABLEAU_HEIGHT")
minwidth <- maxwidth <- Sys.getenv("TABLEAU_WIDTH")
}
xml2::xml_set_attrs(size_part, c("maxheight" = maxheight, "maxwidth" = maxwidth,
"minheight" = minheight, "minwidth" = minwidth))
## save adjusments
Ans_1 <-
readline(prompt = "Do you want to save the adjustments in a new file? y/n: ")
if (substr(Ans_1, 1, 1) == "y") {
Ans_2 <-
readline(prompt = "Yes, so did you give a new save file as input? y/n: ")
if (substr(Ans_2, 1, 1) == "y") {
cat("a new save file is given so this file will be used for saving \n")
# check if the save_location file is .twb
if (tools::file_ext(save_location) != "twb") {
stop("save_location file is no .twb file")
}
# update and save the new file
data <- XML::xmlParse(data)
XML::saveXML(doc = data, file = save_location)
}
else {
stop(
"stop the process, the user wants his file to be saved at onther file,
however no new file as input is given."
)
}
}

else {
cat("file can be overwritten \n")
data <- XML::xmlParse(data)
XML::saveXML(doc = data, file = file)
}
}
}
66 changes: 66 additions & 0 deletions R/check_dashboard_names.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#' check_dashboard_names
#'
#' Check the names of dashboards in tableau, according to the style guide.
#' The wrong dashboards will be returned together with the reason why it is a
#' wrong name
#'
#' @param import_files selected tableau file or folder,
#' to check the names of the dashboards.
#' If it is only one file, make sure it is a twb file.
#'
#' @return the wrongly named dashboards
#' @export
#' @family xml
check_dashboard_names <- function(import_files) {
if (tools::file_ext(import_files) != "twb") {
import_files = list.files(import_files, full.names = TRUE)
}
for (file in import_files) {
data <- XML::xmlParse(file = file)

## Extract de root node.
rootnode <- XML::xmlRoot(data)

## Find the dashboards part
DB_part <- rootnode[["dashboards"]]

## Find the amount of dashboards
number_of_DB <- length(XML::xmlChildren(DB_part))

## while loop over all DB's to save each dashboards current name
count <- 1
name_list <- list()

while (count <= number_of_DB) {
DB_names <- rootnode[["dashboards"]][[count]]
name_list <- append(name_list, XML::xmlAttrs(DB_names))
count <- count + 1
}

## check all conditions for a correct DB name
names <- 1
while (names <= number_of_DB) {

if (name_list[names] == "Filters" || name_list[names] == "Toelichting") {
## do nothing, it is a correct name, so skip it.
}

else if (name_list[names] == "filters" || name_list[names] == "toelichting") {
print(paste0("Wrong DB name: '", name_list[names], "', not starting with an capital letter"))
}

else if (stringr::str_detect(name_list[names], "^\\d.") == F) {
print(paste0("Wrong DB name: '", name_list[names],"', not starting with a digit and a dot"))
}

else if (stringr::str_detect(name_list[names], "[A-Z]") == F) {
print(paste0("Wrong DB name: '", name_list[names], "', not starting with an capital letter"))
}

else if (grepl("DB", name_list[names]) == T) {
print(paste0("Wrong DB name: '", name_list[names], "', no 'DB' allowed in DB name"))
}
names <- names + 1
}
}
}
27 changes: 27 additions & 0 deletions man/adjust_tableau_font_style.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions man/adjust_tableau_size.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions man/check_dashboard_names.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/get_actions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/get_folders.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/get_hierarchy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/get_nodenames.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/get_parameter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/get_revision.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/get_server_location.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5622781

Please sign in to comment.