I'd like to plot a histogram of step-data that is show when the App is first run and that bit is working. So is the changing of the bins and the color of the plot density. One of the features I would like to include is the ability for the user to click an actionButton or some other action-toggle to overlay a dnorm-line on the histogram. On or off would be preferable.
However, I'm only able to get the code to draw the overlay line once at this point. I tried using the 'isolate' function from this idea here but couldn't figure out the implementation in my code.
Ideally, the plot would look something like (ignoring finer points of plot control) where the user clicks a "toggle" to add or remove the curve:
w<-rnorm(1000)
hist(w,col="red",freq=F,xlim=c(-5,5))
curve(dnorm,-5,5,add=T,col="blue")
ui.R
library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Data Products - Final Project"),
# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
helpText("Select some of the features of the histogram."),
sliderInput("bins", label = h4("Number of bins: ")
, min = 5
, max = 50
, value = 10),
radioButtons("radio-color", helpText("Select a color for density plot."),
choices = list("Salmon" = "salmon", "Black" = "black"
,"Red" = "red", "Dark Blue" = "darkblue"
, "Dark Grey" = "darkgrey")
,selected = "salmon"),
actionButton("hist-dnorm", label = "Add Curve")
),#end sideBarPanel
# Show a plot of the generated distribution
mainPanel(
h1("Lorem Ipsum", align = "left"),
p("Some paragraph text", align = "left"),
plotOutput("histPlot"),
plotOutput("histLine")
)#End mainPanel
)#End sidebarLayout
)#End fluidPage
)#End ShinyUI
server.R
Hopefully you can see my scratch code for the curve whose elements are currently commented out because I can't figure out how to implement them in the code. The are (m, s, xfit, yfit, yfit2 and hline)
library(shiny)
library(caret)
library(ggplot2)
library(dplyr)
library(lubridate)
dat <- read.csv("data/fitbit_data.csv", stringsAsFactors = FALSE)
dat$Day <- weekdays(x = as.Date(dat$Date, "%m/%d/%Y", label = TRUE, abbr = FALSE))
dat$Steps <- as.numeric(sub(",","",dat$Steps))
dat$Steps[dat$Steps == 0 & is.numeric(dat$Steps)] <- NA
dat$Calories.Burned <- as.numeric(sub(",","",dat$Calories.Burned))
dat$Calories.Burned[dat$Calories.Burned == 0
& is.numeric(dat$Calories.Burned)] <- NA
dat$Minutes.Sedentary <- as.numeric(sub(",","",dat$Minutes.Sedentary))
dat$Minutes.Sedentary[dat$Minutes.Sedentary == 0
& is.numeric(dat$Minutes.Sedentary)] <- NA
dat$Activity.Calories <- as.numeric(sub(",","",dat$Activity.Calories))
dat$Activity.Calories[dat$Activity.Calories == 0
& is.numeric(dat$Activity.Calories)] <- NA
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
output$histPlot <- renderPlot({
steps <- dat$Steps
bins <- seq(min(steps, na.rm = TRUE), max(steps, na.rm = TRUE)
, length.out = input$bins + 1)
h <- hist(dat$Steps, breaks = bins, density = 10, col = input$`radio-color`
, xlim = c(500, 25000)
, xlab = "# of Steps"
, ylab = "Frequency"
, main = "Histogram of Steps")
isolate(
output$histLine <- renderPlot({
m <- mean(dat$Steps, na.rm = TRUE)
s <- sqrt(var(dat$Steps, na.rm = TRUE))
xfit <- seq(min(dat$Steps, na.rm = TRUE)
, max(dat$Steps, na.rm = TRUE), length = 40)
yfit <- dnorm(xfit, mean = m, sd = s)
yfit2 <- yfit*diff(h$mids[1:2])*length(dat$Steps)
lines(xfit, yfit2, col = "darkblue", lwd = 2)
})
)
})
#end isolate
})#end shinyServer
Questions
- How do I implement a toggle in ui.R that allows the user to toggle the overlay of the dnorm curve calculated in server.R (on or off; off by default)?
- All of the subsetting of the data in the first few lines of code do you have any recommendations for a better less messy to clean that up?
Thanks for your time and consideration
actionButton
increase by one every time you click it, so probably you can check whether the number is even or odd and decide whether you want to plot the line or not. – Xiongbing Jin