在。

时间:2017-08-24 01:59:39

标签: r shiny

我想通过Shiny App用户访问和使用上传的.Rdata文件中的多个对象。 可以通过简单的调用load()中的global.R来访问存储在.Rdata中的多个对象,但我无法弄清楚如何在{{1}时访问和使用这些对象文件已上传。

模仿this related question where the .Rdata file contains only one object.

的可重复示例
.Rdata

在上传library(shiny) # Define several objects and store them to disk x <- rnorm(100) y <- rnorm(200) z <- "some text for the title of the plot" save(x, file = "x.RData") save(x, y, z, file = "xyz.RData") rm(x, y, z) # Define UI ui <- shinyUI(fluidPage( titlePanel(".RData File Upload Test"), mainPanel( fileInput("file", label = ""), actionButton(inputId="plot","Plot"), plotOutput("hist")) ) ) # Define server logic server <- shinyServer(function(input, output) { observeEvent(input$plot,{ if ( is.null(input$file)) return(NULL) inFile <- isolate({input$file }) file <- inFile$datapath # load the file into new environment and get it from there e = new.env() name <- load(file, envir = e) data <- e[[name]] # Plot the data output$hist <- renderPlot({ hist(data) }) }) }) # Run the application shinyApp(ui = ui, server = server) 但不包含x.RData时会出现以下错误消息:

xyz.RData

理想情况下,由于Warning: Error in [[: wrong arguments for subsetting an environment Stack trace (innermost first): 65: observeEventHandler [/Users/.../Desktop/app.R#31] 1: runApp 中的三个不同对象将被重用,我正在寻找一种能够创建反应元素.RDatax()y()的解决方案可以在多个z()中重复使用。

1 个答案:

答案 0 :(得分:1)

此代码有效:

library(shiny)

# Define several objects and store them to disk
x <- rnorm(100)
y <- rnorm(200)
z <- "some text for the title of the plot"

save(x, file = "x.RData")
save(x, y, z, file = "xyz.RData")
rm(x, y, z)


# Define UI
ui <- shinyUI(fluidPage(
  titlePanel(".RData File Upload Test"),
  mainPanel(
    fileInput("file", label = ""),
    actionButton(inputId="plot","Plot"),
    tableOutput("contents"),
    plotOutput("hist"))
)
)

# Define server logic
server <- shinyServer(function(input, output) {
  observeEvent(input$plot,{
    if ( is.null(input$file)) return(NULL)
    inFile <- isolate({input$file })
    file <- inFile$datapath
    load(file, envir = .GlobalEnv)

    # Plot the data
    output$hist <- renderPlot({
      plot(x,y[1:100],main=z)
    })
  })
})

# Run the application 
shinyApp(ui = ui, server = server)

制作如下情节:

enter image description here