2013-04-08 7 views
26

hinzufügen Ich möchte eine App mit glänzenden erstellen, die Diagramme auf der Seite dynamisch hinzufügen. es könnte 10 Grundstücke sein und es könnte nur eins sein. Ich verwende this tutorial in der glänzenden Homepage für dynamische UI.dynamisch Plots auf Web-Seite mit glänzenden

Dies ist ein vereinfachtes Beispiel. die Funktion showme ist ploting die Grafik

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") 
))) 

Ich erhalte die folgende Fehler

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

wenn dies nicht der Fall ist Der Weg - ich würde jede Führung zu schätzen wissen. Danke.

> sessionInfo() 
R version 2.15.3 (2013-03-01) 
Platform: i386-w64-mingw32/i386 (32-bit) 

Antwort

24

Vielleicht ist dieses Beispiel wegen Winston Chang ist hilfreich:

Shiny example app with dynamic number of plots

Hier die vollständige Beispiel ist nur bei 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 
    ) 
)) 
+1

Dank Mann - das ist das, was ich suchte. Es ist mir gelungen, eine dynamische Liste von gezeichneten Plots zu erhalten, aber ich möchte eine Liste von Objekten ausdrucken - jedes Objekt enthält eine Kopfzeile, ein Diagramm und eine Tabelle. Weißt du wie ich das machen kann? – haki

+1

Meinst du, dass a) für jedes gewählte Objekt alle drei Dinge (Kopf, Plot und Tabelle) geplottet werden sollen, oder b) für jedes gewählte Objekt, dann wähle, welche dieser drei Plots (oder du) meinst du noch etwas anderes)? –

+0

a - Für jedes Objekt möchte ich eine Kopfzeile, ein Diagramm und eine Übersichtstabelle. Die dynamische UI sollte von einem Container irgendeiner Art und nicht nur von einem Plot sein. Ich habe versucht, Tabellen zur Tagliste und zur Ausgabe hinzuzufügen, indem ich 'renderTable' verwende, aber es wird nur die letzte hinzugefügt - in meinem Fall die Tabelle. – haki

4

Um die Frage in dem Kommentar oben zu beantworten, wie dynamisch eine Liste von Objekten zurückkehren (zum Beispiel ein Grundstück, ein Tisch und einen Text), können Sie eine Liste erzeugen der entsprechenden Ausgaben, die in einem div-Tag in der renderUI enthalten sind, stimmen Sie dann mit den entsprechenden Renderfunktionen in der for-Schleife überein. Zum Beispiel:

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="") 
    plottitle <- paste("plottitle", i, sep="") 
    tablename <- paste("tablename", i, sep="") 
    tags$div(class = "group-output", 
     textOutput(plottitle, container = h3), 
     plotOutput(plotname, height = 280, width = 250), 
     tableOutput(tablename) 
    ) 
    }) 

    # 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="") 
     plottitle <- paste("plottitle", my_i, sep="") 
     tablename <- paste("tablename", 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 = "")) 
     }) 
     output[[plottitle]] <- renderText({paste("1:", my_i, ". n is ", input$n, sep = "") 
     }) 
     output[[tablename]] <- renderTable({table(x = 1:my_i, y = 1:my_i) 
     }) 
    }) 
} 
}) 

Ich hoffe, dass hilft!

0

dynamisch N Plots hinzufügen, wie Sie wollen glänzend mit:

runApp(
list(
ui = fluidPage(
    headerPanel('Tittle'), 

    sidebarPanel(
    fileInput('file1', 'Choose File:'), 
    textInput("target", label="Target", value="Choose target"), 
    actionButton("addButton", "Go!"), 
    p('The button start the code server'), 
    p("This is UI") 

), 

    mainPanel(

    uiOutput("plots") 
)), 
#SERVER 
server = function(input, output) {  
    dataset <- eventReactive(input$addButton, { 

    #Obtain the file and textinput 
    inFile <- input$file1 
    df <- read.csv(inFile$datapath) 
    df$target <- input$target 
    return(df) 

    }) 

    output$plots <- renderUI({ 


    df <- dataset() 
    n <- df$target[1] 

    plot_output_list <- lapply(1:n, function(i) { 

     plotname <- paste("plot", i, sep="") 
     plotOutput(plotname, height = 580, width = 550) 


    }) 


    do.call(tagList, plot_output_list) 


    }) 
    observe({    

    for (i in 1:length()) { 
     local({ 


     plotname <- paste("plot", i, sep="") 

     output[[plotname]] <- renderPlot({ 

      #function_plot is the function generate plot 
      function_plot() 

     }) 
     })#endlocal 
    } 

    }) 
} 
) 
) 

https://github.com/ericbellet/recomendacion-modelos/blob/master/shiny/generate_rocSHINY.R

Verwandte Themen