I'm currently having issues rendering my plot in shiny. The layout is all fine, but when run the plot does not appear.
Link to data in csv file:
https://www.dropbox.com/s/hv3k12ja9r10tzz/pointvaluedfmelt.csv?dl=0
UI Code:
library(shiny)
library(ggplot2)
library(RColorBrewer)
pointvaluedf.melt<- read.csv("pointvaluedfmelt.csv", stringsAsFactors = F)
pointvaluedf.melt$X<- NULL
pointvaluedf.melt$PLAYER_NAME<- as.factor(pointvaluedf.melt$PLAYER_NAME)
pointvaluedf.melt$TEAM_ABBREVIATION<- as.factor(pointvaluedf.melt$TEAM_ABBREVIATION)
pointvaluedf.melt$name.zone<- as.factor(pointvaluedf.melt$name.zone)
ui <- fluidPage(
titlePanel("Top 5 Most Valuable Shots by Player"),
sidebarLayout(
sidebarPanel(selectInput("team",
label = "Choose a Team",
choices = c("Celtics", "Nets","Knicks", "76ers", "Raptors",
"Mavericks","Rockets","Grizzlies","Pelicans",
"Spurs","Bulls","Cavs","Pistons","Pacers","Bucks",
"Nuggets","Timberwolves","Thunder","Blazers",
"Jazz","Hawks","Hornets","Heat","Magic","Wiz",
"Warriors","Clippers","Lakers","Suns","Kings"),
selected = "Celtics"), width = 2
),
mainPanel(plotOutput("myplot"))
))
Server Code:
library(shiny)
library(ggplot2)
library(RColorBrewer)
pointvaluedf.melt<- read.csv("pointvaluedfmelt.csv", stringsAsFactors = F)
pointvaluedf.melt$X<- NULL
pointvaluedf.melt$PLAYER_NAME<- as.factor(pointvaluedf.melt$PLAYER_NAME)
pointvaluedf.melt$TEAM_ABBREVIATION<- as.factor(pointvaluedf.melt$TEAM_ABBREVIATION)
pointvaluedf.melt$name.zone<- as.factor(pointvaluedf.melt$name.zone)
server <- function(input, output) {
df<- reactive({pointvaluedf.melt[pointvaluedf.melt$TEAM_ABBREVIATION==input$team,]})
output$myplot <- renderPlot(function(){
dd<- df()
tea <- switch(input$team,
"Celtics" = "BOS",
"Nets" = "BKN",
"Knicks" = "NYK",
"76ers" = "PHI",
"Raptors" = "TOR",
"Mavericks" = "DAL",
"Rockets" = "HOU",
"Grizzlies" = "MEM",
"Pelicans" = "NOP",
"Spurs" = "SAS",
"Bulls" = "CHI",
"Cavs" = "CLE",
"Pistons" = "DET",
"Pacers" = "IND",
"Bucks" = "MIL",
"Nuggets" = "DEN",
"Timberwolves" = "MIN",
"Thunder" = "OKC",
"Blazers" = "POR",
"Jazz" = "UTA",
"Hawks" = "ATL",
"Hornets" = "CHA",
"Heat" = "MIA",
"Magic" = "ORL",
"Wiz" = "WAS",
"Warriors" = "GSW",
"Lakers" = "LAL",
"Clippers" = "LAC",
"Suns" = "PHX",
"Kings" = "SAC")
p<- ggplot(data=head(subset(dd, TEAM_ABBREVIATION %in% tea)
[order(-subset(dd, TEAM_ABBREVIATION %in% tea)[,4]),],5),
aes(x=reorder(name.zone,-value), y=value))+
geom_bar(stat="identity", fill="#4292C6", col="black", size=1.2)+
theme(axis.text.x=element_text(angle=35, hjust=1))+
labs(x="Player and Shot Type", y="Point Value", title="Top 5 Value Shots")
print(p)
})
}
There are some errors in there:
1- Render plot does not need the "function()" keyword on it, just renderPlot({})
2- You are not using reactive the proper way. You can make it simple and better with two reactive objects, and renderPlot consuming it besides put everything inside the renderPlot logic. This way, you can reuse objects and make your code cleaner.
3- Because you are doing reactive the wrong way, the data.frame was empty when you change the values...
library(shiny)
library(ggplot2)
library(RColorBrewer)
pointvaluedf.melt<- read.csv("pointvaluedfmelt.csv", stringsAsFactors = F)
pointvaluedf.melt$X<- NULL
pointvaluedf.melt$PLAYER_NAME<- as.factor(pointvaluedf.melt$PLAYER_NAME)
pointvaluedf.melt$TEAM_ABBREVIATION<- pointvaluedf.melt$TEAM_ABBREVIATION
pointvaluedf.melt$name.zone<- as.factor(pointvaluedf.melt$name.zone)
ui <- fluidPage(
titlePanel("Top 5 Most Valuable Shots by Player"),
sidebarLayout(
sidebarPanel(selectInput("team",
label = "Choose a Team",
choices = c("Celtics", "Nets","Knicks", "76ers", "Raptors",
"Mavericks","Rockets","Grizzlies","Pelicans",
"Spurs","Bulls","Cavs","Pistons","Pacers","Bucks",
"Nuggets","Timberwolves","Thunder","Blazers",
"Jazz","Hawks","Hornets","Heat","Magic","Wiz",
"Warriors","Clippers","Lakers","Suns","Kings"),
selected = "Celtics"), width = 2
),
mainPanel(plotOutput("myplot"))
))
server <- function(input, output, session) {
df <- reactive({
pointvaluedf.melt[pointvaluedf.melt$TEAM_ABBREVIATION==tea(),]
})
tea <- reactive({
switch(input$team,
"Celtics" = "BOS",
"Nets" = "BKN",
"Knicks" = "NYK",
"76ers" = "PHI",
"Raptors" = "TOR",
"Mavericks" = "DAL",
"Rockets" = "HOU",
"Grizzlies" = "MEM",
"Pelicans" = "NOP",
"Spurs" = "SAS",
"Bulls" = "CHI",
"Cavs" = "CLE",
"Pistons" = "DET",
"Pacers" = "IND",
"Bucks" = "MIL",
"Nuggets" = "DEN",
"Timberwolves" = "MIN",
"Thunder" = "OKC",
"Blazers" = "POR",
"Jazz" = "UTA",
"Hawks" = "ATL",
"Hornets" = "CHA",
"Heat" = "MIA",
"Magic" = "ORL",
"Wiz" = "WAS",
"Warriors" = "GSW",
"Lakers" = "LAL",
"Clippers" = "LAC",
"Suns" = "PHX",
"Kings" = "SAC")
})
output$myplot <- renderPlot({
p <- ggplot(data=head(subset(df(), TEAM_ABBREVIATION %in% tea())
[order(-subset(df(), TEAM_ABBREVIATION %in% tea())[,4]),],5),
aes(x=reorder(name.zone,-value), y=value))+
geom_bar(stat="identity", fill="#4292C6", col="black", size=1.2)+
theme(axis.text.x=element_text(angle=35, hjust=1))+
labs(x="Player and Shot Type", y="Point Value", title="Top 5 Value Shots")
p
})
}
shinyApp(ui, server)
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 trying to create and render an interactive formattable table in a shiny app.
Here is a sample dataframe:
tcharts <- data.frame(pgm = c(1,2,3,4,5,6,7,8),
horse = c("Cigar", "Funny Cide", "Animal Kingdom", "Blame", "Zenyatta", "New Years Day", "Northern Dancer", "Beautiful Pleasure"),
groundloss = c(55,70,85,42,90,45,53,50),
distanceRun = c(5050,5070,5085,5045,5090,5045,5053,5050),
ttl = c(50,70,85,42,90,45,53,50),
fps = c(52.3,51.8,51.9,52.0,53.6,52.9,53.7,53.1),
finishTime = c(52.3,51.8,51.9,52.0,53.6,52.9,53.7,53.1),
finish = c(4,7,1,2,5,6,3,8),
BL = c(0,1,2,6,2,9,6,8),
rnum = c(1,1,1,1,1,1,1,1),
sixteenth = c(330,330,330,330,330,330,330)
)
Working version
This version of the code, when list() is empty (use all variables in dataframe) produces a table as expected.
library(shiny)
library(formattable)
inputPanel(
selectInput("rnum", label = "Race Number:",
choices = c(1,2,3,4,5,6,7,8,9), selected = 1),
sliderInput("poc", label = "Point of Call:",
min = 330, max = 5280, value = 330, step = 330)
)
cdat <- reactive({
tcharts %>% filter(rnum %in% input$rnum) %>%
filter(Sixteenth %in% input$poc)
})
renderFormattable({
formattable(cdat(),list(
))
})
Error Version:
With this version, I get an ERROR: object pgm not found
library(shiny)
library(formattable)
inputPanel(
selectInput("rnum", label = "Race Number:",
choices = c(1,2,3,4,5,6,7,8,9), selected = 1),
sliderInput("poc", label = "Point of Call:",
min = 330, max = 5280, value = 330, step = 330)
)
cdat <- reactive({
tcharts %>% filter(rnum %in% input$rnum) %>%
filter(Sixteenth %in% input$poc)
})
renderFormattable({
formattable(cdat(),list(
pgm,
Horse
))
})
The error message leads me to believe I'm not specifying the variable correctly, but I'm not sure how to do it. I'v looked at several formattable / shiny SO questions and responses, but have not come up with the correct sytax.
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)
)
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)