Two easy fixes? (I hope!)

This shiny app is a random list generator. The user can upload their data, select the parameter they want to base the random list on, and select a number of rows to be selected before generating the list. The user can then download the list. The data typically used is for companies that have liquor licenses including their address and license type. They usually randomize based on zipcode and for that the app is great! But if they try to randomize for licenses, we get kicked out of the app.

My first problem is that licenses don't randomize. My best guess is that it's because license data is a string (license type D, A, etc), and zip code data is numeric. I don't know how to change the server to accept both data types.

My second problem is that when the system malfunctions (like when trying to use license data) the user gets kicked out and you have to restart R to try it again.

If anyone has any ideas on what to do about these things I am so incredibly grateful!

library(shiny)
library(DT)

ui <- fluidPage(
sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(
        
        # Input: Select a file ----
        fileInput("file1", "Choose CSV File",
                  multiple = FALSE,
                  accept = c("text/csv",
                             "text/comma-separated-values,text/plain",
                             ".csv")),
        
        selectInput("variable", "Choose variable", choices = ""),
        selectInput("subset", "Choose subset", choices = ""),
        numericInput("nSamples", "How many random samples?", value = 1),
        
        # Button
        actionButton("update", "Generate", 
                     class = "btn-primary",style='padding:4px; font-size:120%'),
        
        downloadLink("downloadData", "Download")
        
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
        
        # Output: Data file ----
        DTOutput("contents")
        
    )
)

)

server <- function(input, output, session) {

#Uncomment, Run and delete these two commented lines of code to get sample file to test...
# df = data.frame(ID = 1:10, zip = c(rep(68503, 5), rep(45763, 5)))
# write.csv(df, "testData.csv", row.names = F)

df = reactiveVal()
randomSubset = reactiveVal()

#Change the variables based on the columns in the file
observeEvent(input$file1, {
    df(read.csv(input$file1$datapath))
    updateSelectInput(session, "variable", choices = colnames(df()))
})

#For the selected variable, display all different options
observeEvent(input$variable, {
    updateSelectInput(session, "subset", choices = unique(df()[,input$variable]))
})

#On update, pick n samples from the selected sub category
observeEvent(input$update, {
    randomSubset(df()[eval(parse(text = paste("df()$",input$variable, "==", input$subset, sep = ""))),])
    randomSubset(randomSubset()[sample(1:nrow(randomSubset()), input$nSamples), ])
    output$contents <- renderDT({
        randomSubset()
    })
})

#Download csv when completed
output$downloadData <- downloadHandler(
    filename = function() {
        paste("randomSample.csv", sep="")
    },
    content = function(file) {
        write.csv(randomSubset(), file, row.names = F)
    }
)

}

shinyApp(ui, server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.