EDITED to include full UI and sample data
I did read the other StackOverflow qs on this issue, but none seemed to address the cause of my error.
When the app loads, I get "error object [name of district I've selected] not found" for the District (inputID = "d"). I know it must be an issue with the subsetting reactive in the server, but I've tried everything (loading the data in the server, removing the vector from the filter function, changing the data type of the variables).
I also took this code from another Shiny App I built, which works. I can't see any differences between the two, besides that one is geom_point() and this is geom_col() so again, not sure what is going on.
Thanks!
Sample data:
sample <- sample_n(pop, 10)
dput(sample)
structure(list(GazID = c(NA, NA, "13872", NA, "13610", "13985",
"13984", "13434", "13428", "13631"), Province = c("Niolandskaia",
"Kaluzhskaia", "Iaroslavskaia", "Vyborgskaia", "Moskovskaia",
"Volynskaia", "Volynskaia", "Orenburgskaia", "Orenburgskaia",
"Arkhangel'skaia"), District = c(NA, "Suhinichinbezuezdniigorod",
"Romanov", NA, "Zvenigorod", "Kovel", "Lutsk", "Ufa", "Orenburg",
"Mezen"), TotalPop = c(NA, NA, 104104, NA, 71746, 103381, 102779,
93145, 62740, 26796), Male = c(NA, NA, 48604, NA, 36948, 52266,
50393, 46403, 32617, 13078), Female = c(NA, NA, 55500, NA, 34798,
51115, 52386, 46742, 30123, 13718), City = c(NA, 5552, NA, NA,
1253, 4254, 5552, 6682, 9533, NA), Rural = c(NA, NA, NA, NA,
70493, 99127, 97228, 86483, 53207, NA)), row.names = c(NA, -10L
), class = c("tbl_df", "tbl", "data.frame"))
Above the UI:
library(tidyverse)
library(readr)
library(shiny)
library(stringr)
library(rebus)
pop <- read_csv("pop.csv")
pop$TotalPop <- str_replace_all(pop$TotalPop, pattern = fixed(","), replacement = "")
pop$Male <- str_replace_all(pop$Male, pattern = fixed(","), replacement = "")
pop$Female <- str_replace_all(pop$Female, pattern = fixed(","), replacement = "")
pop$City <- str_replace_all(pop$City, pattern = fixed(","), replacement = "")
pop$Rural <- str_replace_all(pop$Rural, pattern = fixed(","), replacement = "")
pop$District <- str_remove_all(pop$District, pattern = "[^[:alnum:]]")
pop$TotalPop <- as.numeric(pop$TotalPop)
pop$Male <- as.numeric(pop$Male)
pop$Female <- as.numeric(pop$Female)
pop$City <- as.numeric(pop$City)
pop$Rural <- as.numeric(pop$Rural)
pop$GazID <- as.character(pop$GazID)
pop$District <- str_trim(pop$District)
The UI:
ui <- fluidPage(
titlePanel("Population Data from VSO"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "y", #internal label
label = "Population to map", #label that user sees
choices = c("Total population" = "TotalPop",
"Male population" = "Male",
"Female population" = "Female",
"Urban population" = "City",
"Rural population" = "Rural"),
selected = "TotalPop"),
selectizeInput(inputId = "d",
label = "Select district",
choices = c(pop$District),
multiple = TRUE, # can choose multiple
options = list(maxItems = 5))),
mainPanel(
plotOutput("plot")
)
)
)
The server:
server <- function(input, output) {
pop_subset <- reactive({
req(input$d)
filter(pop, District %in% c(input$d)
)})
output$plot <- renderPlot({
ggplot(data = pop_subset(), aes_string(x = pop_subset()$District, y = input$y)) +
geom_col(aes(fill = pop_subset()$District)) +
labs(x = "District", y = "Population") +
scale_fill_discrete(name = "Districts")
})}
shinyApp(ui = ui, server = server)
The problem is that you are using aes_string in your ggplot, but trying to pass District without quotes. I realize you need aes_string because you are using input$y, so just change your plot call to
output$plot <- renderPlot({
req(pop_subset())
ggplot(data = pop_subset(), aes_string(x = "District", y = input$y)) +
geom_col(aes(fill = District)) +
labs(x = "District", y = "Population") +
scale_fill_discrete(name = "Districts")
})
For reproducibility, packages and some sample data (no idea of its true representative nature, doesn't really matter I think).
library(dplyr)
library(shiny)
library(ggplot2)
set.seed(42)
n <- 50
pop <- data_frame(
TotalPop = sample(1e4, size=n, replace=TRUE)
) %>%
mutate(
Male = pmax(0, TotalPop - sample(1e4, size=n, replace=TRUE)),
Female = TotalPop - Male,
City = sample(LETTERS, size=n, replace=TRUE),
District = sample(letters, size=n, replace=TRUE)
)
Related
I have a shiny app that pulls in NBA statistics from a database and makes a scatterplot. The app.R file code is below. You need to install nbaplotR from github devtools::install_github("abresler/nbastatR") along with nbaplotR if (!require("pak")) install.packages("pak") pak::pak("mrcaseb/nbaplotR"). This app works when I run it locally but not on shinyapps.io when I try to deploy it. In shinyapps.io neither the plot or the table show up. I think the shinyapps.io server is not properly connecting with or installing the above mentioned packages. Any help would be greatly appreciated!
library(devtools)
library(shiny)
library(ggplot2)
library(nbastatR)
library(tidyverse)
library(nbaplotR)
library(nbapalettes)
library(forcats)
library(ggpubr)
library(DT)
library(ggpath)
ui <- fluidPage(
titlePanel("NBA team stats"),
sidebarLayout(
sidebarPanel(
selectInput("x", "X-axis stat",
choices = c("gp", "pctWins", "fgm", "fga", "pctFG",
"fg3m", "fg3a", "pctFG3", "pctFT",
"gpRank", "pctWinsRank", "minutesRank", "fgmRank",
"fgaRank", "pctFGRank", "fg3mRank", "fg3aRank",
"pctFG3Rank", "pctFTRank", "fg2m", "fg2a",
"pctFG2", "wins", "losses", "minutes", "ftm",
"fta", "oreb", "dreb", "treb", "ast", "tov", "stl",
"blk", "blka", "pf", "pfd", "pts", "plusminus",
"winsRank", "lossesRank", "rankFTM", "rankFTA",
"orebRank", "drebRank", "trebRank", "astRank",
"tovRank", "stlRank", "blkRank", "blkaRank", "pfRank",
"pfdRank", "ptsRank", "plusminusRank", "Name_abbreviation"),
selected = "fgm",
multiple = FALSE
),
selectInput("y", "Y-axis stat",
choices = c("gp", "pctWins", "fgm", "fga", "pctFG",
"fg3m", "fg3a", "pctFG3", "pctFT",
"gpRank", "pctWinsRank", "minutesRank", "fgmRank",
"fgaRank", "pctFGRank", "fg3mRank", "fg3aRank",
"pctFG3Rank", "pctFTRank", "fg2m", "fg2a",
"pctFG2", "wins", "losses", "minutes", "ftm",
"fta", "oreb", "dreb", "treb", "ast", "tov", "stl",
"blk", "blka", "pf", "pfd", "pts", "plusminus",
"winsRank", "lossesRank", "rankFTM", "rankFTA",
"orebRank", "drebRank", "trebRank", "astRank",
"tovRank", "stlRank", "blkRank", "blkaRank", "pfRank",
"pfdRank", "ptsRank", "plusminusRank"),
selected = "pctFG",
multiple = FALSE
),
),
mainPanel(
plotOutput("logoscatter"),
DT::DTOutput("Table")
)
)
)
server <- function(input, output) {
#Getting team logos for the plots
Names_abbrev <- valid_team_names()
Names_abbrev[2] <- Names_abbrev[3]
Names_abbrev[3] <- "BKN"
Names_abbrev[26] <- "SAC"
Names_abbrev[27] <- "SA"
Sys.setenv(VROOM_CONNECTION_SIZE=500072)
team_stats_general <- unique(nbastatR::teams_players_stats(seasons = 2023,
types = "team",
tables = "general"))
team_stats_df <- as.data.frame(team_stats_general[[7]])
team_stats_df$Name_abbreviation <- Names_abbrev
team_stats_df$Name_abbreviation <- as.factor(team_stats_df$Name_abbreviation)
team_stats_df <- team_stats_df[,c(10, 12:ncol(team_stats_df))]
output$logoscatter <- renderPlot({
req(input$x, input$y)
plot_scale_x <- if (input$x %in% c("pctWins", "pctFG", "pctFG3", "pctFT", "pctFG2")){
scale_x_continuous(labels = scales::percent_format(accuracy = 1))
}else{
scale_x_continuous()
}
plot_scale_y <- if (input$y %in% c("pctWins", "pctFG", "pctFG3", "pctFT", "pctFG2")){
scale_y_continuous(labels = scales::percent_format(accuracy = 1))
}else{
scale_y_continuous()
}
p1 <- ggplot(data = team_stats_df)+
geom_smooth(aes_string(x = input$x, y = input$y),
method = "lm", se = F, color = "black", linetype = "dashed")+
geom_nba_logos(aes_string(x = input$x, y = input$y, team_abbr = "Name_abbreviation"),
width = 0.075, height = 0.075)+
stat_cor(aes_string(x = input$x, y = input$y, label="..rr.label.."),
label.x.npc = 0.85, label.y.npc = 0.02, size = 6)+
plot_scale_x+
plot_scale_y+
ylab(input$y)+
xlab(input$x)+
theme_bw()+
theme(plot.title = element_text(hjust = 0.5),
text = element_text(size = 18))
p1
})
output$Table <- renderDT({
team_stats_df
})
}
shinyApp(ui = ui, server = server)
I'm fairly new to r and shiny, so bear with me - I have created a plot which shows the accumulated weekly distance covered by players in a sports team, where the user can select the player and the week range. Each player has an individual target distance they should meet and I want the data points in the plot to be green if they have met the target and red if they have not.
The data for weekly distance and target distance are located in different data frames (and they need to be) so I need that when a player is selected in selectInput(), the weekly distance is pulled from the first data frame and the target for the same player is pulled from the second data frame and used for conditional formatting.
EDIT - This is the gps2 data frame (though the PlayerName column lists the actual name which I've changed to initials here):
structure(list(Week = c(14, 14, 14, 14, 14, 15), PlayerName = c("CF",
"DR", "GB", "KB", "RA",
"AM"), Distance = c(3.8088, 2.1279, 2.4239, 1.3565,
4.5082, 4.4097), SprintDistance = c(291.473, 146.97, 11.071,
67.596, 252.787, 0), TopSpeed = c(22.6402, 21.3442, 20.5762,
21.6002, 20.5602, 18.6401)), row.names = c(NA, -6L), groups = structure(list(
Week = c(14, 15), .rows = structure(list(1:5, 6L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
And the targets data frame:
structure(list(PlayerName = c("AM", "AB", "AMc",
"BC", "CD", "CM"), Distance = c(28.2753333333333,
34.867, NA, 31.633, 34.6122, 32.1405), SprintDistance = c(1355.2,
1074.85, NA, 2426.55, 2430.54, 2447.9), TopSpeed = c(32.61, 30.3,
NA, 36.82, 42, 33.44)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
I have been working on this for a few days now and can't wrap my head around how to do it or find a post which describes what I want to do. So far this is what I have:
# DEFINE UI ####
ui <- fluidPage(
titlePanel("GPS Monitoring Dashboard"),
sidebarLayout(
sidebarPanel(
#select player
selectInput(inputId = "name",
label = strong("Choose player"),
choices = unique(gps2$PlayerName),
selected = "AB"),
#select weeks
numericRangeInput(inputId = "week",
label = strong("Choose weeks"),
value = c(36, 37))),
# graphs and tables
mainPanel(
plotOutput(outputId = "TD"),
tableOutput(outputId = "TDsum"))
)
)
# DEFINE SERVER ####
server <- function(input, output) {
# Total Distance ----
# Data for distance plot
TD_plot <- reactive({
gps2 %>%
filter(PlayerName == input$name,
Week >= input$week [1] &
Week <= input$week [2],
) %>%
select(Distance)
})
# Build distance plot
output$TD <- renderPlot({
ggplot(TD_plot()) +
geom_point(aes(Week, Distance,
color = Distance > 5),
stat = "identity", size = 3) +
scale_color_manual(name = "Target met", values = set_names(c("green", "red"), c(TRUE, FALSE))) +
geom_line(aes(Week, Distance), size = 1) +
labs(title = "Weekly Total Distance", x = "Week", y = "Distance (km)")
})
# Data for distance table
TD_sum <- reactive({
gps2 %>%
filter(PlayerName == input$name,
Week >= input$week [1] &
Week <= input$week [2])%>%
select(Distance) %>%
pivot_wider(.,
names_from = Week,
values_from = Distance)
})
# Build distance table
output$TDsum <- renderTable(TD_sum())
}
shinyApp(ui = ui, server = server)
Right now the data points changes based on an arbitrary value (5) as I was trying to expand on that. I hope this explains in enough detail what I'm trying to do, thanks in advance for your help!
Here's a working example that may be helpful.
First, would left_join your actual distances by players, and their target distances. This will rename columns with "Actual" or "Target" as suffixes to keep them apart.
In geom_point you can use color = DistanceActual > DistanceTarget to have differential color based on whether a distance is greater or less than the target.
I simplified the other functions for demonstration.
library(shiny)
library(tidyverse)
full_data <- left_join(gps2, df_targets, by = "PlayerName", suffix = c("Actual", "Target"))
# DEFINE UI ####
ui <- fluidPage(
titlePanel("GPS Monitoring Dashboard"),
sidebarLayout(
sidebarPanel(
#select player
selectInput(inputId = "name",
label = strong("Choose player"),
choices = unique(full_data$PlayerName),
selected = "player1"),
#select weeks
numericRangeInput(inputId = "week",
label = strong("Choose weeks"),
value = c(36, 37))),
# graphs and tables
mainPanel(
plotOutput(outputId = "TD"),
tableOutput(outputId = "TDsum"))
)
)
# DEFINE SERVER ####
server <- function(input, output) {
# Filter by week and player name
TD_data <- reactive({
full_data %>%
filter(PlayerName == input$name,
Week >= input$week [1],
Week <= input$week [2])
})
# Build distance plot
output$TD <- renderPlot({
ggplot(TD_data()) +
geom_point(aes(Week, DistanceActual, color = DistanceActual > DistanceTarget), stat = "identity", size = 3) +
scale_color_manual(name = "Target met", values = set_names(c("green", "red"), c(TRUE, FALSE))) +
geom_line(aes(Week, DistanceActual), size = 1) +
labs(title = "Weekly Total Distance", x = "Week", y = "Distance (km)")
})
# Build distance table
output$TDsum <- renderTable(
TD_data() %>%
select(Week, DistanceActual)
)
}
shinyApp(ui = ui, server = server)
I have a data frame where I have multiple 'Strains', found in animals over time.
I have created a shiny app to look at the relative proportions of these strains over time.
I want to be able to filter the plot so that it looks at different combinations of the locations and time.
My problem is that I want to set particular colours to the groups of strains - so Staphylococcus remain yellow, Bacillus blue and Enterococcus red etc. However, when I create the shiny reactive element to filter the data, it doesn't seem to change the colour vector I have created. I'm not sure what I'm doing wrong
I've created a small example of this data and put my current code below.
library(plyr)
library(dplyr)
library(shiny)
library(ggplot2)
library(reshape2)
library(RColorBrewer)
# Toy Data
Strains <- c("Enterococcus faecium","Wickerhamomyces anomalus", "Staphylococcus vitulinus","Staphylococcus lentus", "Staphylococcus succinus", "Bacillus licheniformis", "Lysinibacillus sphaericus","Staphylococcus succinus", "Bacillus licheniformis", "Lysinibacillus sphaericus","Staphylococcus aureus" )
Location <- c("A", "B", "C", "B", "A", "A", "C", "C", "C", "B", "B" )
Time <- c( "2", "1", "3", "3", "4", "2", "1", "4", "1", "3", "1")
toy <- data.frame(Strains,Location, Time)
toy$count <- 1
# define colors by Genus
staphcol <- colorRampPalette(brewer.pal(9, 'YlGn')[c(2)])
colicol <- colorRampPalette(brewer.pal(9, 'RdPu')[c(3)])
baccol <- colorRampPalette(brewer.pal(9, 'PuBu')[c(3)])
othercol <- colorRampPalette(brewer.pal(9, 'YlOrRd')[c(9)])
# Colour function
colourFunction <- function(data){
species <- data.frame(table(data$Strains)) # Frequency table of strains
species <- species[order(species$Freq), ] #Ordered smallest to largest frequency
# Order species by genus
specieslist <- as.character(species$Var1[order(species$Freq, decreasing = TRUE)])
staphlist <- grep('Staph', specieslist, value = TRUE)
baclist <- c(grep('Bac', specieslist, value = TRUE),grep('bacillus', specieslist, value = TRUE))
colilist <- c(grep('Enterococcus', specieslist, value = TRUE))
all <- c(staphlist, baclist, colilist)
otherlist <- specieslist[!specieslist %in% all] # All species that haven't already been selected
# Create colour vector
c(staphcol(length(staphlist))[seq(length(staphlist), 1, -1 )],
colicol(length(colilist))[seq(length(colilist), 1, -1 )],
baccol(length(baclist))[seq(length(baclist), 1, -1 )],
othercol(length(otherlist))[seq(length(otherlist), 1, -1 )])
}
# Factor function
factorFunction <- function(data){
species <- data.frame(table(data$Strains)) # Frequency table of strains
species <- species[order(species$Freq), ] #Ordered smallest to largest frequency
# Order species by genus
specieslist <- as.character(species$Var1[order(species$Freq, decreasing = TRUE)])
staphlist <- grep('Staph', specieslist, value = TRUE)
baclist <- c(grep('Bac', specieslist, value = TRUE),grep('bacillus', specieslist, value = TRUE))
colilist <- grep('Enterococcus', specieslist, value = TRUE)
all <- c(staphlist, baclist, colilist)
otherlist <- specieslist[!specieslist %in% all] # All species that haven't already been selected
c( staphlist, colilist, baclist, otherlist)
}
ui <- fluidPage(
#Add application title
titlePanel("Relative abundance of strains"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput("LocationInput", "Location",
choices = c('A','B','C'),
selected = c('A','B','C')),
checkboxGroupInput("TimeInput", "Time",
choices = c('1', "2", "3", "4"),
selected = c('1', "2", "3", "4")),
textInput("titleInput","Enter Title for Graph:")
),
mainPanel(
plotOutput("plot")
))
)
server <- function(input, output) {
filtered <- reactive({
subset(toy, Time %in% input$TimeInput & Location %in% input$LocationInput)
})
output$plot <- renderPlot({
data <- ddply(filtered(), c("Strains", "Time", "Location"), summarize, tot = sum(count))
#Set colours for filtered plot
col <- colourFunction(data)
levels <- factorFunction(filtered())
data$Strains <- factor(data$Strains,levels = levels )
p <- ggplot(data = data, aes(y = tot, x = Time, fill = Strains), colour = "black") +
geom_bar(position = "fill", stat = "identity") +
theme_bw() +
scale_fill_manual(values = col) +
ylab("Relative Proportion") + theme_bw() +
theme(legend.position="right")+
theme(axis.text = element_text(size = 12),
axis.title = element_text(size = 12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.key=element_blank()) +
ggtitle(input$titleInput)
p
})
}
shinyApp(ui = ui, server = server)
I' trying to modify pch parameter of plot by inserting an input from selectInput:
selectInput("points", "Points:",
list("Job lost" = "joblost",
"Sex" = "sex",
))
into
output$Plot <- renderPlot({
plot(as.formula(formula()),data=Benefits,
main = caption(), pch = as.numeric(input$points),
col=as.numeric(input$points))
})
Unfortunately, I get an error: cannot coerce type 'closure' to vector of type 'double'. What steps should I take to fix this ? Of course, both joblost and sex are factors.
Full code:
library(shiny)
library(Ecdat)
attach(Benefits)
u <- shinyUI(pageWithSidebar(
headerPanel("Social benefits"),
sidebarPanel(
selectInput("variable1", "Zmienna X:",
list("Bezrobocie" = "stateur",
"Max zasilek" = "statemb",
"Wiek" = "age",
"Staz w bezrobociu" = "tenure",
"Replacement rate" = "rr"
)),
selectInput("variable2", "Zmienna Y:",
list("Bezrobocie" = "stateur",
"Max zasilek" = "statemb",
"Wiek" = "age",
"Staz w bezrobociu" = "tenure",
"Replacement rate" = "rr"
)),
selectInput("points", "Punkty:",
list("Powod utraty pracy" = "joblost",
"Plec" = "sex",
"Nie-bialy" = "nwhite",
">12 lat szkoly" = "school12",
"Robotnik fizyczny" = "bluecol",
"Mieszka w miescie" = "smsa",
"Zonaty" = "married",
"Ma dzieci" = "dkids",
"Male dzieci" = "dykids",
"Glowa rodziny" = "head",
"Otrzymuje zasilki" = "ui"
)),
checkboxInput("reg", "Pokaz krzywa regresji", FALSE)
),
mainPanel(
plotOutput("Plot")
)
))
s <- shinyServer(function(input, output)
{
formula <- reactive({paste(input$variable2,"~",input$variable1)})
caption <- renderText({formula()})
pkt <- reactive({input$points})
#pkt <- renderText({paste(input$points)})
output$Plot <- renderPlot({
plot(as.formula(formula()),data=Benefits,
main = caption(), pch = as.numeric(input$points),
col=as.numeric(input$points))
if(input$reg == TRUE){
abline(lm(as.formula(formula())),col ="red", lwd = 2)
legend("topleft",inset = 0.02, legend = "Krzywa regresji",
col="red",lty = 1, lwd = 2)
}
})
})
shinyApp(u,s)
The issue was resolved by using a switch in selectInput:
pkt <- reactive({
switch(input$points,
"Powod utraty pracy" = joblost,
"Plec" = sex,
"Nie-bialy" = nwhite,
">12 lat szkoly" = school12,
"Robotnik fizyczny" = bluecol,
"Mieszka w miescie" = smsa,
"Zonaty" = married,
"Ma dzieci" = dkids,
"Male dzieci" = dykids,
"Glowa rodziny" = head,
"Otrzymuje zasilki" = ui)
})
txt <- renderText({paste(input$points)})
output$Plot <- renderPlot({
plot(as.formula(formula()),data=Benefits,
main = caption(), pch = as.numeric(pkt()),
col=as.numeric(pkt()))
I'm building an shiny application to show some quality control data to our clients. First i had the application created with GGplot functionalities. Now i am converting all graphs to Plotly output. For one of these plots (a boxplot). I have the problem that i cant pass a shiny input selector to the plot.
In GGplot there is no problem at all and the plot is changed each time i choose a different plotColumn. Here i solved the problem of column parsing with the aes_string function. Basically i am looking for something similar in plotly.
Working GGPLOT example:
ggplot(finalDf, aes_string("runName",input$getBoxplotField),na.rm = T) +
geom_boxplot(aes_string(fill="runName"), notch = F) +
geom_jitter() +
scale_y_continuous(labels = format1) +
theme_bw()
Not working Plot_ly example
p <- plot_ly(finalDf,x = runName, y = input$getBoxplotField, type = "box")
exampleDf
> dput(head(finalDf))
structure(list(runName = c("Gentrap.1451849446759", "Gentrap.1451849446759",
"Gentrap.1451849446759", "Gentrap.1451849446759", "Gentrap.1451849446759",
"Gentrap.1451849446759"), sampleName = c("Hart_FC42b_L5_I2_SRD329",
"S1", "S2", "S3","S4", "S5"), readGroupName = c(NA,
NA, NA, NA, NA, NA), maxInsertSize = c(227615351L, 202850798L,
249001722L, 234388122L, 188295691L, 249009605L), medianCvCoverage = c(0.501303,
0.494183, 0.574364, 0.487233, 0.495491, 0.483041), medianInsertSize = c(197L,
203L, 200L, 208L, 200L, 194L), median3PrimeBias = c(0.283437,
0.263973, 0.372476, 0.266946, 0.296308, 0.292954), median5PrimeBias = c(0.139005,
0.21233, 0.123449, 0.185168, 0.169128, 0.152902), median5PrimeTo3PrimeBias = c(0.586081,
0.9234, 0.409042, 0.83276, 0.680496, 0.640518), nBasesAligned = c(1627112497,
1572782400, 1772774189, 1595461211, 1593529487, 1705441762),
nBasesCoding = c(795255442, 778886694, 762223625, 819014623,
759061861, 838846117), nBasesIntergenic = c(140893219, 176728812,
194156767, 120900630, 137267440, 148815172), nBasesIntron = c(134528982,
111795186, 121091943, 96554581, 142587231, 139962698), nBasesRibosomal = c(NA,
NA, NA, NA, NA, NA), nBasesUtr = c(556434854, 505371708,
695301854, 558991377, 554612955, 577817775), nCorrectStrandReads = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
), nIncorrectStrandReads = c(NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_), nReadsAligned = c(33157934L,
32082625L, 36181227L, 32595741L, 32538544L, 34783342L), nReadsProperPair = c(31935921L,
30983730L, 35015854L, 31358224L, 31405592L, 33479007L), nReadsSingleton = c(3919886L,
4311016L, 4382092L, 3848808L, 3873270L, 4122759L), nReadsTotal = c(37077604L,
36393382L, 40563115L, 36444288L, 36411547L, 38905908L), pctChimeras = c(0.004783,
0.003078, 0.003063, 0.004278, 0.002983, 0.00485), rateIndel = c(0.000071,
0.000076, 0.000081, 0.000066, 0.000072, 0.00007), rateReadsMismatch = c(0.001438,
0.001643, 0.001627, 0.001467, 0.001716, 0.001471), stdevInsertSize = c(120.677992,
129.927513, 114.820226, 138.486257, 118.98163, 115.25774),
group = c("Gentrap.1451849446759", "Gentrap.1451849446759",
"Gentrap.1451849446759", "Gentrap.1451849446759", "Gentrap.1451849446759",
"Gentrap.1451849446759")), .Names = c("runName", "sampleName",
"readGroupName", "maxInsertSize", "medianCvCoverage", "medianInsertSize",
"median3PrimeBias", "median5PrimeBias", "median5PrimeTo3PrimeBias",
"nBasesAligned", "nBasesCoding", "nBasesIntergenic", "nBasesIntron",
"nBasesRibosomal", "nBasesUtr", "nCorrectStrandReads", "nIncorrectStrandReads",
"nReadsAligned", "nReadsProperPair", "nReadsSingleton", "nReadsTotal",
"pctChimeras", "rateIndel", "rateReadsMismatch", "stdevInsertSize",
"group"), row.names = c(NA, 6L), class = "data.frame")
server.R
shinyServer(function(input, output, session) {
output$selectBoxplotField <- renderUI({
selectInput("getBoxplotField", label = "Select variable to plot", choices = names(getAllSampleStats()))
})
output$boxplot <- renderPlotly({
finalDf #as defined above in the example
p <- plot_ly(finalDf, x = runName, y = input$getBoxplotField , type = "box")
})
}
GUI.R
shinyUI(navbarPage(
theme = "bootstrap_sandstone.css",
"SPIN", fluid = T,
tabPanel("Gentrap",
fluidPage(fluidRow(
sidebarlogin(pipelineName = "gentrap"),
column(10,
tabsetPanel(
tabPanel("Metrics distribution",
fluidRow(
column(2),
column(8, plotlyOutput("boxplot")),
column(2)
),
fluidRow(
column(3, uiOutput("selectBoxplotField")),
column(3, checkboxInput("checkboxplot", label = "Compare to All", value = TRUE))
),
fluidRow(
column(9, helpText("If no plot shows up it means this data is not present in the Sentinel QC database"))
)),
))
)))
))
The problem is fixed by passing the DF plus columns directly to the X and Y axes without first passing the DF name as a argument.
Proper plot will be generated when this is done:
plot_ly(x = finalDf[,'runName'], y = finalDf[,input$getBoxplotField] , type = "box", color = 'red') %>%
layout(xaxis = list(showticklabels = FALSE, title = ''), yaxis = yName)
This is wrong:
plot_ly(finalDf, x = runName, y = input$getBoxplotField , type = "box", color = 'red') %>%
layout(xaxis = list(showticklabels = FALSE, title = ''), yaxis = yName)