Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamically add plots to web page using shiny

Tags:

r

rstudio

shiny

i want to create an app using shiny that dynamically add plots to the page. it could be 10 plots and it could be only one. i'm using this tutorial in the shiny homepage for dynamic UI.

this is a simplified example. the function showme is ploting the graph

server.r

shinyServer(function(input, output) {   # Create an environment for storing data   symbol_env <- new.env()   # Make a chart for a symbol, with the settings from the inputs   make_chart <- function(symbol) {     showme(symbol)   }    display <- c("1083484" , "1101732")    output$MyList <- renderUi({      for (i in i:nrow(display))        renderPlot({make_chart(display[i])})     }) }) 

ui.r

shinyUI(pageWithSidebar(   headerPanel("My Plots !"),   sidebarPanel(     wellPanel(       p(strong("Scan1"))))  ,mainPanel(       uiOutput("MyList") ))) 

i'm getting the following error

Listening on port 8100 Error in .subset2(x, "impl")$defineOutput(name, value, deparse(substitute(value))) :    Unexpected character output for display 

if this is not the way - i would appreciate any guidance. Thanks.

> sessionInfo() R version 2.15.3 (2013-03-01) Platform: i386-w64-mingw32/i386 (32-bit) 
like image 231
haki Avatar asked Apr 08 '13 09:04

haki


1 Answers

Perhaps this example due to Winston Chang is helpful:

Shiny example app with dynamic number of plots

Here is the full example just in case of linkrot:

server.R

max_plots <- 5  shinyServer(function(input, output) {  # Insert the right number of plot output objects into the web page output$plots <- renderUI({   plot_output_list <- lapply(1:input$n, function(i) {      plotname <- paste("plot", i, sep="")      plotOutput(plotname, height = 280, width = 250)    })     # Convert the list to a tagList - this is necessary for the list of items    # to display properly.    do.call(tagList, plot_output_list) })  # Call renderPlot for each one. Plots are only actually 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 renderPlot() will be the same across all instances, because     # of when the expression is evaluated.     local({         my_i <- i         plotname <- paste("plot", my_i, sep="")          output[[plotname]] <- renderPlot({         plot(1:my_i, 1:my_i, xlim = c(1, max_plots), ylim = c(1, max_plots), main = paste("1:", my_i, ".  n is ", input$n, sep = ""))         })     }) } }) 

ui.R

shinyUI(pageWithSidebar(    headerPanel("Dynamic number of plots"),      sidebarPanel(       sliderInput("n", "Number of plots", value=1, min=1, max=5)     ),      mainPanel(       uiOutput("plots") # This is the dynamic UI for the plots     ) )) 
like image 58
Rahul Savani Avatar answered Oct 16 '22 07:10

Rahul Savani