Shiny Rstudio upload xlsx file - r

I am trying to upload an xlsx file using shiny rstudio. But the data can't be read in. Can anyone help me figure it out?
ui <- fluidPage(titlePanel("AFSI Data Reformat"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx"))),
mainPanel(
tableOutput('file2'))))
Server(function(input, output) {
output$file2 <- renderTable({
inFile <- input$file1
if(is.null(inFile))
return(NULL)
read_excel(paste(inFile$datapath, ".xlsx", sep=""), 1)})
When I clicked 'Run App', and the tried to upload an
xlsx file, it shows nothing was read in, 'No file uploaded'.

Works fine for me after removing the instruction that adds a second ".xlsx" extension to file name :
ui <- fluidPage(titlePanel("AFSI Data Reformat"),
sidebarLayout(sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx"))
),
mainPanel(tableOutput('file2'))))
server <- function(input, output) {
output$file2 <- renderTable({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
readxl::read_excel(inFile$datapath)
})
}
shinyApp(ui, server)

Related

Is there an equivalent to the Shiny fileInput function for saving outputs?

Shiny has a nice fileInput() function for allowing the user to browse directories and select the file to upload into the App. Extracted from https://shiny.rstudio.com/reference/shiny/1.7.0/fileInput.html, here's the MWE:
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV file to upload", accept = ".csv"),
checkboxInput("header", "Header", TRUE)
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output) {
output$contents <- renderTable({
file <- input$file1
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "csv", "Please upload a csv file"))
read.csv(file$datapath, header = input$header)
})
}
shinyApp(ui, server)
I'm using this in my App for retrieving data, I really like it. However, I'm looking for a similar feature where the user can save a dataframe that results from running the App in a similar way, by browsing directories to choose a location to save the file and selecting a file name. Key is the ability to browse local directories. Any ideas how to accomplish this? Preferably in a way as simple as fileInput().
Files will be .csv, though I'm thinking about .xls too.
You can use downloadButton in ui and downloadHandler in server as follows:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV file to upload", accept = ".csv"),
checkboxInput("header", "Header", TRUE),
downloadButton("download")
),
mainPanel(
tableOutput("contents"),
)
)
)
server <- function(input, output) {
output$contents <- renderTable({
file <- input$file1
ext <- tools::file_ext(file$datapath)
req(file)
validate(need(ext == "csv", "Please upload a csv file"))
read.csv(file$datapath, header = input$header)
})
output$download <- downloadHandler(
filename = function() {
paste0(input$file1, ".csv")
},
content = function(file) {
write.csv(contents(), file)
}
)
}
shinyApp(ui, server)
Here's a really basic example that invokes file.choose when you click on the action button.
library(shiny)
shinyApp(
ui =
fluidPage(
textInput(inputId = "txt_to_save",
label = "Enter text to save to a file"),
actionButton(inputId = "btn_save_txt",
label = "Save Text to File")
),
server =
shinyServer(function(input, output, session){
observeEvent(
input$btn_save_txt,
{
dest_file <- file.choose()
write(input$txt_to_save,
dest_file)
}
)
})
)
In practice, if you wanted to have the file name created by the application, but the directory chosen by the user, you could also use choose.dir().

R Shiny openxlsx: uploaded xlsx file - select a sheet from drop-down menu

In my Shiny app, I can upload an xlsx file and select sheet by typing the sheet's name:
library(shiny)
library(openxlsx)
runApp(
list(
ui = fluidPage(
titlePanel("openxlsx - choose sheet"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx"))),
mainPanel(tableOutput('contents'),
textInput("tab1", "Type in sheet name:", "Sheet1")))),
server = function(input, output,session){
output$contents <- renderTable({
inFile <- input$file1
if(is.null(inFile))
return(NULL)
file.rename(inFile$datapath,paste(inFile$datapath, ".xlsx", sep=""))
read.xlsx(paste(inFile$datapath, ".xlsx", sep=""), sheet=input$tab1)
})}))
I would prefer to be able to use a drop-down menu to select a sheet. I know I can use openxl package to get the sheets names, but I am not sure how to implement that in Shiny. Any help will be appreciated.
In the UI:
uiOutput("dropdownUI")
In the server:
Workbook <- eventReactive(input$file1, {
loadWorkbook(input$file1$datapath)
})
Sheets <- eventReactive(Workbook(), {
names(Workbook())
})
output$dropdownUI <- renderUI({
req(Sheets())
selectInput("sheet", "Choose a sheet", Sheets())
})
Dat <- eventReactive(input$sheet, {
read.xlsx(Workbook(), sheet = input$sheet)
})
output$contents <- renderTable({
req(Dat())
Dat()
})

Upload data, call r script and change data, download using ShinyApp

I want to upload a dataframe, call r scipt and apply a function that updates the dataframe, then download it. Can anyone help me?
Let's say this is the function I need to call
Identify_IP(dataframe)
return(dataframe')
This is Shiny App
library(shiny)
source('InflectionP2.R', local = TRUE)
runApp(
list(
ui = fluidPage(
titlePanel("Upload your file"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xls file',
accept = c(".XLS")),
actionButton("btn", "Update Table")
),
mainPanel(
tableOutput('what'))
)
),
server = function(input, output, session){
dataframe <- reactive({
inFile <- input$file1
if (is.null(input$file1))
return(NULL)
Identify_IP(read.table(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote))
})
observeEvent(input$btn, {output$what <- renderTable({dataframe})})
}
)
)
This is what I get
Warning: Error in as.data.frame.default: cannot coerce class "c("reactiveExpr", "reactive")" to a data.frame

how to do dynamic dropdown in shiny from imported data?

In R shiny I have imported the .xlsx file using the below code, I have to select the header attributes to the drop-down and want to do the bar chart using that?? I am new to R can anyone help me??
library(shiny)
library(readxl)
runApp(
list(
ui = fluidPage(
titlePanel("File Upload"),
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)
})
}
)
)

Using an .xlsx file as an input in a Shiny App

I am working on a shiny app that requires a Excel with multiple sheets as an input. It was working well with the xlsx library and read.xlsx function. However, it returns the following error:
java.lang.IllegalArgumentException: Cell index must be >= 0
I've now switched to gdata and read.xls, but I get a file path error every time I run the model.
I wonder if anyone dealt with this problem before and if there are any simple solutions to fix this problem.
EDIT:
The following code
library(shiny)
library(xlsx)
ui <- fluidPage(
fileInput("uploadFile", "XLSX file"),
verbatimTextOutput("summary")
)
server <- function(input, output) ({
dataset<-reactive({
inFile <- input$uploadFile
dat<-read.xlsx(inFile$datapath, 1)
return(dat)
})
output$summary <- renderText({summary(dataset())})
})
shinyApp(ui, server)
Returns:
Error : package ‘rJava’ could not be loaded
If, instead I use
library(shiny)
library(readxl)
ui <- fluidPage(
fileInput("uploadFile", "XLSX file"),
verbatimTextOutput("summary")
)
server <- function(input, output) ({
dataset<-reactive({
inFile <- input$uploadFile
dat<-read_excel(inFile$datapath, sheet = 1)
return(dat)
})
output$summary <- renderText({summary(dataset())})
})
shinyApp(ui, server)
I get:
Error: Missing file extension.
(Using readxl)
The datapath column from input$uploadFile is a path to a temp file which has no extension. So read_excel() does not know what format it is.
Use read_xlsx() instead:
dat <- read_xlsx(inFile$datapath, sheet = 1)
I just tried this and it worked fine for me.
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)
})
}
)
)
Also, see this.
https://shiny.rstudio.com/gallery/file-upload.html

Resources