stat-utas's picture
Update app.R
b2d115d verified
Raw History Blame Contribute Delete
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)