I would like to implement an App which takes a .csv file as input (the user selects the file from his computer), then I want to take this file as a parameter in a function "f" that I've implemented previously.
I can give you the ui.R and server.R
ui.R
library(shiny)
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file",
"Put your portfolio data here",
accept=c(".csv")
)
),
mainPanel(tableOutput("table"))
)
))
server.R
library(shiny)
shinyServer(function(input, output) {
data <- reactive({
file1 <- input$file
if(is.null(file1)){return()}
read.csv2(file=file1$datapath)
})
#to view the data
output$table <- renderTable({
if(is.null(data())){return ()}
data()
})
})
Finally my function "f" is supposed to give a .csv file as output.
ui.R
library(shiny)
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file",
"Put your portfolio data here",
accept=c(".csv")
),
downloadButton(download_data.csv, "Download File")
),
mainPanel(tableOutput("table"))
)
))
server.R
shinyServer(function(input, output) {
data <- reactive({
file1 <- input$file
if(is.null(file1)){return()}
read.csv2(file=file1$datapath)
})
#to view the data
output$table <- renderTable({
if(is.null(data())){return ()}
data()
})
# to download the tadata
output$download_data.csv <- downloadHandler(
filename = "download_data.csv",
content = function(file){
data <- data() # At this point you should have done the transformation
# to the data
write.csv(data, file, row.names = F)
}
)
})
Related
I have been referencing the following post which has been tremendously helpful in helping me understand Rshiny functionality:
How can I update plot from rhandsontable with uploaded data, without clicking into the table first?
I am still having some trouble grasping the concept of "Observe" so that may be the issue here. I want to upload 2 csvs where one is static (saving it in a separate tab) and one that can be edited (with edits reflected in the corresponding plot after hitting a button). My end goal is to be able to plot these data sets together on the same axes, but for now, I am having difficulty getting my table display which makes me think it's not uploading properly. I am using the following code:
library(shiny)
library(rhandsontable)
#sample data
year <- substr(Sys.Date(),1,4)
empty_dat=as.data.frame(matrix(1,nrow = 3,ncol = 4,dimnames = list(c("Cat A", "Cat B", "Cat C"),
c(paste("May",year),paste("June",year),paste("July",year),
paste("August",year)))))
ui = fluidPage(sidebarLayout(
sidebarPanel(
#static data input
fileInput('file1_new', 'Choose CSV File'),
#reactive data inut
fileInput('file1', 'Choose CSV File'),
#display reactive data
rHandsontableOutput('contents'),
actionButton("go", "Plot Update"),
width=7
),
mainPanel(
tabsetPanel(
#plot reactive data first tab
tabPanel("Plot", plotOutput("plot1")),
#table static data second tab
tabPanel("Table", tableOutput("table"))
)
)
))
server = function(input, output) {
#static input
output$table <- renderTable({
inFile <- input$file_new
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header = input$header,
sep = input$sep, quote = input$quote)
})
indat <- reactiveValues(data=empty_dat)
#reactive input
observe({
inFile = input$file1
if (is.null(inFile))
return(NULL)
data1 = read.csv(inFile$datapath)
indat$data <- data1
})
observe({
if(!is.null(input$contents))
indat$data <- hot_to_r(input$contents)
})
output$contents <- renderRHandsontable({
rhandsontable(indat$data)
})
#***example uses only one column-why I attempt multiple columns (indat$data[,1:4],indat$data[],indat$data) I get an error
#update data when user hits button
test <- eventReactive(input$go, {
return(indat$data[,3])
})
output$plot1 <- renderPlot({
#plot updated data
plot(test(),type = "l")
})
}
shinyApp(ui, server)
Any help would be greatly appreciated.
I made few changes to the code, and now the second tab displays the static table.
I am not sure if this is what you want but here's the code. The changes that I made are below the code.
library(shiny)
library(rhandsontable)
#sample data
year <- substr(Sys.Date(),1,4)
empty_dat=as.data.frame(matrix(1,nrow = 3,ncol = 4,dimnames = list(c("Cat A", "Cat B", "Cat C"),
c(paste("May",year),paste("June",year),paste("July",year),
paste("August",year)))))
ui = fluidPage(sidebarLayout(
sidebarPanel(
#static data input
fileInput('file1_new', 'Choose CSV File'),
#reactive data inut
fileInput('file1', 'Choose CSV File'),
#display reactive data
rHandsontableOutput('contents'),
actionButton("go", "Plot Update"),
width=7
),
mainPanel(
tabsetPanel(
#plot reactive data first tab
tabPanel("Plot", plotOutput("plot1")),
#table static data second tab
tabPanel("Table", tableOutput("table"))
)
)
))
server = function(input, output) {
#static input
output$table <- renderTable({
inFile <- input$file1_new
if (is.null(inFile))
{return(mtcars)}
else{
read.csv(inFile$datapath)}
})
indat <- reactiveValues(data=empty_dat)
#reactive input
observe({
inFile = input$file1
if (is.null(inFile))
return(NULL)
data1 = read.csv(inFile$datapath)
indat$data <- data1
})
observe({
if(!is.null(input$contents))
indat$data <- hot_to_r(input$contents)
})
output$contents <- renderRHandsontable({
rhandsontable(indat$data)
})
#***example uses only one column-why I attempt multiple columns (indat$data[,1:4],indat$data[],indat$data) I get an error
#update data when user hits button
test <- eventReactive(input$go, {
return(indat$data[,3])
})
output$plot1 <- renderPlot({
#plot updated data
plot(test(),type = "l")
})
}
shinyApp(ui, server)
So in the part where you do the renderTable, the variable name was slightly wrong :p, and in the read.csv, there were too many arguments.
output$table <- renderTable({
inFile <- input$file1_new
if (is.null(inFile))
{return(mtcars)}
else{
read.csv(inFile$datapath)}
})
Please let me know if this works for you... Cheers!
As, I am new to shiny apps need some assistance, uploading excel file and generating table output in shiny app works fine, but can't able to download the plot to a pdf format
Here is my code
library(shiny)
library(openxlsx)
library(lattice)
runApp(
list(
ui = fluidPage(
titlePanel("plots"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")),
tags$hr(),
downloadButton('down',"download plot")
),
mainPanel(
tableOutput('contents'),
plotOutput('plot'))
)
),
server = function(input, output){
output$contents <- renderTable({
inFile <- input$file1
if(is.null(inFile))
return(NULL)
else
read.xlsx(inFile$datapath)
})
plotInput <- reactive({
df <- input$file1
xyplot(df[,2]~df[,1],df(),xlim=c(0,10),ylim=c(0,100),type = "b")
})
output$plot <- renderPlot({
print(plotInput())
})
output$down <- downloadHandler(
filename = function(){paste("plot",".pdf",sep=".") },
content = function(file) {
pdf(file)
xyplot(df[,2]~df[,1],df(),xlim=c(0,10),ylim=c(0,100),type = "b")
dev.off()
}
)
}
)
)
The problem was that in some parts of your code you were accessing a dynamic data frame via df() but you had never defined it.
In this kind of problem, it is best to create a reactive data frame, say, df which contains the uploaded data and is passed to other reactive parts of the code via df().
Full example:
library(shiny)
library(openxlsx)
library(lattice)
runApp(
list(
ui = fluidPage(
titlePanel("plots"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")),
tags$hr(),
downloadButton('down',"download plot")
),
mainPanel(
tableOutput('contents'),
plotOutput('plot'))
)
),
server = function(input, output){
df <- reactive({
inFile <- input$file1
req(inFile) # require that inFile is available (is not NULL)
# (a user has uploaded data)
# read.xlsx(inFile$datapath)
head(iris, 10)
})
output$contents <- renderTable({
# access uploaded data via df()
df()
})
plotInput <- reactive({
df <- df()
xyplot(df[,2]~df[,1], df ,xlim=c(0,10),ylim=c(0,100),type = "b")
})
output$plot <- renderPlot({
plotInput()
})
output$down <- downloadHandler(
filename = function(){paste("plot",".pdf",sep=".") },
content = function(file) {
pdf(file)
#xyplot(df[,2]~df[,1],df(),xlim=c(0,10),ylim=c(0,100),type = "b")
# you have to print the plot so that you can open pdf file
print(plotInput())
dev.off()
}
)
}
)
)
I would like to design a basic production planning features. Mainly, i try to do in shiny what i do in excel. I have a file that is uploaded by user. Then, user should adjust new quantity(s) which decreases the inventory level. -in one day, multiple adjustments-
*Day is taken from system date. * The new lines should be displayed.
here is the sample data : http://www.filedropper.com/data2_2
library(shiny)
shinyServer(function(input, output) {
output$table1 <- renderTable({
if (is.null(input$file1))
return(NULL)
read.csv(input$file1$datapath, header=TRUE, sep=";",
quote='')
})
addData <- observe({
if(input$action > 0) {
f (is.null(input$file1))
return(NULL)
read.csv(input$file1$datapath, header=TRUE, sep=";",
quote='')
`uploadedfile1` <- read.csv(input$file1$datapath, sep=";")
uploadedfile2 <- uploadedfile1
safetystock <- 585
newLine <- isolate(c(Sys.Date(), "provided by file", input$spotQuantity, inventory, safetystock, Location2))
isolate(uploadedfile2Plot <- rbind(as.matrix(uploadedfile2), unlist(newLine)))
uploadedfile2 Plot
}
})
})
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose File', accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
numericInput("Quantity", "Enter Quantity",value=0),
actionButton("action","Confirm")
),
# Show a plot of the generated distribution
mainPanel(
tableOutput("table1")
)
)
))
Does anyone how to fix this error when I'm trying to find the degree of each vertex from the input file? Here's the Pajek file i want to import in and export the degree into CSV.
When I tried using a smaller input file. The renderTable works but when I tried with my file(which is in the link) it somehow keeps showing that error message and does not display on the tab set.
Here's what I've done so far:
ui.R
shinyUI(fluidPage(
titlePanel("Finding most influential vertex in a network"),
sidebarLayout(
sidebarPanel(
fileInput("graph", label = h4("Pajek file")),
downloadButton('downloadData', 'Download')
),
mainPanel( tabsetPanel(type = "tabs",
tabPanel("Table", tableOutput("view"))
)
)
)
))
server.R
library(igraph)
options(shiny.maxRequestSize=-1)
shinyServer(
function(input, output) {
filedata <- reactive({
inFile = input$graph
if (!is.null(inFile))
read.graph(file=inFile$datapath, format="pajek")
})
Data <- reactive({
df <- filedata()
vorder <-order(degree(df), decreasing=TRUE)
DF <- data.frame(ID=as.numeric(V(df)[vorder]), degree=degree(df)[vorder])
})
output$view <- renderTable({
Data()
})
output$downloadData <- downloadHandler(
filename = function() {
paste("degree", '.csv', sep='')
},
content = function(file) {
write.csv(Data(), file)
}
)
})
Also, I'm also not sure how to write to csv file from the data frame I've output.
In my app, the user can upload their file as the input data (using file upload widget). If they don't have their data, I provide some demo data (user can choose demo data by clicking actionButton).
How do I make a variable = the upload OR the demo, whichever is later? Any help is appreciated.
server.R
library(shiny)
DemoData = data.frame('Col.1'=c('Demo','Demo'),
'Col.2'=c('Data','Data'))
shinyServer(function(input, output) {
# option 1: use demo data
getDemo = eventReactive(input$Demo,{
DemoData
})
# option 2: user upload data
getUpload = reactive({
inFile = input$file
if (is.null(inFile)) return(NULL)
read.csv(inFile$datapath)
})
# need getData() to be option 1 or 2, whichever happened later
# should respond to multiple times of changing between option 1 and 2
getData = # ??? getDemo() or getUpload(), whichever is later
# show the data
output$InputData = renderDataTable({
as.data.frame( getData() )
})
})
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel(h2("Hello Shiny")),
sidebarLayout(
sidebarPanel(
fileInput('file',
'Choose CSV File (two columns: Town and State)',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')
),
actionButton('Demo', 'Use Demo Data')
),
mainPanel(
tabsetPanel(
tabPanel(title=h4('Data'),
column(5, tags$h3('Input Data'), dataTableOutput('InputData'))
)
)
)
)
))
UserData.R (maybe make it easier for you to test)
getwd()
setwe()
UserData = data.frame('Col.1'=c('User','User'),
'Col.2'=c('Data','Data'))
write.csv(UserData, file="UserData.csv", row.names=FALSE)
You can use observers, one to watch the demo button and one to watch for file uploads. Both update the same reactive data, so you see the effect of whichever happened last.
library(shiny)
DemoData <- data.frame('Col.1'=1:10,
'Col.2'=rnorm(10))
shinyApp(
shinyUI(fluidPage(
titlePanel(h2("Hello Shiny")),
sidebarLayout(
sidebarPanel(
fileInput('file',
'Choose CSV File (two columns: Town and State)',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')
),
actionButton('Demo', 'Use Demo Data')
),
mainPanel(
tabsetPanel(
tabPanel(title=h4('Data'),
column(5, tags$h3('Input Data'), tableOutput('InputData'))
)
)
)
)
)),
shinyServer(function(input, output) {
values <- reactiveValues() # store values to be changed by observers
values$data <- data.frame()
## Observer for uploaded file
observe({
inFile = input$file
if (is.null(inFile)) return(NULL)
values$data <- read.csv(inFile$datapath)
})
## Observer for demo data button
observe({
if (input$Demo > 0) # otherwise demo data shows on startup
values$data <- DemoData
})
## show the data
output$InputData = renderTable({
values$data
})
})
)