2013-04-08 133 views
26

我想創建一個應用程序使用閃亮動態添加圖塊到頁面。它可能是10個地塊,它可能只有一個。我在動態用戶界面的閃亮主頁中使用this tutorial使用閃亮動態地添加圖表到網頁使用閃亮

這是一個簡化的例子。 功能showme被ploting圖

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

,我發現了以下錯誤

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

如果這不是方式 - 我會很感激任何指導。 謝謝。

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

回答

24

也許這個例子中,由於溫斯頓常是有幫助的:

Shiny example app with dynamic number of plots

這裏只是在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

感謝的人 - 這就是我一直在尋找。我設法得到繪製的動態列表,但是,我想打印出一個對象列表 - 每個對象都包含一個標題,一個圖表和一個表格。你知道我該怎麼做? – haki 2013-05-25 09:03:46

+1

你的意思是:a)對於你想要繪製所有三件事物(標題,情節和表格)的每個選定對象,或者b)對於每個選定的對象,然後選擇要繪製哪三個對象意味着別的東西)? – 2013-05-25 10:09:18

+0

a - 對於每個對象我想要一個標題,一個繪圖和一個彙總表。動態用戶界面應該由某種容器而不只是一個情節。我已經嘗試使用'renderTable'向標記列表和輸出添加表格,但它只顯示最後添加的一個表格 - 在我的情況下,表格。 – haki 2013-05-25 14:40:53

4

要回答的評論上面的問題,如何動態地返回對象的列表(例如,一個小區,一個桌子和一個文本),您可以生成一個列表在renderUI中的div標籤中包含適當的輸出,然後您可以在for循環中匹配適當的渲染函數。比如:

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

我希望有幫助!

0

,只要你想使用閃亮的動態加正地塊:

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