如何将数据帧列表呈现为表格以在 Shiny 中显示为输出

How to render a list of dataframes as tables to show as output in Shiny

我在一个闪亮的应用程序中工作,根据用户定义的输入来比较多个项目。代码工作正常,但我有一个问题。我不知道我应该应用什么功能才能将某些计算的结果显示为应用程序右侧的表格。接下来是应用程序的代码:

library(shiny)
library(shinydashboard)

#Function

compute <- function(firstitem,seconditem)
{
  Sum <- firstitem+seconditem
  Difference <- firstitem+seconditem
  Product <- firstitem*seconditem
  Ratio <- firstitem/seconditem
  Res <- data.frame(C1=Sum,C2=Difference,C3=Product,C4=Ratio)
  return(Res)
}

#App

ui = shinyUI(fluidPage(
  
  titlePanel("Compare"),
  
  sidebarLayout(
    sidebarPanel(
      numericInput("numitems", label = "Number of items to compare?",
                   min = 1, max = 5, value = 1),
      uiOutput("period_cutpoints"),
      uiOutput("period_cutpoints2"),
      actionButton("submit", "Submit")
    ),
    mainPanel(
      textOutput("numitems"),
      textOutput("cutpoints")
    )
  )
))

server = shinyServer(function(input, output, session) {
  
  output$period_cutpoints<-renderUI({
    req(input$numitems)
    lapply(1:(input$numitems), function(i) {
      numericInput(inputId=paste0("firstitem",i), 
                   label=paste0("Enter the value of first item ", i, ":"),value = 0)
    })
  })
  
  output$period_cutpoints2<-renderUI({
    req(input$numitems)
    lapply(1:(input$numitems), function(i) {
      numericInput(inputId=paste0("seconditem",i), 
                   label=paste0("Enter the value of second item ", i, ":"),value = 0)
    })
  })
  
  seldates <- reactiveValues(x=NULL)
  observeEvent(input$submit, {
    seldates$x <- list()
    lapply(1:(input$numitems), function(i) { 
      seldates$x[[i]] <- compute(firstitem = input[[paste0("firstitem", i)]],seconditem = input[[paste0("seconditem", i)]])
    })
  })
  
  output$cutpoints <- renderText({as.character(seldates$x)})
  
  
})

shinyApp(ui = ui, server = server)

它正在工作,但我的问题是我不知道如何设置 seldates 的内容,它们是数据帧,作为应该一个接一个出现的表格。此任务是使用 output$cutpoints 完成的,但我无法将它们作为表获取:

有人知道我该如何解决这个问题吗?非常感谢!

试试这个

library(shiny)
library(shinydashboard)
library(DT)
#Function

compute <- function(firstitem,seconditem)
{
  Sum <- firstitem+seconditem
  Difference <- firstitem+seconditem
  Product <- firstitem*seconditem
  Ratio <- firstitem/seconditem
  Res <- data.frame(C1=Sum,C2=Difference,C3=Product,C4=Ratio)
  return(Res)
}

#App

ui = shinyUI(fluidPage(
  
  titlePanel("Compare"),
  
  sidebarLayout(
    sidebarPanel(
      numericInput("numitems", label = "Number of items to compare?",
                   min = 1, max = 5, value = 1),
      uiOutput("period_cutpoints"),
      uiOutput("period_cutpoints2"),
      actionButton("submit", "Submit")
    ),
    mainPanel(
      textOutput("numitems"),
      textOutput("cutpoints"),
      uiOutput("t1")
    )
  )
))

server = shinyServer(function(input, output, session) {
  
  output$period_cutpoints<-renderUI({
    req(input$numitems)
    lapply(1:(input$numitems), function(i) {
      numericInput(inputId=paste0("firstitem",i), 
                   label=paste0("Enter the value of first item ", i, ":"),value = i)
    })
  })
  
  output$period_cutpoints2<-renderUI({
    req(input$numitems)
    lapply(1:(input$numitems), function(i) {
      numericInput(inputId=paste0("seconditem",i), 
                   label=paste0("Enter the value of second item ", i, ":"),value = i+i)
    })
  })
  
  seldates <- reactiveValues(x=NULL)
  observeEvent(input$submit, {
    seldates$x <- list()
    lapply(1:(input$numitems), function(i) { 
      seldates$x[[i]] <- compute(firstitem = input[[paste0("firstitem", i)]],seconditem = input[[paste0("seconditem", i)]])
    })
  })
  
  output$cutpoints <- renderText({as.character(seldates$x)})
  
  observeEvent(input$submit, {
    
    lapply(1:(input$numitems), function(i) { 
      output[[paste0("table",i)]] <- renderDT(seldates$x[[i]])
    })
    
    output$t1 <- renderUI({
      tagList(
        lapply(1:(input$numitems), function(i) { 
          DTOutput(paste0("table",i))
        })
      )
    })
    
  })
  
})

shinyApp(ui = ui , server = server)