R/Shiny: Populate SelectInput Options using Headers From Uploaded File - r

I'm trying to build a page using R Shiny that has:
A File Widget for uploading CSV files
A SelectInput component
I'd like to use these as follows:
Upon uploading a valid CSV file, populate the SelectInput whose options are the headers from the CSV file, the first header being the default selected
I've tried various forms of observe() and observeEvent() so far, but have had no success in getting this even close. Any suggestions you may have would be great.

Here's an option -
library(shiny)
#Sample data
#write.csv(mtcars, 'data.csv', row.names = FALSE)
ui <- fluidPage(
fileInput('file', 'Upload csv file'),
uiOutput('dropdown')
)
server <- function(input, output) {
data <- reactive({
req(input$file)
read.csv(input$file$datapath)
})
output$dropdown <- renderUI({
req(data())
selectInput('cols', 'Select Column', names(data()))
})
}
shinyApp(ui, server)

Related

R/Shiny: Populate CheckBox Group Dynamically Using Headers From Uploaded File

I'm trying to build a page using R Shiny that has:
A File Widget for uploading CSV files
A Checkbox Group component
I'd like to use these as follows:
Upon uploading a valid CSV file, populate the CheckBox group whose checkboxes are all the headers from the CSV file, all checked by default
I've tried various forms of observe() and observeEvent() so far, but have had no success in getting this even close. Any suggestions you may have would be great.
You may try checkboxGroupInput.
library(shiny)
ui <- fluidPage(
fileInput('file', 'Upload csv file'),
uiOutput('dropdown')
)
server <- function(input, output) {
data <- reactive({
req(input$file)
read.csv(input$file$datapath)
})
output$dropdown <- renderUI({
req(data())
checkboxGroupInput('cols', 'Select Column', names(data()),
names(data()), inline = TRUE)
})
}
shinyApp(ui, server)
You can set inline = FALSE if you want to arrange them vertically.

How to upload PDF File, Display it and Remove it on Shiny R

I have been tried to upload PDF file and try to display in Shiny it but it's not working.
I tried using the script from here upload and view a pdf in R shiny but i got result Not Found.
Is there anyone can help
UPDATE
Thanks a lot for #YBS for the advise, the reference link that shown above are work properly.
After I create the www folder it runs well and it's must run from the browser
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Testing File upload"),
sidebarLayout(
sidebarPanel(
fileInput('file_input', 'upload file ( . pdf format only)', accept = c('.pdf')),
fileInput('file_input2', 'upload file2 ( . pdf format only)', accept = c('.pdf'))
),
mainPanel(
uiOutput("pdfview"),
uiOutput("pdfview2")
)
)
))
server <- shinyServer(function(input, output) {
observe({
req(input$file_input)
file.copy(input$file_input$datapath,"www\\pdf1", overwrite = T)
output$pdfview <- renderUI({
tags$iframe(style="height:600px; width:100%", src="pdf1\\0.pdf")
})
})
observe({
req(input$file_input2)
file.copy(input$file_input2$datapath,"www\\pdf2", overwrite = T)
output$pdfview2 <- renderUI({
tags$iframe(style="height:600px; width:100%", src="pdf2\\0.pdf")
})
})
})
shinyApp(ui = ui, server = server)

Excel data distribution with Shiny

I am not good at English, so sentences may be wrong.
I want to distribute excel files prepared in advance to users. Is it possible to realize such a system with shiny? No problem with .zip.
Thank you
ui.R
shinyUI(
fluidPage(
downloadButton('downloadData', 'Excel Download')
)
)
server.R
shinyServer(function(input, output) {
output$downloadData <- downloadHandler(
filename = "distribution.xlsx",
content = "distribution_excel"
)
})
Yes, it is possible and you were nearly there. Below is a minimal working example. I assume that your .xlsx file is located in the same folder as your app.R. Notice that I have created the app in a single R file as opposed to two separate files.
The trick to getting the file to download is to use a function for the content inside of downloadHandler(). Specifically we are using the base function file.copy(). Clicking the button should now download the file: distribution.xlsx. This file can obviously be exchanged with a zip file.
If you want different users to access different Excel files, you can write an additional function inside of your server function that passes the file argument to the downloadHandler().
# Load packages ----
pkgs <- c("shiny")
invisible(lapply(pkgs, require, character.only = TRUE))
# Set up the UI ----
ui <- fluidPage(
# Define your download button
downloadButton(
outputId = "downloadData",
label = "Excel Download"
)
)
# Set up the server side ----
server <- function(input, output, session) {
# Define the download handler with function() for content.
output$downloadData <- downloadHandler(
filename = "distribution.xlsx",
content = function(file) {
file.copy("distribution.xlsx", file)
}
)
}
# Combine into an app ----
shinyApp(ui = ui, server = server)

How to get the path from input$.. or output$.. and use it to list.files and then copy/cut the files

I am learing Shiny. I want to make a simple app that allows for dynamic paths that the user enters. The app should then list csv files in folder A and then copy them from folder A to folder B (the working directory). Then the app does some operations in folder B using an external exe program. Afterwards the folder will cut the results files (.txt) from B and copies them into A.
The structure of my app is as follows ( I have also attached a picture). the problem is explained in the comments in the code.
library(shiny)
ui<-fluidPage(
textInput("prg","Program",getwd()),
verbatimTextOutput("prg"),
textInput("prj","Project","Project"),
verbatimTextOutput("prj")
)
server<-function(input, output,session) {
output$prg=renderText(input$prg)
renderPrint(output$prg)
output$prj=renderText(paste0(input$prg,"/",input$prj))
#This is where my challenge is
#I want to
#list.files(path=path-shown-in-text-box-Project,pattern=".csv")
#Then i want to copy csv files from A to B as described above and run the following program
#This works
observeEvent(input$run,
{
system("my.exe") #exe not shared
})
#Finally I want to cut and paste the results (.txt) from B back into A
}
shinyApp(ui,server)
I want to list.files(path=path-shown-in-text-box-Project,pattern=".csv")
Here's code you can use to browse any directory for a particular CSV file, read that file and display its contents.
library(shiny)
# Define UI
ui <- pageWithSidebar(
# App title ----
headerPanel("Open a File and Show Contents"),
# Sidebar panel for inputs ----
sidebarPanel(
label="Data Source",fileInput("fileName", "File Name",accept=c(".csv"))),
# Main panel for displaying outputs ----
mainPanel(
tableOutput(outputId = "table")
)
)
# Define server logic
server <- function(input, output) {
inputData <- reactive ({
if (is.null(input$fileName)) return(NULL)
inFile <- input$fileName
conInFile <- file(inFile$datapath,open='read')
inData <- read.csv(conInFile,stringsAsFactors = FALSE)
close (conInFile)
return (inData)
})
output$table <- renderTable ({
inData <- inputData()
if (length(inData) > 0) inData
})
}
shinyApp(ui, server)

How to get correct file path after uploading using fileInput using R shiny?

I have a code that uploads a file. After uploading the file using a action button "Save to Database", I store the file name and file path in vectors.
In the same app, I have another tab that displays the excel output in form of table. So, to read the file I use the file path retrieved while saving the file using the action button.
The problem is I get "File does not exist" since the path is something like below
"C:\Users\Arun\AppData\Local\Temp\RtmpINivvL/69ff834f0b2623ef2ec95c41/0.xlsx"
While the location I uploaded the file from is
" "D:/Data_Dump/summary.xlsx"
How to solve this issue?
UI.R code
tabItem(tabName = "file",
mainPanel(
titlePanel(h2("Upload your XLSX file here ")), fluidRow(
column(6,
fileInput('file1', 'Choose a XLSX file to upload',
accept = c('.xlsx'))),
column(6,actionButton("save","Save to Database")),
div(DT::dataTableOutput("contents"),style = "font-size: 100%;width: 150%")
)
)
)
server.R code
eventReactive(input$save,{
filenm <- input$file1
filenm$name
tablelist <<- c(tablelist,as.character(filenm$name))
print(tablelist)
filePath <<- c(filePath,as.character(filenm$datapath))
print(filePath)
return (tablelist)
})
one workaround for getting the actual filepath, might be using the shinyFiles package. But you have to ensure the upload functionality by something like file.copy() manually. In addition in shinyFiles you cannot specify a certain filetype to be accepted.
Anyway here is a small example of shinyFiles:
library(shiny)
library(shinyFiles)
ui <- fluidPage(
shinyFilesButton("Btn_GetFile", "Choose a file" ,
title = "Please select a file:", multiple = FALSE,
buttonType = "default", class = NULL),
textOutput("txt_file")
)
server <- function(input,output,session){
tablelist<-NULL
filePath<<-NULL
volumes = getVolumes()
observe({
shinyFileChoose(input, "Btn_GetFile", roots = volumes, session = session)
if(!is.null(input$Btn_GetFile)){
browser()
file_selected<-parseFilePaths(volumes, input$Btn_GetFile)
#check if file extension is .xlsx
tablelist <<- c(tablelist,as.character(file_selected$name))
print(tablelist)
filePath <<- c(filePath,as.character(file_selected$datapath))
print(filePath)
#upload file via e.g. file.copy()
output$txt_file <- renderText(as.character(file_selected$datapath))
}
})
}
shinyApp(ui = ui, server = server)

Resources