I want to draw 20 graphs in shiny by loop and I don't want to write the output one by one. So I am thing doing a loop to output these graphs. I found a very good example in the shiny gallery which shows how to output texts. I tiried it and it worked.
Now my problem is: How can I replace the text output to plotly? I have the plotly ready(to simplify I am not showing here). What I tried is first replace the strong(paste0(.. line with my plotly object. Second, replace renderUI to renderplotly and replace uiOutput to plotOutput. I am getting errors ggplotly has no applicable method for shiny.tag which I understand that plotOutput is not compatible with tagged output. So what can I do here?
server.r:
shinyServer(function(input, output,session) {
lapply(1:2, function(i) {
output[[paste0('b', i)]] <- renderUI({
strong(paste0('Hi, this is output B#', i)) })# to be replaced with a plotly object p
})})
ui.r:
fluidRow(
lapply(1:2, function(i) {
uiOutput(paste0('b', i))
})
)
Check out this example Shiny app that displays a dynamic number of plots: https://gist.github.com/wch/5436415/
I adapted the above app to plot the cars dataset with ggplotly.
library(shiny)
library(ggplot2)
library(plotly)
shinyApp(
##### ui #######
ui = fluidPage(
fluidRow(
sliderInput("n",
"Number of plots",
value = 1, min = 1, max = 5)),
fluidRow(
uiOutput("plots"))
),
##### server ######
server = function(input, output) {
data("cars")
# define max number of plots
max_plots <- 5
# generate the plots
output$plots <- renderUI({
plot_output_list <- lapply(1:input$n, function(i) {
plotname <- paste0("plot", i)
plotlyOutput(plotname)
})
# convert the list to a tagList - this is necessary for the list of
# items to display properly
do.call(tagList, plot_output_list)
})
# call renderPlotly for each plot. Plots are only generated when they are
# visible on the web page
for(i in 1:max_plots) {
# Need local so that each item gets its own number. Without it, the value
# of i in the renderPlotly() will be the same across all instances, because
# of when the expression is evaluated
local({
my_i <- i
plotname <- paste0("plot", my_i)
output[[plotname]] <- renderPlotly({
g <- ggplot(cars, aes(x = speed, y = dist)) +
geom_point() +
labs(title = paste0("Plot ", my_i))
g <- ggplotly(g)
dev.off()
g
})
})
}
}
)
Creating one plot with many subplots:
library(shiny)
library(ggplot2)
library(plotly)
library(grid)
shinyApp(
##### ui #######
ui = fluidPage(
fluidRow(
sliderInput("n",
"Number of plots",
value = 1, min = 1, max = 5)),
fluidRow(
plotlyOutput("plots")
)
),
##### server ######
server = function(input, output) {
data("cars")
# define max number of plots
max_plots <- 5
# generate the plots
output$plots <- renderPlotly({
plot_list <- lapply(1:input$n, function(i) {
g <- ggplot(cars, aes(x = speed, y = dist)) +
geom_point() +
theme(plot.margin = unit(c(3, 1, 1, 1), "lines"))
ggplotly(g)
})
p <- subplot(plot_list[1:input$n], shareX = TRUE, shareY = TRUE) %>%
layout(title = "Car Plots")
dev.off()
p
})
}
)
Related
I am trying to create multiple plots in the same shiny window. Not sure what I am doing wrong. It throws an error that something is up with my main panel.
I tried a few different things but nothing seems to work. Btw this is an adaptation of Mike's answer to this question
How can put multiple plots side-by-side in shiny r?
library(ggplot2)
library(gridExtra)
library(shiny)
ui <- fluidPage(
titlePanel("title panel"),
sidebarLayout(position = "left",
sidebarPanel("sidebar panel",
checkboxInput("donum1", "plot1", value = T),
checkboxInput("donum2", "plot2", value = F),
checkboxInput("donum3", "plot3", value = F)
),
mainPanel("main panel", outputplot="myPlot")))
server <- function(input, output, session) {
Feature_A <- c(1, 2,1, 4,2)
Feature_B <- c(4,5,6,6,6)
Feature_C <- c(22,4,3,1,5)
df<- data.frame(Feature_A ,Feature_B ,Feature_C)
pt1 <- reactive({
if (!input$donum1) return(NULL)
p1<-ggplot(data=df, aes(Feature_A))+ geom_histogram()
})
pt2 <- reactive({
if (!input$donum2) return(NULL)
p2<-ggplot(data=df, aes(Feature_B))+ geom_histogram()
})
pt3 <- reactive({
if (!input$donum3) return(NULL)
p3<-ggplot(data=df, aes(Feature_C))+ geom_histogram()
})
output$myPlot = renderPlot({
grid.arrange(p1, p2, p3, ncol=2,top="Main Title")
})
}
shinyApp(ui, server)
EDIT:
Thanks to YBS's comment below, I have been able to figure out the above issue and have made the change to my code above. But the code is not generating any plot. Can someone help please!
That mainPanel() call wasn't nested correctly. It needed a comma before it, because it's an argument to sidebarLayout() (which in turn is an argument to fluidPage().
ui <- fluidPage(
titlePanel("title panel"),
sidebarLayout(
position = "left",
sidebarPanel(
"sidebar panel",
checkboxInput("donum1", "plot1", value = T),
checkboxInput("donum2", "plot2", value = F),
checkboxInput("donum3", "plot3", value = F)
),
mainPanel("main panel", outputplot="myPlot")
)
)
I like using the indention/tab level to show the nested structure more clearly. It helps me catch my mistakes.
After your second question:
I'm not sure how far you want to take it towards the example you gave. The most important difference I see is that you're not returning anything from pt1, pt2, & pt3. Remove the assignment part (e.g., p1<-).
server <- function(input, output, session) {
Feature_A <- c(1, 2,1, 4,2)
Feature_B <- c(4,5,6,6,6)
Feature_C <- c(22,4,3,1,5)
df<- data.frame(Feature_A ,Feature_B ,Feature_C)
pt1 <- reactive({
if (!input$donum1) return(NULL)
ggplot(data=df, aes(Feature_A))+ geom_histogram()
})
pt2 <- reactive({
if (!input$donum2) return(NULL)
ggplot(data=df, aes(Feature_B))+ geom_histogram()
})
pt3 <- reactive({
if (!input$donum3) return(NULL)
ggplot(data=df, aes(Feature_C))+ geom_histogram()
})
output$myPlot = renderPlot({
ptlist <- list(pt1(),pt2(),pt3())
wtlist <- c(input$wt1,input$wt2,input$wt3)
# remove the null plots from ptlist and wtlist
to_delete <- !sapply(ptlist,is.null)
ptlist <- ptlist[to_delete]
wtlist <- wtlist[to_delete]
if (length(ptlist)==0) return(NULL)
grid.arrange(grobs=ptlist,widths=wtlist,ncol=length(ptlist))
})
}
I am trying to make an RShiny app that you can pick a gene from a list, and it will display different graphs using that gene's transcripts. However, each gene has a different number of transcripts, so a different number of graphs must be displayed every time a different gene is chosen. How I have it set right now is that when a person chooses a gene, a new table is created with the transcript numbers (data to be plotted) along with a new list of all the transcript names (length of this list is the amount of plots that I need). These are reactive values.
Below, in the server, I made a function that creates the graph that I want, and then I iterate through the creation of the function by indexing into the reactive list of names, so it creates a graph for each name (as each name is a different transcript). Right now, the code iterates through all the names correctly but only displays the last plot. Is there a way to have every plot displayed? I have tried a lot of different things, from renderUI to using local calls, but cannot figure it out.
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("var", label = "Choose a gene to display", names),
mainPanel(
plotOutput("tdot"))
))
server <- function(input, output) {
genename <- reactive({
input$var
})
transTable2 <- reactive ({
cbind(biofluids, select(transTable, starts_with(input$var)))
})
names <- reactive ({
tableBF <- cbind(biofluids, select(transTable, starts_with(input$var)))
n <- colnames(tableBF)
final <- n[-1]
})
createUI <- function(name, table) {
ggplot(table, aes_string(x = "biofluids", y = name))+geom_boxplot(aes(color = biofluids))+
geom_boxplot(aes(fill = biofluids)) + scale_y_log10()+ylab( 'log10 normalized counts')+
ggtitle(name)}
output$tdot <- renderPlot({
lapply(1:length(names()), function(i)
createUI(names()[i], transTable2()))
})
}
# Run the application
shinyApp(ui = ui, server = server)
A reproducible example is as follows with the iris dataset, which would have the user select a category (either "Sepal" or "Petal"), and then create a plot for every column in the dataset that starts with that word:
cats <- c("Sepal", "Petal")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("var", label = "Choose a category to display", cats),
mainPanel(
plotOutput("tdot"))
))
server <- function(input, output) {
category <- reactive({
input$var
})
iris2 <- reactive ({
select(iris, starts_with(input$var))
})
names <- reactive ({
table2 <- select(transTable, starts_with(input$var))
n <- colnames(table2)
})
createUI <- function(name, table) {
ggplot(table, aes_string(x = "species", y = name))+geom_boxplot(aes(color = species))+
geom_boxplot(aes(fill = species)) + scale_y_log10()+ylab( 'log10 normalized counts')+
ggtitle(name)}
output$tdot <- renderPlot({
lapply(1:length(names()), function(i)
createUI(names()[i], iris2()))
})
}
# Run the application
shinyApp(ui = ui, server = server)
The following code generates dynamic number of outputs with iris data. You should be able to adapt this to your data.
library(shiny)
library(tidyverse)
# Load data
data("iris")
# Add row id
iris2 <- iris %>% mutate(ID = 1:n())
# ui
ui <- fluidPage(
sidebarPanel(
selectInput(inputId = "sel", label = "Select one or more parameters",
choices = names(iris2), multiple = TRUE)
),
mainPanel(
uiOutput("plots")
)
)
# server
server <- function(input, output, session){
# Dynamically generate the plots based on the selected parameters
observe({
req(input$sel)
lapply(input$sel, function(par){
p <- ggplot(iris2, aes_string(x = "ID", y = par)) +
geom_boxplot(aes(fill = Species, group=Species, color=Species)) +
ggtitle(paste("Plot: ", par))
output[[paste("plot", par, sep = "_")]] <- renderPlot({
p
},
width = 380,
height = 350)
})
})
# Create plot tag list
output$plots <- renderUI({
req(input$sel)
plot_output_list <- lapply(input$sel, function(par) {
plotname <- paste("plot", par, sep = "_")
plotOutput(plotname, height = '250px', inline=TRUE)
})
do.call(tagList, plot_output_list)
})
}
shinyApp(ui, server)
It gives the following output:
I am working on a Shiny app and as I go I have been adding figures and tables in a haphazard way. I would like to have a better framework so that I can flexibly add reactive figures and tables to the output as it develops further.
At the moment I have been using tabPanel and fluidrow to add additional a summary table and a second plot. However I have had trouble adapting this. For example I currently generate 3 plots but have only able to plot 2 at a time. Could anyone show me a way to modify the code to display all three plots (distPlot1, distPlot2, distPlot3) and the summary table on the same page? Ideally in a way that it would be simple to add additional tables and plots in the future.
Thank you in advance.
My current code is below.
ui.R
library(reshape2)
library(shiny)
library(ggplot2)
# Define UI for application that draws a histogram
fluidPage(
# Application title
titlePanel("Mutation Probability"),
# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
sliderInput("x", "Probability of mutation (per bp):",
min=1/1000000000000, max=1/1000, value=1/10000000),
sliderInput("y", "Size of region (bp):",
min = 10, max = 10000, value = 1000, step= 100),
sliderInput("z", "Number of samples:",
min = 1, max = 100000, value = 1000, step= 10)
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(
tabPanel("Plot",
fluidRow(
splitLayout(cellWidths = c("50%", "50%"), plotOutput("distPlot1"), plotOutput("distPlot3"), plotOutput("distPlot3)"))
)),
tabPanel("Summary", verbatimTextOutput("summary"))
)
)
)
)
server.R
server <- function(input, output) {
mydata <- reactive({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
Muts
})
output$distPlot1 <- renderPlot({
Muts <- mydata()
ggplot(Muts, aes(Muts)) + geom_density() +xlab("Observed variants")
})
output$distPlot2 <-renderPlot({
Muts <- mydata()
ggplot(Muts, aes(Muts)) + geom_histogram() + xlab("Observed variants")
})
#get a boxplot working
output$distPlot3 <-renderPlot({
Muts <- mydata()
ggplot(data= melt(Muts), aes(variable, value)) + geom_boxplot() + xlab("Observed variants")
})
output$summary <- renderPrint({
Muts <- mydata()
summary(Muts)
})
}
I like laying out the graphics in the server using tools like grid.arrange from the package gridExtra or the package cowplot - they offer a lot of layout flexiblity. This for example:
library(reshape2)
library(shiny)
library(ggplot2)
library(gridExtra)
# Define UI for application that draws a histogram
u <- fluidPage(
# Application title
titlePanel("Mutation Probability"),
# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
sliderInput("x", "Probability of mutation (per bp):",
min=1/1000000000000, max=1/1000, value=1/10000000),
sliderInput("y", "Size of region (bp):",
min = 10, max = 10000, value = 1000, step= 100),
sliderInput("z", "Number of samples:",
min = 1, max = 100000, value = 1000, step= 10)
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(
tabPanel("Plot",
fluidRow(
plotOutput("distPlot4"),
verbatimTextOutput("summary"))
)),
tabPanel("Summary", verbatimTextOutput("summary1"))
)
)
)
)
s <- function(input, output) {
mydata <- reactive({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
Muts
})
output$distPlot4 <- renderPlot({
Muts <- mydata()
p1 <- ggplot(Muts, aes(Muts)) + geom_density() +xlab("Observed variants")
p2 <- ggplot(Muts, aes(Muts)) + geom_histogram() + xlab("Observed variants")
p3 <- ggplot(data= melt(Muts), aes(variable, value)) + geom_boxplot() + xlab("Observed variants")
grid.arrange(p1,p2,p3, ncol=3,widths = c(2,1,1))
})
output$summary <- renderPrint({
Muts <- mydata()
summary(Muts)
})
}
shinyApp(u,s)
which yields:
For summary tables, I just add them to the bottom, one after the other, not much else you can do there I think.
Could anyone can tell me why I get an error when I change a dataset in first selectInput widget? When I change a dataset from diamonds to mtcars I get an error Could not find 'carat' in input$bins and in the plot just for one second and after that everything works fine. Why it happened?
library(shiny)
library(ggplot2)
data(diamonds)
data(mtcars)
ui <- fluidPage(
column(3,
selectInput("data", "", choices = c('mtcars', 'diamonds')),
uiOutput('server_cols'),
uiOutput('server_bins')
),
column(9,
plotOutput("plot")
)
)
server <- function(input, output) {
data <- reactive({
switch(input$data,
diamonds = diamonds,
mtcars = mtcars)
})
output$server_cols <- renderUI({
data <- data()
nam <- colnames(data)
selectInput('cols', "Choose numeric columns:", choices = nam[sapply(data, function(x) is.numeric(x))])
})
output$server_bins <- renderUI({
if (!is.null(input$cols)) {
df <- data()
x <- eval(input$cols)
max_value <- max(df[,x])
sliderInput('bins','Choose number of bins:', min = 0.1,
max = max_value,
value = max_value/2)
}
})
output$plot <- renderPlot({
if (!is.null(input$cols) & !is.null(input$bins)) {
basicData <- data()
var <- eval(input$cols)
ggplot(basicData, aes_string(var)) +
geom_histogram(binwidth = input$bins, color = 'white', fill = 'red')
}
})
}
shinyApp(ui, server)
Your respective output objects respond to any changes of your input variables. Thus, when you change your dataset via input$data, the plot rebuilds itself, although input$cols did not yet adjust. Actually, try inserting some print("a") inside the output$plot to see that it is called up to three times if you change input$data.
The fix is to rethink your reaction logic and let your elements respond only to specific changes, to get some kind of response "thread".
For example, input$data should only trigger output$server_cols. And output$server_bins should only be triggered by input$cols (because this already implies that input$data changed earlier). Ultimately, output$plot just has to listen to changes of input$bins (because changes in input$cols and input$data always result in changes of input$bins since it is at the end of the thread).
Here is my suggestion using isolate.
library(shiny)
library(ggplot2)
data(diamonds)
data(mtcars)
ui <- fluidPage(
column(3,
selectInput("data", "", choices = c('mtcars', 'diamonds')),
uiOutput('server_cols'),
uiOutput('server_bins')
),
column(9,
plotOutput("plot")
)
)
server <- function(input, output) {
data <- reactive({
switch(input$data, diamonds = diamonds, mtcars = mtcars)
})
output$server_cols <- renderUI({
data <- data()
nam <- colnames(data)
selectInput('cols', "Choose numeric columns:", choices = nam[sapply(data, function(x) is.numeric(x))])
})
output$server_bins <- renderUI({
if (!is.null(input$cols)) {
df <- isolate(data())
x <- eval(input$cols)
max_value <- max(df[,x])
sliderInput('bins','Choose number of bins:', min = 0.1, max = max_value, value = max_value/2)
}
})
output$plot <- renderPlot({
if (!is.null(isolate(input$cols)) & !is.null(input$bins)) {
basicData <- isolate(data())
var <- eval(isolate(input$cols))
ggplot(basicData, aes_string(var)) +
geom_histogram(binwidth = input$bins, color = 'white', fill = 'red')
}
})
}
shinyApp(ui, server)
You might also want to look into updateSelectInput and updateSliderInput if you want to alter Input Elements depending on other input.
How can we get interactive coordinates(x and y) of multiple histograms in shiny. I have tried this code
#server.R
library(xts)
shinyServer(function(input, output,session) {
output$info <- renderText({
paste0("x=", input$plot_click$x, "\ny=", input$plot_click$y)
})
output$plot<- renderPlot({
set.seed(3)
Ex <- xts(1:100, Sys.Date()+1:100)
df = data.frame(Ex,matrix(rnorm(100*3,mean=123,sd=3), nrow=100))
df<-df[,-1]
par(mfrow = c(2,2))
for(i in names(df)){
hist(df[[i]] , main=i,xlab="x",freq=TRUE,label=TRUE,plot = TRUE)
}
})
})
ui.R
#ui.r
mainPanel(
tabsetPanel(type="tab",tabPanel("plot", plotOutput("plot",click = "plot_click"), verbatimTextOutput("info"))
)
The problem with above code is I get random coordinates of the whole plot like this
x=124.632301932263
y=20.4921068342051
instead I want to get coordinates of individual plots with its corresponding values. For example if I click any place in X1's chart I should get x and y coordinates of that chart . How can I do this?
I originally was going to say that this occurs because the click is governed by the pixels of the plot instead of the data, but I am proved wrong here:
Notice that the x and y coordinates are scaled to the data, as opposed to simply being the pixel coordinates. This makes it easy to use those values to select or filter data.
I instead am going to honestly guess that within a graphics device Shiny can't tell the difference between the individual plots, to which a solution would be to create individual devices for each plot:
ui.R
library(shiny)
shinyUI(
tabsetPanel(type="tab",
tabPanel("plot",
uiOutput("coords"),
uiOutput("plots")
)
)
)
server.R
library(xts)
set.seed(3)
Ex <- xts(1:100, Sys.Date() + 1:100)
df <- data.frame(Ex, matrix(rnorm(100*3, mean = 123, sd = 3), nrow = 100))
cn <- colnames(df)
df <- df[, cn[cn != "Ex"]]
n_seq <- seq(ncol(df))
shinyServer(function(input, output, session) {
output$plots <- renderUI({
plot_output_list <- lapply(n_seq, function(i) {
plotOutput(paste0("plot", i), click = paste0("plot_click", i),
height = 250, width = 300)
})
})
for (i in n_seq) {
output[[paste0("plot", i)]] <- renderPlot({
hist(df[[i]] , main = i, xlab = "x", freq = TRUE, label = TRUE)
})
}
output$coords <- renderUI({
coords_output_list <- lapply(n_seq, function(i) {
renderText({
set <- input[[paste0("plot_click", i)]]
paste0("Plot ", i, ": x=", set$x, "\ny=", set$y)
})
})
})
})