Spaces:
Sleeping
Sleeping
Download app.R from stat-utas/persistentdataxls: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/spaces/stat-utas/persistentdataxls/resolve/main/app.R
- Command line
-
hf download hf://spaces/stat-utas/persistentdataxls/app.R
-
curl -L -o app.R https://huggingface.co/spaces/stat-utas/persistentdataxls/resolve/main/app.R
1.53 kB
| library(shiny) | |
| library(openxlsx) | |
| library(shinyjs) | |
| ui <- fluidPage( | |
| useShinyjs(), | |
| titlePanel("Save Input Text to Excel"), | |
| sidebarLayout( | |
| sidebarPanel( | |
| textInput("input_text", "Enter text:", ""), | |
| actionButton("save_btn", "Save to Excel"), | |
| actionButton("clear_btn", "Clear Excel") | |
| ), | |
| mainPanel( | |
| tableOutput("excel_table") | |
| ) | |
| ) | |
| ) | |
| server <- function(input, output, session) { | |
| observeEvent(input$save_btn, { | |
| text <- input$input_text | |
| file <- "/data/input_texts.xlsx" | |
| if (file.exists(file)) { | |
| data <- read.xlsx(file, sheet = 1) | |
| new_row <- data.frame(Text = text, stringsAsFactors = FALSE) | |
| data <- rbind(data, new_row) | |
| write.xlsx(data, file, sheetName = "Sheet1", rowNames = FALSE) | |
| } else { | |
| data <- data.frame(Text = text, stringsAsFactors = FALSE) | |
| write.xlsx(data, file, sheetName = "Sheet1", rowNames = FALSE) | |
| } | |
| updateTableOutput() | |
| }) | |
| observeEvent(input$clear_btn, { | |
| file <- "/data/input_texts.xlsx" | |
| if (file.exists(file)) { | |
| file.remove(file) | |
| } | |
| updateTableOutput() | |
| }) | |
| updateTableOutput <- function() { | |
| file <- "/data/input_texts.xlsx" | |
| if (file.exists(file)) { | |
| data <- read.xlsx(file, sheet = 1) | |
| output$excel_table <- renderTable({ | |
| data | |
| }) | |
| } else { | |
| output$excel_table <- renderTable({ | |
| data.frame(Text = character(), stringsAsFactors = FALSE) | |
| }) | |
| } | |
| } | |
| updateTableOutput() | |
| } | |
| shinyApp(ui, server) | |