|
767 | 767 | } |
768 | 768 |
|
769 | 769 |
|
770 | | -# helper grouping parameters ------------------- |
771 | | - |
772 | | - |
773 | | -.parameter_groups <- function(x, groups) { |
774 | | - # only apply to conditional component for now |
775 | | - if ("Component" %in% colnames(x) && !any(x$Component == "conditional")) { |
776 | | - return(x) |
777 | | - } |
778 | | - if ("Component" %in% colnames(x)) { |
779 | | - row_index <- which(x$Component == "conditional") |
780 | | - } else { |
781 | | - row_index <- seq_len(nrow(x)) |
782 | | - } |
783 | | - |
784 | | - x_other <- x[-row_index, ] |
785 | | - x <- x[row_index, ] |
786 | | - |
787 | | - att <- attributes(x) |
788 | | - indent_rows <- NULL |
789 | | - indent_parameters <- NULL |
790 | | - |
791 | | - if (is.list(groups)) { |
792 | | - # find parameter names and replace by rowindex |
793 | | - group_rows <- lapply(groups, function(i) { |
794 | | - if (is.character(i)) { |
795 | | - i <- match(i, x$Parameter) |
796 | | - } |
797 | | - i |
798 | | - }) |
799 | | - |
800 | | - # validation check - check if all parameter names in the |
801 | | - # group list are spelled correctly |
802 | | - misspelled <- vapply(group_rows, anyNA, TRUE) |
803 | | - |
804 | | - if (any(misspelled)) { |
805 | | - # remove invalid groups |
806 | | - group_rows[misspelled] <- NULL |
807 | | - # tell user |
808 | | - insight::format_alert( |
809 | | - "Couldn't find one or more parameters specified in following groups:", |
810 | | - toString(names(misspelled[misspelled])), |
811 | | - "Maybe you misspelled parameter names?" |
812 | | - ) |
813 | | - } |
814 | | - |
815 | | - |
816 | | - # sort parameters according to grouping |
817 | | - selected_rows <- unlist(group_rows) |
818 | | - indent_parameters <- x$Parameter[selected_rows] |
819 | | - x <- rbind(x[selected_rows, ], x[-selected_rows, ]) |
820 | | - |
821 | | - # set back correct indices |
822 | | - groups <- 1 |
823 | | - for (i in 2:length(group_rows)) { |
824 | | - groups <- c(groups, groups[i - 1] + length(group_rows[[i - 1]])) |
825 | | - } |
826 | | - names(groups) <- names(group_rows) |
827 | | - } else { |
828 | | - # find parameter names and replace by rowindex |
829 | | - group_names <- names(groups) |
830 | | - groups <- match(groups, x$Parameter) |
831 | | - names(groups) <- group_names |
832 | | - |
833 | | - # order groups |
834 | | - groups <- sort(groups, na.last = TRUE) |
835 | | - } |
836 | | - |
837 | | - |
838 | | - empty_row <- x[1, ] |
839 | | - for (i in seq_len(ncol(empty_row))) { |
840 | | - empty_row[[i]] <- NA |
841 | | - } |
842 | | - |
843 | | - for (i in rev(seq_along(groups))) { |
844 | | - x[seq(groups[i] + 1, nrow(x) + 1), ] <- x[seq(groups[i], nrow(x)), ] |
845 | | - x[groups[i], ] <- empty_row |
846 | | - } |
847 | | - |
848 | | - # find row indices of indented parameters |
849 | | - if (!is.null(indent_parameters)) { |
850 | | - indent_rows <- match(indent_parameters, x$Parameter) |
851 | | - } |
852 | | - |
853 | | - # add other rows back |
854 | | - if (nrow(x_other) > 0) { |
855 | | - x <- rbind(x, x_other) |
856 | | - } |
857 | | - |
858 | | - attributes(x) <- utils::modifyList(att, attributes(x)) |
859 | | - attr(x, "indent_rows") <- indent_rows |
860 | | - x |
861 | | -} |
862 | | - |
863 | | - |
864 | 770 | # .insert_row <- function(x, newrow, r) { |
865 | 771 | # existingDF[seq(r+1,nrow(existingDF)+1),] <- existingDF[seq(r,nrow(existingDF)),] |
866 | 772 | # existingDF[r,] <- newrow |
|
0 commit comments