简体   繁体   English

为什么这个 Shiny App 不使用 RStudio 显示数据框?

[英]Why does this Shiny App not display dataframe using RStudio?

I have a list of data frames, ls_df , comprising two dataframes from the datasets package.我有一个数据框列表ls_df ,其中包含来自datasets包的两个数据框。

I am trying to load these two dataframes into a Shiny app using the code below.我正在尝试使用下面的代码将这两个数据帧加载到 Shiny 应用程序中。 However, it does not work, with the error message no item called "ls_df" on the search list being returned.但是,它不起作用,错误消息no item called "ls_df" on the search list Does anyone know how to fix?有谁知道如何解决?

ls_df <- list(datasets::airmiles,
datasets::AirPassengers)


ui <- fluidPage(
  selectInput("ls_df", label = "Dataset", choices = ls("ls_df")),
  verbatimTextOutput("summary"),
  tableOutput("table")
)

server <- function(input, output, session) {
  output$summary <- renderPrint({
    dataset <- get(input$ls_df, "ls_df")
    summary(dataset)
  })
  
  output$table <- renderTable({
    dataset <- get(input$ls_df, "ls_df")
    dataset
  })
}
shinyApp(ui, server)

The list needs the names:该列表需要名称:

library(shiny)
ls_df <- list(airmiles=datasets::airmiles,AirPassengers=datasets::AirPassengers)

ui <- fluidPage(
  selectInput("ls_df", label = "Dataset", choices = names(ls_df)),
  verbatimTextOutput("summary"),
  tableOutput("table")
)

server <- function(input, output, session) {
  output$summary <- renderPrint({
    dataset <- ls_df[[input$ls_df]]
    summary(dataset)
  })
  
  output$table <- renderTable({
    dataset <- ls_df[[input$ls_df]]
    dataset
  })
}
shinyApp(ui, server)

Two things wrong:错了两点:

  1. Your list needs names, as discussed in PorkChop's answer.您的列表需要名称,如 PorkChop 的回答中所述。 If this were the only required change, then PorkChop's answer would suffice.如果这是唯一需要的更改,那么 PorkChop 的回答就足够了。

  2. get(input$ls_df, "ls_df") is an error. get(input$ls_df, "ls_df")是一个错误。 This should be rather clear, though, since it prevents the shiny interface from starting.不过,这应该很清楚,因为它会阻止闪亮的界面启动。 This error is because the envir= argument of ls and get require an object , not the character name of an object.这个错误是因为lsgetenvir=参数需要一个对象,而不是对象的character名。 (One could go "inception" and use ls(get("ls_df")) and similarly for get , but that hardly seems necessary or useful.) 可以去“开始”并使用ls(get("ls_df"))和类似的get ,但这似乎几乎没有必要或有用。)

     ls_df <- list(airmiles=datasets::airmiles, # <-- named list AirPassengers=datasets::AirPassengers) ui <- fluidPage( selectInput("ls_df", label = "Dataset", choices = ls(ls_df)), # <-- no quotes verbatimTextOutput("summary"), tableOutput("table") ) server <- function(input, output, session) { output$summary <- renderPrint({ dataset <- get(input$ls_df, ls_df) # <-- no quotes summary(dataset) }) output$table <- renderTable({ dataset <- get(input$ls_df, ls_df) # <-- no quotes dataset }) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM