r - 关于文件上传的更新 Shiny R

标签 r csv shiny

我正在尝试向我的 Shiny 应用程序添加文件上传功能。理想情况下,我希望用户能够上传 csv 文件,然后从下拉菜单中选择他们想要使用的变量。 当应用程序启动时,我最初预加载了一个数据集,下拉列表显示该数据集的正确变量名称。但是,当我尝试上传不同的 csv 文件时,我似乎无法更新下拉列表以显示上传的 csv 中包含的变量。这是我的代码:

服务器.R

library(shiny)
library(ggplot2)

inFile <- mtcars

shinyServer(function(input, output) {

  tableData <- reactive({
    inFile <<- input$file1

    if (!is.null(inFile)){
        read.csv(inFile$datapath, header=input$header, sep=input$sep,
                                                   quote=input$quote)
    }
    else {
        inFile <<- mtcars
    }
  })

  #for x values
  output$opt.x <- renderUI({
         selectInput("xcolumn", "X column to Plot", names(inFile))
  })
})

ui.R

library(shiny)

shinyUI(fluidPage(
     titlePanel("App"),
        sidebarLayout(
            sidebarPanel(
                fileInput('file1', 'Choose CSV File',
                     accept = c(
                            '.csv',
                            '.tsv')
                ),
                uiOutput("opt.x")
             ),
    mainPanel()
))

我还尝试将 output$opt.x 赋值放置在读取文件的 react 函数中,但这似乎没有帮助。

最佳答案

嗨,不要将变量传递给全局环境,而是使用 Shiny 的 react 功能,您的服务器应该是:

library(shiny)
library(ggplot2)

# Test data
# write.csv(x = iris, file = "iris.csv", row.names = FALSE)
# write.csv(x = data.frame(V1 = 1:10, V2 = rnorm(10)), file = "test.csv", row.names = FALSE)

shinyServer(function(input, output) {

  tableData <- reactive({
    inFile <- input$file1

    if (!is.null(inFile)){
      read.csv(inFile$datapath)
    } else {
      mtcars
    }
  })

  #for x values
  output$opt.x <- renderUI({
    selectInput("xcolumn", "X column to Plot", names(tableData()))
  })

 output$your_data <- renderTable({
   head(tableData())
 })
})

并将主面板替换为:

mainPanel(
  tableOutput("your_data")
)

我删除了 read.csv 中的参数,因为这些输入未在 UI 中定义。

编辑

试试这个应用程序,它不是一个 Shiny 的解决方案,而是一个 jquery/js 解决方案(区别在于用户界面)(请参阅 this answer ):

library("shiny")
ui <- fluidPage(
  titlePanel("App"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept = c('.csv','.tsv')
      ),
      tags$script(
        '$( "#file1" ).on( "click", function() {
          this.value = null; 
        });'
      ),
      uiOutput("opt.x")
    ),
    mainPanel(
      tableOutput("your_data")
    )
    )
  )
server <- function(input, output) {

  tableData <- reactive({
    inFile <- input$file1
    if (!is.null(inFile)){
      read.csv(inFile$datapath)
    } else {
      mtcars
    }
  })
  #for x values
  output$opt.x <- renderUI({
    selectInput("xcolumn", "X column to Plot", names(tableData()))
  })

  output$your_data <- renderTable({
    head(tableData())
  })
}
shinyApp(ui = ui, server = server)

为了测试我使用:

write.csv(x = data.frame(V1 = 1:10, V2 = rnorm(10)), file = "test.csv", row.names = FALSE)
write.csv(x = data.frame(V11 = 11:20, V12 = rnorm(10), ABC = letters[1:10]), file = "test.csv", row.names = FALSE)

关于r - 关于文件上传的更新 Shiny R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29378493/

相关文章:

r - 对组学/生物统计学中的重复数据进行平均

r - R中的非线性逻辑回归包

mysql - 第 1 行不包含所有列的数据

Python DictWriter : why won't python quickly return my output file? 使用计数器、csv 和 group-by

R - 如何在 Shiny 中使用 selectInput 来更改 ggplot 渲染图中的 x 和填充变量?

r - 在 ggplot2 中添加构面标题和更改图例标题

R sink() 消息并输出到同一文件 - 完整性检查

objective-c - CSV 文件查找 - Objective C

r - 访问 rshiny 中动态生成的输入

r - 如何将数据从 Shiny 下载到多张纸上?