简体   繁体   English

尝试将Excel CSV文件放入R Shinydashboard

[英]Trying to put an Excel CSV file into R shinydashboard

I created the main dashboard already in R using the shinydashboard package. 我已经使用Shinydashboard软件包在R中创建了主仪表板。 However, my main question is how do I upload an Excel Csv file (which contains all the data) into the dashboard? 但是,我的主要问题是如何将Excel Csv文件(包含所有数据)上载到仪表板? I have been trying to figure this out and I am having trouble. 我一直在试图解决这个问题,但遇到了麻烦。 So far, I have the following script: 到目前为止,我有以下脚本:

install.packages("shinydashboard")
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Analysis Project"),
  dashboardSidebar(
    menuItem("Dashboard", tabName = "dashboard"),
    menuItem("Filter Page"),
    menuItem("Data")
  ),
    dashboardBody(
      tabItems(
       tabItem(tabName = "dashboard")
)
)

) server <- function(input,output) { } shinyApp(ui, server) )服务器<-函数(输入,输出){} ShinyApp(用户界面,服务器)

How about doing it this way? 这样做怎么样?

library(shiny)
library(readxl)

ui <- fluidPage(
  fileInput('file1', 'Insert File', accept = c(".xlsx")),
  textInput('file1sheet','Name of Sheet (Case-Sensitive)'),
  tableOutput("value")
)

server <- function(input, output) {

  sheets_name <- reactive({
    if (!is.null(input$file1)) {
      return(excel_sheets(path = input$file1$datapath))  
    } else {
      return(NULL)
    }
  })

  output$value <- renderTable({
    if (!is.null(input$file1) && 
        (input$file1sheet %in% sheets_name())) {
      return(read_excel(input$file1$datapath, 
                        sheet = input$file1sheet))
    } else {
      return(NULL)
    }
  })
}

shinyApp(ui, server)

Try to make a code like that ! 尝试编写这样的代码!

library(shiny) library(shinydashboard) 库(发光)库(发光仪表板)

header <- dashboardHeader(
title = "Analysis Project"
)

sidebar <- dashboardSidebar(
menuItem(text = "Importing file", tabName = "dashboard",icon = icon("file"),
       menuSubItem(text = "Importing file", tabName = "dataset")
       ),
menuItem("Dashboard", tabName = "dashboard"),
menuItem("Filter Page"),
menuItem("Data")
)


body <- dashboardBody(
 fluidPage(
   tabItems(
     tabItem(tabName = "dataset",
                fluidRow(
                  box(width = 12,
                      fileInput(inputId = "file",
                                label = "Choose a file",
                                accept = c(".xlsx",".csv")
                                ),
                      tableOutput(outputId = "Contents"),
                      verbatimTextOutput(outputId = "Data")
                      )
                  )
              )
          )
     )
 )

ui <- dashboardPage(header = header, 
                sidebar = sidebar, 
                body = body)

server <- function(input, output, session) {
  output$Data <- renderPrint({
    if(is.null(input$file)){
      print("Import Excel data file")
  } else {
      inFile <- input$file
      df <- read_excel(inFile$datapath)
      print(df)
    }
  })
 }
shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM