【问题标题】:How to render a plot from a list of multiple objects on ShinyApp如何从 ShinyApp 上的多个对象列表中渲染绘图
【发布时间】:2018-04-21 05:09:45
【问题描述】:

我有一个函数 identify_IP(),它返回一个 1- 数据帧列表 2-ggplot。我需要在 ShinyApp 中渲染表和渲染图。这个 shinyApp 代码只呈现数据帧。但我无法渲染情节。有什么帮助吗?

library(shiny)

source('InflectionP2.R', local = TRUE)

runApp(
  list(
    ui = fluidPage(
      titlePanel("Upload your file"),
      sidebarLayout(
        sidebarPanel(
          fileInput('file1', 'Choose xls file',
                    accept = c(".XLS")),

          actionButton("btn", "Update Table"),
          actionButton("btn1", "Display Plot"),

          downloadButton('downloadData', 'Download')
        ),

      mainPanel(
        tableOutput('what'),
        plotOutput('pl'))
    )
    )
    ,

    server = function(input, output, session){

      dataOP <- reactive({
    inFile <- input$file1
    if (is.null(input$file1))
      return(NULL)

  Identify_IP(read.table(inFile$datapath))
  list(tble = df1, txt = inflection_points, plt = p )
  })


  observeEvent(input$btn, output$what <- renderTable({dataOP()$tble}))

  observeEvent(input$btn1, output$pl <- renderPlot({
    plot(dataOP()$plt)
  }))              
     }
      ))

【问题讨论】:

  • 您必须绘制dataOP()$p,例如使用plot()
  • 你能提供更多细节吗?这与我已经做过的有什么不同?
  • 顺便说一句,我不认为该列表有效,因为我尝试了 dataOP()$df1 并且没有呈现表格。所以我选择了 dataOP()
  • 是的,列表可能需要移到reactive({ ... }) 中。试试plot(dataOP()$p) 之类的东西,或者你希望你的情节看起来像什么。
  • 更具体地说:绘图命令需要在renderPlot({ ... })

标签: r list plot render shiny


【解决方案1】:

使用以下 server 为我工作:

server = function(input, output, session){

    dataOP <- reactive({
        inFile <- input$file1
        if(is.null(input$file1)){
            return(NULL)
        }
        #Identify_IP(read.table(inFile$datapath))
        list(tble = c(1:20), txt = c(1:10), plt = rnorm(100))
    })

    observeEvent(input$btn,{
        output$what <- renderTable({
            dataOP()$tble
        })
    })

    observeEvent(input$btn1,{
        output$pl <- renderPlot({
            plot(dataOP()$plt)
        })
    })              
}

请注意,我注释掉了您的函数 Identify_IP 并将结果替换为任意值。 如果这仍然不起作用,您的问题可能与此函数或函数返回的值有关。

【讨论】:

  • 我知道这是非常基本的。但是如何从 fn() 调用 tble、txt 和 plt?是这样的吗? dfs
  • 什么fn()?反应式dataOP 返回一个包含命名元素tblettxtplt 的列表。因此,您例如使用dataOP()$plt 检索plt
  • 是的,你必须用list(tble = dfs$df1 , txt = dfs$inflection_point, plt = dfs$p)替换我的任意值
  • df &lt;- read.table(inFile$datapath) 应该可以用来定义data.frame,我看不出你想用Identify_IP () 做什么
  • Identify_IP() 是我试图在 Shiny 中调用的 R 脚本。它是一个返回dataframe、txt和plot列表的函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-21
  • 2019-04-01
  • 1970-01-01
  • 2018-12-25
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
相关资源
最近更新 更多