简体   繁体   中英

Connect R and Excel

I want to connect Excel and R. I have done a visualisation in R and the data was taken from excel. Now what i want to do is, the input and output should be on excel itself. So for the input, the screen on the excel will be a table and the user has to fill the table. The table will be the input for the visualisation in R, so then the table will be read in R and the output will be displayed in excel. Now I know how to show the output from R to excel. But I don't know how to do this input screen in excel.

My goal is the user doesn't have to open R as they won't know. I want R to be in background. I know RExcel but it still has to give some commands. Is it possible to do it this way.

This is how i want it to be

It will be great, if any of you can help. Thanks

One of these options should get you going.

library(shiny)
library(readxl)

runApp(
    list(
        ui = fluidPage(
            titlePanel("Use readxl"),
            sidebarLayout(
                sidebarPanel(
                    fileInput('file1', 'Choose xlsx file',
                              accept = c(".xlsx")
                              )
                    ),
                mainPanel(
                    tableOutput('contents'))
                )
            ),
        server = function(input, output){
            output$contents <- renderTable({
                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)
            })
        }
        )
    )

OR

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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