使用shiny动态向网页添加图表

34

我希望用Shiny创建一个应用程序,动态地向页面添加图表。它可以是10个图表,也可以只有一个。我正在使用Shiny首页上的这个教程来实现动态UI。

这是一个简化的例子。 函数showme绘制了图形。

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)
3个回答

31

也许这个由Winston Chang提供的示例会有所帮助:

使用动态数量的图表的Shiny示例应用程序

以下是完整示例(以防链接失效):

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
1
您的意思是a) 对于每个选择的对象,您想绘制所有三个内容(标题、图表和表格),还是b) 对于每个选择的对象,您要选择要绘制其中哪一个(或者您是指其他内容)? - Rahul Savani
3
这似乎在ggplot中无法工作,有什么想法吗? - evolvedmicrobe
1
@RahulSavani:你能把 max_plots 变成一个反应式值,而不是硬编码吗?谢谢。 - TTT
@RahulSavani 我需要 b。 (在此处提出了一个专门的问题:https://dev59.com/YInda4cB1Zd3GeqPGf3v) - Uri Barenholz
显示剩余3条评论

6
为了回答上面评论中的问题,关于如何动态返回对象列表(例如,绘图、表格和文本),您可以在renderUI中生成一个包含适当输出的列表,并将其放置在一个div标签中,然后在for循环中与相应的render函数匹配。例如:
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)
        })
    })
}
})

我希望你能明白这些内容!

1
动态使用shiny添加N个图表:
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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接