Skip to content

Instantly share code, notes, and snippets.

@thedivtagguy
Last active February 12, 2021 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thedivtagguy/7e5bf127dffa447bcb6dc01526183b3e to your computer and use it in GitHub Desktop.
Save thedivtagguy/7e5bf127dffa447bcb6dc01526183b3e to your computer and use it in GitHub Desktop.
library(shiny)
ui <- fluidPage(
titlePanel("Srishti Library Database"),
# Sidebar layout with input and output definitions ----
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select a file ----
fileInput("file1", "Choose Library File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
# Copy the line below to make a checkbox
checkboxInput("checkbox", label = "Or Use default file", value = FALSE),
# Horizontal line ----
tags$hr(),
numericInput("skiplines", "Number of Lines to Skip:", 10, min = 1, max = 100),
# Horizontal line ----
tags$hr(),
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Data file ----
dataTableOutput('myTable')
)
)
# Define server logic to read selected file ----
server <- function(input, output) {
output$myTable <- renderDataTable({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, head of that data file by default,
# or all rows if selected, will be shown.
if(input$checkbox == FALSE){
req(input$file1)
data <- readr::read_fwf(input$file1$datapath,
fwf_empty(input$file1$datapath),
skip = input$skiplines
)}
else {
data <- readr::read_fwf("https://raw.githubusercontent.com/thedivtagguy/srishtilibrary/main/books_raw.txt", fwf_empty("https://raw.githubusercontent.com/thedivtagguy/srishtilibrary/main/books_raw.txt", col_names = c("Title")),skip = 10
)
}
data <- data %>% rename(Title = X1),
return(data)
}, options = list( extensions = 'Buttons', buttons =
list("copy", list(
extend = "collection"
, buttons = c("csv", "excel", "pdf")
, text = "Download")),pageLength = 10, info = FALSE, searchHighlight = TRUE))
}
# Run the app ----
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment