How to arrange or align the output of renderUI - r

I am learning how to use renderUI to dynamically generate multiple plots. Here is an example app I designed (https://yuchenw.shinyapps.io/Format_UI_Example/). The idea is to design an app that allows users to select one or more parameters in the mtcars data set and plot the row index and the value as a scatter plot dynamically.
The example app works, but all the plots are aligned in one column. As the users selected more parameters, the number of plots increases, and the length of the web page also increases. In addition, there are lots of white space. If possible, I would like to arrange or align the multiple plots as a two columns or three columns structure to reduce the length of the web page and to reduce the white space.
I usually used the column function and set the width argument to achieve this. But I don't how to do it using renderUI. I would appreciate any help.
Here is the code.
### This script creates an R shiny app that plot mpg, disp, and hp, from the mtcars data set
# Load packages
library(shiny)
library(tidyverse)
# Load data
data("mtcars")
# Add row id
mtcars2 <- mtcars %>% mutate(ID = 1:n())
# ui
ui <- fluidPage(
sidebarPanel(
selectInput(inputId = "sel", label = "Select one or more parameters",
choices = names(mtcars), multiple = TRUE)
),
mainPanel(
uiOutput("plots")
)
)
# server
server <- function(input, output, session){
# Create plot tag list
output$plots <- renderUI({
plot_output_list <- lapply(input$sel, function(par) {
plotname <- paste("plot", par, sep = "_")
plotOutput(plotname)
})
do.call(tagList, plot_output_list)
})
# Dynamically generate the plots based on the selected parameters
observe({
req(input$sel)
lapply(input$sel, function(par){
output[[paste("plot", par, sep = "_")]] <- renderPlot({
ggplot(mtcars2, aes_string(x = "ID", y = par)) +
geom_point() +
ggtitle(paste("Plot: ", par))
},
width = 250,
height = 250)
})
})
}
# Run app
shinyApp(ui, server)

Try this :
plotOutput(plotname, height = '250px', inline=TRUE)
It will give you the following output:

Related

How to create a plot in shiny combining x number of CSV inputs?

I'm trying to create a dashboard that accepts any number of CSV files, combines them together (probably using bind_rows), wrangles the code, and then creates a plot with data from each file being represented as a separate geom_line.
So far I have found code that allows the generation of the UI, but I'm stuck as to how to get the data from those inputs into a reactive tibble that I can then plot.
library(shiny)
library(purrr)
ui <- fluidPage(
numericInput("n", "Number of files", value = 5, min = 1),
uiOutput("file"),
)
server <- function(input, output, session) {
file_names <- reactive(paste0("file", seq_len(input$n)))
output$file <- renderUI({
map(file_names(), ~ fileInput(.x, NULL,
accept = c('text/csv', 'text/comma-separated-values', '.csv')))
})
}
shinyApp(ui = ui, server = server)
Any help appreciated.
library(shiny)
library(purrr)
library(readr)
library(ggplot2)
library(dplyr)
# create some dummy data
map(1:2, ~ tibble(x = 1:10, y = x + .x) |> write_csv(paste0("file", .x, ".csv"))) |>
invisible()
ui <- fluidPage(
numericInput("n", "Number of files", value = 2, min = 1),
uiOutput("file"),
plotOutput("plot", width = "20%")
)
server <- function(input, output, session) {
file_names <- reactive(replicate(input$n, paste0(sample(c(0:9, letters), 9), collapse = '')))
output$file <- renderUI({
map(file_names(), ~ fileInput(.x, NULL,
accept = c('text/csv', 'text/comma-separated-values', '.csv')))
})
df <- reactive({
map(isolate(file_names()), ~ req(input[[.x]]$datapath))
map(seq_along(isolate(file_names())),
~ input[[file_names()[.x]]]$datapath |>
read_csv() |>
mutate(class = as.factor(.x))
) |>
bind_rows()
})
output$plot <- renderPlot({
req(df())
ggplot(df()) +
geom_line(aes(x, y, color = class))
})
}
shinyApp(ui = ui, server = server)
One problem with creating dynamic number of file input is that even if you change the n, once you have uploaded some files in the previous round, in the next round (after you change n), previous file upload information is still there. This is because you didn't change the input ID for the next round. To avoid so, first we need to make sure file upload IDs are unique each time you change n. This is done by replicate(... sample(.... This is a very simple implementation of unique IDs. To use a more robust method, read UUID.
We use req to make sure all file inputs have files uploaded
Read individual files into memory and give them a unique class column value so we can plot them as different groups on plot.
bind all rows to a tibble.
plot them all. use class as a color marker to distinguish data from different files.

Dynamic number of Plots with reactive data in R Shiny

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:

How to render ggplot from a function that returns a list of multiple objects in Shiny

I have an r script includes a Identify_IP() that returns a list of dataframe and a ggplot. I want to call the script and render both the dataframe and the plot.
This is Identify_IP() function. I took off unrelative code and kept only the plot, lines and ggplot code to give a clear example of my type of ggplot.
library(ggplot2)
library(matrixStats)
library(fda.usc)
#df <- read.table("name.XLS", header = FALSE)
Identify_IP = function(df1){
mlearn <- df1[,'V7']
formul <- plot(blue_curve$x, blue_curve$y * 30, type = 'l', col = 'blue')
formula_deriv <- lines(blue_curve$x, red_curve$y1 * 30, col = 'red')
p <- ggplot(df1, aes(blue_curve$x)) +
geom_line(aes(y = blue_curve$y, colour = "0 Deriv")) +
geom_line(aes(y = red_curve$y1, colour = "1st Deriv")) +
geom_vline(xintercept = x_loc) + geom_hline(yintercept = 0)
return(list(df1,p))
}
Now, this is a modified Shiny code based on amrr and micstr suggestion.
source('InflectionP2.R', local = TRUE)
library(ggplot2)
library(shiny)
runApp(
list(
ui = fluidPage(
titlePanel("Upload your file"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xls file',
accept = c(".XLS")),
actionButton("btn", "Update Table"),
actionButton("btn1", "Display Plot")
),
mainPanel(
tableOutput('what'),
plotOutput('pl'))
)
)
,
server = function(input, output, session){
dataOP <- reactive({
inFile <- input$file1
if (is.null(input$file1))
return(NULL)
dfs <- Identify_IP(read.table(inFile$datapath))
return(dfs)
})
observeEvent(input$btn, output$what <- renderTable({
dataOP()[[1]]
}))
observeEvent(input$btn1, output$pl <- renderPlot({
pp <- dataOP()
pp[[2]]
}))
}))
This was really helpful in teaching me how to call r script in reactive(). And it makes sense to me. Yet, it render the table but the Display Plot button is not rendering the plot. Does my ggplot in Identify_IP function has anything to do with not being able to display the plot? I also tried print(ggplot(pp[[2]])) and still the same.
I managed to get this working.
Note I used the internal data set iris and made a toy Identify_IP function as I do not have your code.
Note you still need to choose a file to trigger the events but it will ignore that file and use iris data.
Workaround I used [[1]] to get the table not dataOP()$tble
CODE
library(shiny)
library(ggplot2)
# source('InflectionP2.R', local = TRUE)
# MAKE TEST FUNCTION
Identify_IP <- function(mydata) {
#shrink data
tble <- head(mydata)
plt <- ggplot(data = head(mydata),
mapping = aes(y = Sepal.Length,
x = Petal.Length)) + geom_point()
return(list(tble, plt))
}
runApp(
list(
ui = fluidPage(
titlePanel("Upload your file"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xls file',
accept = c(".XLS")),
actionButton("btn", "Update Table"),
actionButton("btn1", "Display Plot")
),
mainPanel(
tableOutput('what'),
plotOutput('pl'))
)
)
,
server = function(input, output, session){
dataOP <- reactive({
inFile <- input$file1
if (is.null(input$file1))
return(NULL)
# ORIGINAL dfs <- Identify_IP(read.table(inFile$datapath))
# using internal dataset for example
dfs <- Identify_IP(iris)
# ORIGINAL list(tble = dfs, plt = dfs)
# lets just return your dfs, its already a list in code above
return(dfs)
})
observeEvent(input$btn, output$what <- renderTable({
#print(dataOP()) # debug line that led to [[1]] idea
# ORIGINAL dataOP()$tble
# just say first in list
dataOP()[[1]]
}))
observeEvent(input$btn1, output$pl <- renderPlot({
#ggplot(dataOP()$plt)
# since already a plot just need to index it
# I found [[2]] worked better than explicit dataOP()$plt
pp <- dataOP()
pp[[2]]
}))
}))
RESULT
Voila!
1) Try print (ggplot(dataOP()$plt))
Take a look at this answer I wrote.
2) Sorry its hard to interpret without your ggplot code bit and data. Given #amrrs questions can you try debug in your Shiny code with print() and str() temporary lines to see what your data is returning. i.e.
print(dataOP()$plt)
str(dataOP())
Worse case, try split your code in two. So Identify_IP code to do the data leg and then make a Print_IP with the ggplot code that just returns the plot. It might rule out your chart is not the problem.
3) Take a look at reactiveValues()
https://shiny.rstudio.com/reference/shiny/0.11/reactiveValues.html
It "bakes" a result that was reactive. The type coming out of your chart may be a reactive type not a chart type. Perhaps share any error messages you are getting.

Dynamically choose inputs based on reactive subset data in shiny

Setup: I already have build a shiny-app with two plots. I used the flexdashboard-package to create two plots in two tabs. In addition I programmed the whole shiny-app in R-markdown.
Now I want to create an interface where the user can subset the data. That part itself works. However I also need to perform some calculations with the subsetted data, before I do my two plots.
Is there any way I can transform some subsetted object like mydata to a dataframe? My problem is that I need to use this subsetted object also in the UI part of the other plots.
EDIT: I specifically need some way to transport my selection from checkboxGroupInput to selectInput("cat_1"," category 1:",choices = levels(mydata()$mycat).
### 1. Create some sample data
myrows<-sample(letters,12)
exdata<- data.frame(mycat=rep(myrows,2),yr=rep(1:2,each=12),KPI_1=rnorm(24),
KPI_2=round(runif(24,1,20)),KPI_3=rbinom(24,6,0.5))
### 2. UI part
fluidPage(fluidRow(
checkboxGroupInput("comp", "Categories",myrows,myrows,inline=TRUE),
actionButton("go", "Update"),
textOutput("txt"),
tableOutput("head"))
)
### 3. Server part
mydata<-eventReactive(input$go,{
res<-subset(exdata,mycat%in%input$comp)
return(res)
})
output$txt <- renderText({
paste("You chose", paste(input$comp, collapse = ", "))
})
output$head <- renderTable({
mydata()
})
In the next chunk I do this:
library(plotly)
library(shiny)
### 4. UI part of my plot
fluidRow(sidebarLayout(sidebarPanel(
selectInput("cat_1",
" category 1:",
choices = levels(mydata()$mycat),
selected = levels(mydata()$mycat)[1]),
selectInput("cat_2",
" category 2:",
choices = levels(mydata()$mycat),
selected = levels(mydata()$mycat)[2])),
mainPanel(plotlyOutput("plot3", height = 300, width = 700))))
### 5. Server part of my plot
output$plot3 <- renderPlotly({
## 5.1 Create plot data
cat1<-input$cat_1
cat2<-input$cat_2
y1<-as.numeric(mydata()[mydata()$mycat==cat1])
y2<-as.numeric(mydata()[mydata()$mycat==cat2])
x0<-c(1,2)
## 5.2 Do plot
plot_ly(x = x0,y = y1, type="scatter",mode='lines+markers',name=Firm1) %>%
add_trace(y = y2, name = Firm2, mode = 'lines+markers') %>%
layout(dragmode = "select")
It took me a while to figure out your code. So:
1) Make use of renderUI which will allow you to dynamically create controls
2) Stick with one ui
3) Make sure you understand the renderPlotly and what you're trying to plot
library(shiny)
library(plotly)
### 1. Create some sample data
myrows<-sample(letters,12)
exdata<- data.frame(mycat=rep(myrows,2),yr=rep(1:2,each=12),KPI_1=rnorm(24),
KPI_2=round(runif(24,1,20)),KPI_3=rbinom(24,6,0.5))
ui <- fluidPage(
sidebarPanel(
uiOutput("c1"),uiOutput("c2")),
mainPanel(
column(6,
checkboxGroupInput("comp", "Categories",myrows,myrows,inline=TRUE),
actionButton("go", "Update"),
textOutput("txt"),
tableOutput("head")),
column(6,
plotlyOutput("plot3", height = 300, width = 700)))
)
server <- function(input, output) {
### 3. Server part
mydata <- eventReactive(input$go,{
res<-subset(exdata,mycat%in%input$comp)
return(res)
})
output$txt <- renderText({
paste("You chose", paste(input$comp, collapse = ", "))
})
output$head <- renderTable({
mydata()
})
conrolsdata <- reactive({
unique(as.character(mydata()$mycat))
})
output$c1 <- renderUI({
selectInput("cat_1", "Variable:",conrolsdata())
})
output$c2 <- renderUI({
selectInput("cat_2", "Variable:",conrolsdata())
})
output$plot3 <- renderPlotly({
if(is.null(input$cat_1)){
return()
}
y1<- mydata()$KPI_1[as.character(mydata()$mycat) %in% input$cat_1]
y2<- mydata()$KPI_2[as.character(mydata()$mycat) %in% input$cat_2]
x0<-c(1,2)
#use the key aesthetic/argument to help uniquely identify selected observations
plot_ly(x = x0,y = y1, type="scatter",mode='lines+markers',name="Firm1") %>%
add_trace(y = y2, name = "Firm2", mode = 'lines+markers') %>%
layout(dragmode = "select")
})
}
shinyApp(ui, server)

add_tooltip not working in ggvis in R

I want to add tooltip containing all variables.
But when I use this code I get following error:
Error in handlers$add(handler, key, tail) : Key / already in use
If I don't use add_tooltip the plot is created without any problem.
(The add_tooltip is near the bottom of server.R)
Please help, I am really frustrated.
I am creating a Shiny application with following ui and server:
ui.R:
library(ggplot2)
library(ggvis)
library(shiny) # load shiny at beginning at both scripts
shinyUI(fluidPage( # standard shiny layout, controls on the
# left, output on the right
titlePanel("Relative Velocity vs Distance Gap"), # give the interface a title
sidebarLayout(position="right",
sidebarPanel( # all the UI controls go in here
radioButtons(inputId = 'dfid', label = h4("Select Data:"),
choices = c("Coordinate Approach",
"Sum Approach")),
selectInput(inputId="vid", label=h4("Select Vehicle ID:"), choices = vehid)
),
mainPanel( # all of the output elements go in here
h3("Plot"), # title with HTML helper
plotOutput("plot") # this is the name of the output
# element as defined in server.R
)
)
))
server.R:
library(ggvis)
library(shiny) # load shiny at beginning at both scripts
shinyServer(function(input, output) { # server is defined within
# these parentheses
new.data <- reactive({switch(input$dfid, "Coordinate Approach"=df1, "Sum Approach"=df2)})
output$plot <- renderPlot({
new.data <- subset(new.data(), new.data()$Vehicle.ID==input$vid)
tittle <- unique(new.data$Vehicle.class)
mtc <- new.data
mtc$id <- 1:nrow(mtc)
all_values <- function(x) {
if(is.null(x)) return(NULL)
row <- mtc[mtc$id == x$id, ]
paste0(names(row), ": ", format(row), collapse = "<br />")
}
mtc %>% ggvis(x = ~relative.v, y = ~gap.dist, key:=~id) %>%
layer_points() %>%
add_tooltip(all_values, "hover")
#ggplot(data= new.data, mapping = aes(x=relative.v, y=gap.dist, color=as.factor(p))) +
# geom_point() + ggtitle(tittle) + labs(x='Relative Velocity (ft/s)', y='Gap (feet)') + theme_bw() #+ my.theme()
})
})
Another unrelated problem is that when I run the app, the shiny app runs in a window but ggvis plot is created in Viewer pane.
How can I render the plot in window within the Shiny app?

Resources