Spaces:
Sleeping
Sleeping
File size: 1,529 Bytes
fd61fc3 4d75452 fd61fc3 4d75452 fd61fc3 4d75452 fd61fc3 4d75452 b2d115d 4d75452 fd61fc3 4d75452 b2d115d 4d75452 b2d115d 4d75452 fd61fc3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 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)
|