Skip to content

Instantly share code, notes, and snippets.

@PaulC91
Created January 27, 2018 13:10
Show Gist options
  • Save PaulC91/922f8472f920d2b139baf9d81893e4eb to your computer and use it in GitHub Desktop.
Save PaulC91/922f8472f920d2b139baf9d81893e4eb to your computer and use it in GitHub Desktop.
dynamic data transforms + input options in shiny for Gokhan
library(shinydashboard)
library(dplyr)
ui <- navbarPage("test",
tabPanel("dataBuilder", icon = icon("database"),
fluidRow(
box(width = 3, title = "Data Upload", solidHeader = TRUE, status = "primary",
helpText("Choose CSV File (Max size 10 MB)"),
fileInput("file1", "",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"))
),
box(width = 2, title = "Outcome", solidHeader = T,
status = "primary",
helpText("Select a variable to predict"),
selectizeInput("yvar", label = "", choices = character(0))
),
box(width = 2, title = "Duration", solidHeader = T,
status = "primary",
helpText("Select the duration variable"),
selectizeInput("tvar", label = "", choices = character(0)),
checkboxInput("splines", "Include cubic splines?", FALSE)),
box(width = 5, title = "Predictors", solidHeader = T, status = "primary",
helpText("Select the independent variables"),
selectizeInput("xvar", label = "", choices = character(0), multiple = T,
width = "500px")
)),
column(width = 12, DT::dataTableOutput("table"))
))
server <- shinyServer(function(input, output, session) {
rawdata <- reactive({
req(input$file1)
inFile <- input$file1
df <- read.csv(inFile$datapath)
})
observe({
updateSelectizeInput(session, "yvar", choices = names(rawdata()), selected = names(rawdata())[1])
})
observe({
nums <- colnames(select_if(rawdata(), "is.numeric"))
ints <- colnames(select_if(rawdata(), "is.integer"))
digits <- c(nums, ints)
updateSelectizeInput(session, "tvar", choices = digits)
})
data_transform <- reactive({
req(input$tvar)
if(input$splines) {
rawdata() %>%
mutate(square = (!! rlang::sym(input$tvar))^2,
cube = (!! rlang::sym(input$tvar))^3)
} else {
rawdata()
}
})
observe({
nms <- names(data_transform())[names(data_transform()) != input$yvar]
updateSelectizeInput(session, "xvar", choices = nms)
})
output$table <- DT::renderDataTable({
DT::datatable(data_transform())
})
})
shinyApp(ui = ui, server = server)
@KrisRaj4
Copy link

What does the code do after we select the variables, I do not see any results displayed! Please suggest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment