【问题标题】:Save the contents of a local csv file as a vector in Shiny将本地 csv 文件的内容保存为 Shiny 中的矢量
【发布时间】:2017-09-07 13:47:18
【问题描述】:

我的 UI 中有一个下拉菜单:

selectInput(inputId = "Header", label = "label", 
            choices = x, 
            selected = NULL),

我希望上面示例中的变量“x”是一个包含大约 100 个不同选项的字符向量。我将这些选项保存在 .csv 文件中,但我无法将 .csv 文件(我已上传到我的目录)中的数据保存为矢量。我应该如何将本地 .csv 文件中的数据保存到 R Shiny 中以用作菜单选项?我一直在尝试 read.csv 以及其他功能,但到目前为止没有运气。

我只需要手动复制它们吗?我想有一个更简单的方法。

【问题讨论】:

  • 只需调用列名即可。例如,choices = df$COL1,其中df 是您的csv 数据集。

标签: r shiny


【解决方案1】:
Example data
write.table(rownames(mtcars), "mtcars.csv",
            row.names = FALSE, col.names = FALSE, quote = FALSE)

library(shiny)

# User interface that lets you to select input
ui <- fluidPage(
    selectInput("mySelection", "Select input", "")
)

server <- function(input, output, session) {
    observe({
        # Load your csv file
        x <- read.csv("mtcars.csv", header = FALSE)

        # Update selection mySelection (passed to UI)    
        updateSelectInput(session, "mySelection",
            label = "Select input",
            choices = x[, 1],
            selected = x[, 1][1]
        )
    })
}

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多