2013-04-08 113 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