Shiny:如何在 Rmd 文档 Shiny 中选择数据框作为输入

Shiny: how to choose a dataframe as input in a Rmd document Shiny

我在选择 Shiny 中的数据帧变量作为输入时遇到问题。

inputPanel(
  selectInput("a", label = "N:",
              choices = c(1,2, 3, 4), selected = 1),
  selectInput("dataframe", label = "dataframe",
              choices = c(A,B), selected = A)
)

在 renderPlot 中我有 input$k 工作正常,因为它是一个简单的数字。

但是,对于 input$dataframe 它不起作用。 我收到错误:non-numeric argument to binary operator.

在此先感谢您的帮助。

selectInput returns 一个字符向量。您可能想给出数据集的名称,然后对其进行评估,而不是尝试传递数据集本身。

来自 selectInput 的帮助:

Server value: A vector of character strings, usually of length 1, with the value of the selected items. When multiple=TRUE and nothing is selected, this value will be NULL.

完整示例:

library(shiny)

ui <- fluidPage(
    selectInput("data",
        "Select dataset",
        choices = c("iris", "mtcars")
    ),
    dataTableOutput("tbl")
)

server <- function(input, output, session) {
    output$tbl <- renderDataTable({
        get(input$data)
    })
}

shinyApp(ui, server)