I am plotting a graph using the Plot_ly package in R Shiny. Right now I am creating a graph with many lines on it and I would like to know if it is possible for the user to toggle the lines on and off using the checkbox input.
Here is a sample of my server side code:
output$site_filter <- renderUI({
selectInput("site_filter", "Sites"
sort(unique(site_list$sites), decreasing = FALSE))
})
output$plots <- renderPlotly({
forecast_detail <- forecast[forecast$site == input$site_filter,]
actual_detail <- actual[actual$site == input$site_filter,]
p <- plot_ly() %>%
add_lines(x = forecast_detail$date, y = forecast_detail$total,
name = 'Forecast', line = list(color = 'purple')) %>%
add_lines(x = actual_detail$date, y = actual_detail$total,
name = 'Actual', line = list(color = 'blue'))
})
For my ui side, I created the checkbox like this:
fluidRow(checkboxInput("Actuals", "Actual Line", value = TRUE))
Is there a way I could use this checkbox input to toggle the actual lines on and off? I've been trying to use an if statement before the add_lines command but I get an error that states it is not logical.
You can store the first group of lines and add the second group based on a condition triggered by your checkbox. It is hard to come up with a working solution without a reproducible example but something like this should do the job:
output$plots <- renderPlotly({
forecast_detail <- forecast[forecast$site == input$site_filter,]
actual_detail <- actual[actual$site == input$site_filter,]
p <- plot_ly() %>%
add_lines(
x = forecast_detail$date,
y = forecast_detail$total,
name = 'Forecast',
line = list(color = 'purple')
)
if(!is.null(input$Actuals)) {
p <- p %>%
add_lines(
x = actual_detail$date,
y = actual_detail$total,
name = 'Actual',
line = list(color = 'blue')
)
}
return(p)
})
Related
I'm using flexdashboard and shiny to choose which variable to plot:
varSelectInput("button_var_fir"
, "Select first num variable"
, data = df_scat,
multiple = FALSE
)
ggplot(df_scat, aes(x = !!input$button_var_fir, y = Gen_type, fill = stat(x))) +
geom_point(size= 3, alpha = .075)
it works fine, so far. My problem is, that I would like to subset the data e.g via
df$variable > 0
ggplot(df_scat, aes(x = df$!!input$button_var_fir > 0, y = Gen_type, fill = stat(x))) +
geom_point(size= 3, alpha = .075)
but this doesn't work due to the $!!. How can I solve this?
In {ggplot2}, which uses tidy evaluation, you can use the .data pronoun to dynamically select variables. It's nicely explained outside the {shiny} context in this answer too. This doesn't apply in {plotly} so you can either select with x = data[[input$column]] or x = get(input$column). This is also explained in this question.
Here's a small example to demonstrate how to do this for each plotting function.
library(plotly)
library(tidyverse)
library(shiny)
nbins <- 10
ui <- fluidPage(titlePanel("Dynamic Variable Selection"),
sidebarLayout(sidebarPanel(
selectInput(
inputId = "y1",
label = "Select variable",
choices = names(mtcars))),
mainPanel(plotOutput(outputId = "ggplot"),
plotlyOutput(outputId = "plotly"))))
server <- function(input, output) {
# dynamically pull variable in ggplot
output$ggplot <- renderPlot({
mtcars %>%
ggplot(aes(x = .data[[input$y1]])) +
geom_histogram(bins = nbins) +
ggtitle("ggplot")})
# dynamically pull variable in plotly
output$plotly <- renderPlotly({
mtcars %>%
plot_ly(x = .[[input$y1]], type = "histogram", nbinsx = nbins) %>%
layout(title = list(text = "Plotly"),
xaxis = list(title = input$y1))
})
}
shinyApp(ui = ui, server = server)
Maybe what you want is
df[[input$button_var_fir]] > 0
instead of df$!!input$button_var_fir > 0.
Addition:
You want to subset the data that goes into the plot, right? What I would actually do is subsetting the dataframe itself before it goes into the plot function. When you use the tidyverse this could be what you want:
df_scat %>%
filter(!!input$button_var_fir > 0) %>%
ggplot(aes(x = !!input$button_var_fir, y = Gen_type, fill = stat(x))) +
geom_point(size= 3, alpha = .075)
I am trying to create a line graph with two y-axises. The x-axis is the date and both of the y-axises are continuous data. I have working code to do this. It works perfectly, however when I push that to my shiny server (on Ubuntu) I get an error saying that 'x' must be a list. Not sure why this works locally but not on my shiny server.
server.R
dataset <- reactive({
infile <- input$datafile
if (is.null(infile)) {
return(NULL)
}
else {read_excel(infile$datapath)}
})
output$plot_data <- renderPlotly({
# Bring in the data
data <- subset(dataset(), select = c(input$date, input$var1, input$var2))
date <- data[[input$date]]
y_var1 <- data[[input$var1]]
y_var2 <- data[[input$var2]]
y1 <- list(tickfont = list(color = "blue"),
side = "left",
title = input$var1
)
y2 <- list(tickfont = list(color = "green"),
overlaying = "y",
side = "right",
title = input$var2
)
plot <- plot_ly() %>%
add_lines(x = date,
y = y_var1,
name = input$var1,
line = list(color = "blue")) %>%
add_lines(x = date,
y = y_var2,
name = input$var2,
yaxis = "y2",
line = list(color = "green")) %>%
layout(title = "Data Over Time",
yaxis = y1,
yaxis2 = y2
)
plot
ui.R
plotlyOutput('plot_data', height = 500)
Here is some sample data that has a date column and two continuous columns.
Date Impressions Sessions
01/01/2019 34124114 11234323
01/02/2019 43523523 12341244
01/03/2019 56547634 11124324
01/04/2019 65756844 12341234
01/05/2019 32454355 11412432
01/06/2019 23543664 12342412
01/07/2019 23534262 12341244
01/08/2019 12341324 12341234
01/09/2019 34645623 23412341
01/10/2019 64364363 12342123
01/11/2019 24114124 13412342
01/12/2019 23411242 13423442
01/13/2019 24124124 11234242
01/14/2019 42141132 12342144
anybody can help? trying to create plotly scatter chart where I can change color of selected markers. I checked the dataframe source and indt$active is a num, not factor, yet plotly chart interprets it as a factor
see the code, everything is fine until i set color = ~active. I tried using inside the reactive indt$active <- as.numeric(as.character( indt$active )) but it still does nothing. I am struggling to find what factor the chart fails on. It fails to load (you can comment out the color=~active to see without the error). To select values, you need to draw a box
library(plotly)
library(shiny)
library(dplyr)
library(tidyr)
tms<-format(seq(as.POSIXct("2013-01-01 00:00:00", tz="GMT"),
length.out=48, by='30 min'), '%H:%M')
dts<-c( "Day Before BH","BH","Sunday","Saturday","Friday","Thursday","Wednesday","Tuesday", "Monday" )
indt <- as.data.frame(matrix( c(0), nrow=9, ncol=48, byrow = TRUE))
indt<-cbind(dts,indt)
colnames(indt) <- c("dt",tms)
indt<-gather(indt,tm,active,-dt)
pal <- c("red", "blue")
pal <- setNames(pal, c(0, 1))
ui <- fluidPage(
plotlyOutput("plot")
)
server <- function(input, output, session) {
ind<-reactive({
d <- event_data("plotly_selected")
indt[d$pointNumber+1,3]<-1
indt<<-indt
return(indt)
})
output$plot <- renderPlotly({
f1 <- list(size = 8)
yform <- list(title = "",
categoryorder = "array",
categoryarray = dts
,tickfont = f1)
plot_ly(ind()
, x = ~tm, y = ~dt, mode = "markers", type = "scatter",
color = ~active,
colors = pal,
marker = list(size = 20)
) %>%
layout(dragmode = "select") %>%
layout(xaxis = list(title = "", tickfont = f1),
yaxis = yform)
})
}
shinyApp(ui, server)
In your plot_ly function try using color = ~as.character(active)instead of color = ~active
I have a simple shiny app like below and you can run it. The plots are created by ggvis and user can choose student name from inputSelect. In the plots, I want to change the color of background in specific score range. For example, in each plot, the color of plot background for the score higher than 80 or lower than 50 are highlighted with blue(See picture attached). I was trying to add layers and draw rectangles onto plot using layer_rects(), but the problem is the values of x-axis are changed if different students are chosen.Anyone did this before or any ideas? And is it possible if I want only the points in that score range pop up? Thanks a lot!
library(shiny)
library(ggvis)
df <- data.frame(Student = c("a","a","a","a","a","b","b","b","b","b","c","c","c","c"),
year = c(seq(2001,2005,1),seq(2010,2014,1),seq(2012,2015,1)),
score = runif(14,min = 50,max = 100), stringsAsFactors=F)
ui = (fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("stu","Choose Student",
choice = unique(df$Student))
),
mainPanel(ggvisOutput("plot"))
)
)
)
server = function(input,output,session){
dataInput = reactive({
gg = df[which(df$Student == input$stu),]
})
vis = reactive({
data = dataInput()
data %>%
ggvis(x = ~year, y = ~score) %>%
scale_numeric("y",domain = c(40,120))%>%
layer_lines()
})
vis %>% bind_shiny("plot")
}
runApp(list(ui = ui, server = server))
To have the width of the rectangles to change with the x-axis variable, you can use x = ~min(year) and x2 = ~max(year). I'm not sure how to make the variables dependent on the current scale limits, which seems like it would be a nicer solution. But, this should work. For example, the lower rectangle would be
layer_rects(x = ~min(year), x2 =~max(year),
y = 40-3.5, y2 = 50, opacity := 0.05, fill := "blue")
It isn't vectorized for different limits (at least it didn't look to be), so you can write a function to simplify having multiple rectangles. The whole server would look like
shinyServer(function(input,output,session){
dataInput = reactive({
gg = df[which(df$Student == input$stu),]
})
buffer <- 3.5 # set to make the rectangle reach the scale boundaries
rectLims <- list(lower=c(40-buffer, 80), upper=c(50, 120+buffer))
make_rect <- function(vis, lims, buffer=buffer) {
for (i in seq_along(lims$lower))
vis <- layer_rects(vis, x = ~min(year), x2 =~max(year),
y = rectLims$lower[i], y2 = rectLims$upper[i],
opacity := 0.05, fill := "blue")
vis
}
vis = reactive({
data = dataInput()
data %>%
ggvis(x = ~year, y = ~score) %>%
scale_numeric("y",domain = c(40,120)) %>%
layer_points()%>%
layer_lines() %>%
make_rect(lims=rectLims)
})
vis %>% bind_shiny("plot")
})
For your second question, if you only want points to show up in that range, you can make a subset of the data to use for the layer_points or a logical vector (converted to numeric with +) and use that as the opacity argument,
vis = reactive({
data = dataInput()
## Option 1: and use layer_points(data=inrange)
## inrange <- with(dataInput(), dataInput()[score >=80 | score <= 50,])
## Options 2, with opacity=~inrange
inrange = +with(data, score >=80 | score <= 50)
data %>%
ggvis(x = ~year, y = ~score) %>%
scale_numeric("y",domain = c(40,120)) %>%
layer_points(opacity = ~inrange) %>%
layer_lines() %>%
make_rect(lims=rectLims)
})
I have the following code on my Server. R
data_agg_plot1<- reactive({
brush1 <- linked_brush(keys = data_agg()$id, "navy" )
data_agg <- data_agg()
plot1<-data_agg%>%
ggvis(x = ~dates_all) %>%
group_by(factor(dates_all.1)) %>%
layer_points(y = ~ value, fill =~dates_all.1, shape =~dates_all.1) %>%
layer_paths(y = ~ value, stroke = ~dates_all.1 , strokeOpacity := 0.5) %>%
scale_ordinal("fill", range = c("green", "red", "blue"))%>%
scale_ordinal("shape", range = c("triangle-up","triangle-down","circle")) %>%
scale_ordinal("stroke",range=c("green","red","blue")) %>%
brush1$input() %>%
hide_legend(c('stroke','fill'))%>%
add_legend(c('shape','fill'),
title = "Symbol", orient = "left",
values = c("New hires", "Attrition" , "Net Growth"),
properties = legend_props(
title = list(fontSize = 16))) %>%
add_axis("x",properties= axis_props(labels = list(angle=60,align = "left")),
tick_padding =0,
title = "") %>%
add_axis("y", title = "Total Count") %>%
set_options(width = "auto",height = 400) %>%
scale_numeric('y',clamp = TRUE)
return(list(plot1,brush1))
})
so this is a reactive function that returns me a list of 2 functions, a plot and my brush object.
the purpose of doing so is so that I can make my keys reactive - this is so that I can make an additional plot based on my user's selection. think of it as the second plot depends on what the first user highlights in the first plot.
this is my following code:
plot1_data<-reactive({
data_agg_plot1()[[1]]
})
plot1_data%>%bind_shiny("plot1")
selected_plot1 <- reactive({
data_agg_plot1()[[2]]
})
output$test <- renderPrint({
temp <- selected_plot1()$selected()
print(temp)
})
however, when I print out the selection, it is all false,
please refer to the image below:
can anybody explain to me how to overcome this?
I highly suspect I have to re-write my linkedbrush function,
I have tried both solutions from:
linked_brush in ggvis cannot work in Shiny when data change
but it does not work.