用户加载的数据中的闪亮图

时间:2018-08-22 17:02:08

标签: r shiny shinydashboard

我正在构建一个闪亮的仪表板,我希望它允许用户能够加载excel文件,然后生成图和其他输出项目。我遇到的问题是我可以加载数据:

ui <- dashboardPage(
       dashboardBody(
        tabItems(
    tabItem(tabName = "data_file",
            fluidPage(
              titlePanel("Upload_Data_File"),
              sidebarLayout(
                sidebarPanel(
                  fileInput('file1', 'Choose xlsx file',
                            multiple = TRUE,
                            accept = c(".xlsx"))
                ),
                mainPanel(
                  tableOutput('contents'))
              ),
            DT::dataTableOutput("sample_table"))
    ))))

server <- function(input, output) {
df_products_upload <- reactive({

inFile <- input$file1

if(is.null(inFile))
  return(NULL)
file.rename(inFile$datapath,
            paste(inFile$datapath, ".xlsx", sep=""))
read_excel(paste(inFile$datapath, ".xlsx", sep=""), 1)
})

output$sample_table <- DT::renderDataTable({
df <- df_products_upload()
DT::datatable(df)
})
shinyApp(ui = ui, server = server)

如果我想对此数据帧进行一些分析,可以在服务器选项内执行此操作:

output$plot <- renderPlot({
  data <- df_products_upload() ........ #Some Analysis
  data2, data3.... # Items generated during analysis 
  plot(data2)})

,然后在输出函数内部完成分析并创建我的其他数据框,这些数据框最终将在调用中使用。但是,如果我想创建将在仪表板上生成的多个输出图,是否有更好的效率允许运行分析,以便将来可以通过这种方式生成输出图?

 output$plot2 <- renderPlot({
     plot(data3) })

当前,我只能通过复制服务器中所有输出的分析来使它工作,这效率很低。

 output$plot2 <- renderPlot({
   data <- df_products_upload() ........ #Some Analysis
   data2, data3.... # Items generated during analysis 
   plot(data3)})

谢谢!

1 个答案:

答案 0 :(得分:0)

在我看来,您需要的是

data<-reactive({
    data <- df_products_upload() ........ #Some Analysis
    data #data should be a vector, list or whatever containg all the itmes
    })

然后

output$plot <- renderPlot({
 plot(data()[1]) 
})

output$plot2 <- renderPlot({
 plot(data()[2]) 
})