I have tried show and visible but can not get the data frame to render in the widget. I have verified that the there is data in the table via the print statement. I am unable to change the visibility of the container to render the table. I am getting the following error message:
Default visible <- method is not implemented
library(openxlsx2)
library(gWidgets2)
library(gWidgets2tcltk)
library(dplyr)
# Initialize global variables
file_path <- NULL
wb <- NULL
combined_data <- list()
selected_table_data <- NULL
selected_table_name <- NULL # To store the selected table name
# Get Sheet and table names and named ranges from Excel workbook
file_path <- file.choose()
wb <- openxlsx2::wb_load(file_path)
sheet_names <- openxlsx2::wb_get_sheet_names(wb)
sheet_dfs <- lapply(sheet_names, function(x) openxlsx2::wb_get_tables(wb, sheet = x))
combined_data <- bind_rows(sheet_dfs, .id = "sheet_name")
# Create the main window
w <- gwindow("Select a Table to Export to CSV", visible = FALSE)
# Create a label for reporting period selection
glabel("Select Reporting Period:", container = w)
# Create a main box container for controls within the main window
main_controls_box <- ggroup(horizontal = FALSE, container = w)
# Create a sub box container for the table selection controls
table_controls_box <- ggroup(horizontal = FALSE, container = main_controls_box)
# Create a gcombobox for table selection
tab_name <- gcombobox(
items = sort(unique(combined_data$tab_name)),
cont = table_controls_box,
label = "Table Name",
expand = TRUE,
selected = 1
)
# Add a handler for gcombobox selection changes
addHandlerChanged(tab_name, handler = function(h, ...) {
selected_table_name <<- svalue(tab_name) # Store the selected table name
# Show or hide the gdf based on selection
if (!is.null(selected_table_name)) {
visible(selected_table) <- TRUE
} else {
visible(selected_table) <- FALSE
}
})
# Create a sub box container for the calendar picker
calendar_controls_box <- ggroup(horizontal = FALSE, container = main_controls_box)
# Create a date input widget for reporting period using GCalendar
reporting_period <- gcalendar(
text = "",
format = "%Y-%m-%d",
container = calendar_controls_box
)
# Create a "Load" button
load_table_btn <- gbutton("Load", cont = main_controls_box, handler = function(h, ...) {
load_selected_table()
})
# Create a gdf (gDataframe) to display the selected table
selected_table <- gdf(
cont = main_controls_box,
expand = TRUE,
text = "",
widgetOptions = list(
header = TRUE
),
visible = TRUE # Render the gdf widget by default
)
# Function to look up and load the selected table
load_selected_table <- function() {
if (is.null(selected_table_name)) {
gmessage("Please select a table first.")
return()
}
selected_row <- which(combined_data$tab_name == selected_table_name)
selected_sheet_name <- combined_data$sheet_name[selected_row]
selected_table_ref <- combined_data$tab_ref[selected_row]
# Use selected_sheet_name and selected_table_ref to read the table
selected_table_data <<- openxlsx2::wb_to_df(wb, sheet = selected_sheet_name, dims = selected_table_ref, col_names = TRUE)
# Update the gdf to display the selected table
svalue(selected_table) <- selected_table_data # Assign the data
# Show the selected_table
visible(selected_table) <- TRUE
}
# Make the main window visible
visible(w) <- TRUE
# Run the GUI event loop
while (TRUE) {
if (!validObject(w)) { # Check if the gwindow is valid
break
}
Sys.sleep(0.1)
}