使用 ShinyFiles 加载数据文件

Tim*_* Ka 2 r shiny

我的 Shiny 应用程序应该加载几个数据文件。为了实现这一点,我尝试使用ShinyFiles. 来自ui.R

shinyFilesButton('file', 'Load Dataset', 'Please select a dataset', FALSE)

但是,我不确定要放入什么server.R来加载文件。我知道如何获取文件路径等等,但是我应该把load()命令放在哪里?

这就是我现在尝试的:(来自server.R):

observeEvent(input$file, {
 inFile <- parseFilePaths(roots=roots, input$file)
 load(as.character(inFile$datapath), envir=.GlobalEnv)
})
Run Code Online (Sandbox Code Playgroud)

这些文件是save.image()由另一个 R 脚本保存的数据文件,其中包含一些由另一个 R 脚本生成的数据框、矩阵和列表。在我的 Shiny 应用程序中,我想将数据主要用于图形,因此我需要在应用程序运行时加载它们。

Bat*_*hek 5

It s hard to understand what means "Shiny seems not to use the contents"

See example ( -- I have object "y" in my data.)

UI

shinyUI(

  fluidPage(    
    shinyFilesButton('file', 'Load Dataset', 'Please select a dataset', FALSE),
    textOutput("txt")

  )
)
Run Code Online (Sandbox Code Playgroud)

Server

shinyServer(function(input, output,session) {
  shinyFileChoose(input,'file', session=session,roots=c(wd='.'))

  observeEvent(input$file, {
    inFile <- parseFilePaths(roots=c(wd='.'), input$file)
    load(as.character(inFile$datapath), envir=.GlobalEnv)
    })

  output$txt=renderPrint({
  input$file
  if(exists("y")) y})

  })
Run Code Online (Sandbox Code Playgroud)

Text changed from data.

for simplisity you can use reactiveValues like

shinyServer(function(input, output,session) {
  shinyFileChoose(input,'file', session=session,roots=c(wd='.'))
  envv=reactiveValues(y=NULL)
  observeEvent(input$file, {
    inFile <- parseFilePaths(roots=c(wd='.'), input$file)
    load(as.character(inFile$datapath))
    envv$y=y
    })

  output$txt=renderPrint({envv$y})

  })
Run Code Online (Sandbox Code Playgroud)

Both variants work but second better if you need different data in sessions.