wfu-dmds / teaching-r-study

3 stars 2 forks source link

First draft of demographic survey code #3

Closed jdtrat closed 3 years ago

jdtrat commented 3 years ago

In response to #2, I made the demographic_sample.Rmd within the jdtrat_additions directory. If you knit it, you will see my thoughts for creating demographic input. Please see the actual code for how I implemented it. I spent some time trying to automate the process, but am running into some problems with rlang and would appreciate your thoughts @LucyMcGowan!

LucyMcGowan commented 3 years ago

Here is the code we went through today:

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

df <- readr::read_csv("~/downloads/data.csv")
library(tidyverse)

f <- function(df) {
    df %>%
        distinct(question, input_type) %>%
        count(input_type)
}
get_demo_questions <- function(df) {
    if (df$input_type == "numeric") {
       out <- numericInput(df$label, df$question, value = 0)
    }
    if (df$input_type == "select") {
       out <- selectInput(df$label[1], df$question[1], choices = df$option[1])
    }
    out
}

get_demo_questions(df[1,])

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            list(get_demo_questions(df[1,]),
            get_demo_questions(df[2,])
            )
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
}

# Run the application 
shinyApp(ui = ui, server = server)
jdtrat commented 3 years ago

Thanks! I added demographic_sample2.Rmd for your review. The chunk titled "make functional" has a function getUICode, which is a crude implementation of what we discussed. If you knit the .Rmd, you'll see how it works!

Two questions:

  1. Do you think that's okay to move forward with?
  2. Do you have suggestions as to how I can save the data out when hitting the "submit" button? I currently use this code, but -- as the comment suggests--I'm not sure how to functionalism that.
# The input$first_language is hard coded. I'm not sure how to change that.
# It should be something like `eval(unique(df$label))`
    formData <- reactive({
      data <- tibble::tribble(~userID, ~language,
                      input$userID, input$first_language)
      data
    })