2017-08-24 15 views
0

Shiny Appユーザーがアップロードした.Rdataファイルからいくつかのオブジェクトにアクセスして使用したいとします。 .Rdataに格納されている複数のオブジェクトにアクセスするという簡単な呼び出しによって、global.Rload()を呼び出すことは可能ですが、.Rdataファイルがアップロードされたときにアクセスして使用する方法を理解できません。Shinyの.RDataファイル内の複数のオブジェクトを読み込んで使用する

再現例えばその模倣this related question where the .Rdata file contains only one object.次のエラーメッセージを与えるxyz.RDatax.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) 

.RData 3つの異なるオブジェクトので、理想的

Warning: Error in [[: wrong arguments for subsetting an environment 
Stack trace (innermost first): 
    65: observeEventHandler [/Users/.../Desktop/app.R#31] 
    1: runApp 

を再利用されるでしょう、私は反応性要素を作成する解決策を探していますx()y()z()はいくつかにまたがって再利用できるrenderXXX()です。

答えて

1

このコードは動作します:

enter image description here

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) 

のようなプロットを作成

関連する問題