How to modifiy a file in real time using shiny? - r

I want to create a shiny app that do two things. Frist it has to be able to upload XLS file and after the user can add it some columns (in this case, just the column "Tecnico"), but for the moment I cant be able to do that. Once the user write the value for the column and update the table, the app crashed.
library(shiny)
library(readxl)
runApp(
list(
ui = fluidPage(
titlePanel("Use readxl"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")),textInput("tecnix","Tecnico"),
actionButton("go", "update")),
mainPanel(
tableOutput('my_output_data'))
)
),
server = function(input, output){
data1 <- reactive({
inFile <- input$file1
if (is.null(inFile)){return(NULL)}
isolate({
input$file1
my_data <- read_excel(inFile$datapath)
})
my_data
})
observeEvent(input$go, {
data1()$Técnico <- input$tecnix
})
output$my_output_data <- renderTable({data1()})
}
))
Any ideas?
Thank you so much,

As suggested by #Till using reactiveValues will help solve the problem.
library(shiny)
library(readxl)
runApp(
list(
ui = fluidPage(
titlePanel("Use readxl"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx")),textInput("tecnix","Tecnico"),
actionButton("go", "update")),
mainPanel(
tableOutput('my_output_data'))
)
),
server = function(input, output){
rv <- reactiveValues(my_data = NULL)
observeEvent(input$file1, {
req(input$file1$datapath)
rv$my_data <- read_excel(input$file1$datapath)
})
observeEvent(input$go, {
rv$my_data$Techni <- input$tecnix
})
output$my_output_data <- renderTable({rv$my_data})
}
))

Related

How to grab data into RShiny from excel based on cell color properties

We want to upload the file in shiny
But read only values based on cell color properties
(Cell background = white -> read value
else ignore)
How can we do it ?
Please find the template to read excel below
and screenshot of an excel :
#install.packages("readxl")
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({
req(input$file1)
inFile <- input$file1
read_excel(inFile$datapath, 1)
})
}
)
)

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)
})
}
)
)

Uploading data to Shiny for analyses

I am trying to upload a file to Shiny to crunch it and then view it.
I already was able to create a table with a CSV document, but the output myData can't be used as an input for a boxplot or any other graph.
SERVER
shinyServer(function(input, output, session){
myData <- reactive({
inFile <- input$file1
if (is.null(inFile)) return(NULL)
data <- read.csv(inFile$datapath, header = TRUE)
data
})
output$contents <- renderTable({
myData()
})
}
UI
library(shiny)
write.csv(data.frame(a = 1:10, b = letters[1:10]), 'test.csv')
shinyUI(fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv'))
),
mainPanel(
tableOutput('contents'),
plotOutput('distPlot')
)
)
)
)
How can I use the data from the file I uploaded out of the function myData?
You should use the functions renderDataTable and dataTableOutput from the DT package for rendering your tables. Your code works fine like this:
library(shiny)
library(DT)
server <- function(input, output, session){
myData <- reactive({
inFile <- input$file1
if (is.null(inFile)) return(NULL)
data <- read.csv(inFile$datapath, header = TRUE)
data
})
output$contents <- DT::renderDataTable({
DT::datatable(myData())
})
}
write.csv(data.frame(a = 1:10, b = letters[1:10]), 'test.csv')
ui<- shinyUI(fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv'))
),
mainPanel(
DT::dataTableOutput('contents'),
plotOutput('distPlot')
)
)
)
)
shinyApp(ui,server)
Also see the article below:
It is also possible to have an user upload csv's to your Shiny app. The code below shows a small example on how this can be achieved. It also includes a radioButton input so the user can interactively choose the separator to be used.
library(shiny)
library(DT)
# Define UI
ui <- shinyUI(fluidPage(
fileInput('target_upload', 'Choose file to upload',
accept = c(
'text/csv',
'text/comma-separated-values',
'.csv'
)),
radioButtons("separator","Separator: ",choices = c(";",",",":"), selected=";",inline=TRUE),
DT::dataTableOutput("sample_table")
)
)
# Define server logic
server <- shinyServer(function(input, output) {
df_products_upload <- reactive({
inFile <- input$target_upload
if (is.null(inFile))
return(NULL)
df <- read.csv(inFile$datapath, header = TRUE,sep = input$separator)
return(df)
})
output$sample_table<- DT::renderDataTable({
df <- df_products_upload()
DT::datatable(df)
})
}
)
# Run the application
shinyApp(ui = ui, server = server)

How to use a data.frame in server.R to update a select menu in ui.R?

Here is my plan for the website:
I use file upload to read a csv file from my local computer using the following code:
datatable <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote)
})
in my ui.R, I will use a select menu to show all the column names of datatable. I will pick more names, like 2, from here.
According to what column names I pick, I will generate some plots.
So, here are my questions:
How can I get those column names in ui.R from the data.frame generated in server.R?
How can I transfer the picked names in ui.R back to server.R so that I can generate the plots?
I read some examples like http://shiny.rstudio.com/gallery/update-input-demo.html or something about renderUI, but I just do not know how to use them or it they are the right way to do this.
Here is my all code. In server.R:
library(shiny)
options(shiny.maxRequestSize=300*1024^2)
shinyServer(function(input, output) {
datatable <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote)
})
output$contents <- renderDataTable({
datatable()
}, options = list(orderClasses = TRUE))
output$summary <- renderPrint({
summary(datatable(),20)
aaa <- "afefagfaegar"
list(summary(datatable(),20),aaa)
})
})
Here is the code in ui.R
library(shiny)
shinyUI(navbarPage("",
tabPanel("Data Summary",
fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
width=2,
fileInput('file1', 'Choose CSV File',
accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',c(Comma=',',Semicolon=';',Tab='\t'),','),
radioButtons('quote', 'Quote',c(None='','Double Quote'='"','Single Quote'="'"),'"')
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Table", dataTableOutput('contents')),
tabPanel("Summary", verbatimTextOutput("summary"))
)
)
)
)
),
tabPanel("Single factor",
fluidPage(
sidebarLayout(
sidebarPanel(
***#I think I should put something here for the select menu***
),
mainPanel()
)
)
),
tabPanel("Multiple factors")
)
)
I think I figure out how to do this.
In server.R, I put:
output$singlefactor <- renderUI({
selectInput("sfactor", "Feature selection:", names(datatable()))
})
In ui.R, I do like:
uiOutput("singlefactor")
So far, everything is fine.

In Shiny, how to see which action happen later?

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
})
})
)

Resources