This question already has an answer here:
R shiny table with dates
(1 answer)
Closed 4 years ago.
While creating a shiny app to get a data frame according to user input, i use the following, which works perfectly fine:
library(shiny)
ui <- fluidPage(
textInput("name", "Comnnay Name"),
textInput("income", "Income"),
textInput("expenditure", "Expenditure"),
dateInput("date", h3("Date input"),value = Sys.Date() , min = "0000-01-01",
max = Sys.Date(), format = "dd/mm/yy"),
#Table showing what is there in the data frame
tableOutput("table"),
#Button which appends row to the existing dataframe
actionButton("Action", "Submit"),
#Button to save the file
downloadButton('downloadData', 'Download')
)
library(shiny)
server <- function(input, output){
#Global variable to save the data
Data <- data.frame()
Results <- reactive(data.frame(input$name, input$income, input$expenditure,
input$date , Sys.Date()))
#To append the row and display in the table when the submit button is clicked
observeEvent(input$Action,{
#Append the row in the dataframe
Data <<- rbind(Data,Results())
#Display the output in the table
output$table <- renderTable(Data)
})
output$downloadData <- downloadHandler(
# Create the download file name
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(Data, file) # put Data() into the download file
})
}
shinyApp(ui = ui, server = server)
But when i press the submit button it does not print the date in the format i want
What do i do to change the output version. Or do i have to specify something specially? Thank you.
All dates in most languages are numeric, which is expected. If you want the string simply parse into it
Results <- reactive(data.frame(input$name, input$income, input$expenditure,
as.character(input$date), as.character(Sys.Date())))
Related
I am new to shiny and trying to combine a couple features and having some trouble.
I want for the user to be able to select a CSV and then be presented with a random instance (in this case tweet) from that table. The following code worked when "tweetData" was a statically loaded csv using read_csv.
## function to return random row number from data set
getTweet <- function(){
tweetData[sample(nrow(tweetData), 1), ]
}
function(input, output, session) {
## set reactive values, get randomized tweet
appVals <- reactiveValues(
tweet = getTweet(),
ratings = data.frame(tweet = character(), screen_name = character(), rating = character())
)
I want to instead use a dynamically chosen csv for "tweetData", something like adding this??
csvName <- reactive(paste0('../folder_path/', input$file_name))
selectedData <- read.csv(csvName)
How can use reactively chosen csvs to fit into the structure of the first code chunk?
You might be looking for fileInput for giving user an option to upload a dataset.
This is a simple reproducible example -
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File"),
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output) {
output$contents <- renderTable({
req(input$file1)
read.csv(input$file1$datapath)
})
}
shinyApp(ui, server)
I have a shiny app that creates a data frame based on user inputs. I want to make a dynamic download button that takes user choices (radioButton) to download dynamically made data frame. The data frames are returned from a function as a list
When I create functions for two different download buttons the downloading works fine
library(shiny)
library(DT)
temp_func <- function(){
x <- mtcars
y = x[,1]
return(list(complete_df = as.data.frame(x), column1 = as.data.frame(y)))
}
# UI
ui <- shinyUI({
fluidPage(
actionButton("fetch", label = "Fetch data first"),
mainPanel(DT::dataTableOutput("table")),
downloadButton("down_all", "Download all"),
downloadButton("down_c1", "Download c1")
)})
# Server
server <- Main_Server <- function(input,output,session){
# Reactive Values
values <- reactiveValues(table = NULL)
# fetchEvent (Consider temp_func() is fetching data from website)
observeEvent(input$fetch, {values$table <- temp_func()})
# Rendering table for display
output$table <- renderDT({datatable(values$table$complete_df)})
# Download 1
output$down_all <- downloadHandler(
filename = function() { paste("All columns","csv", sep=".")},
content = function(file) {write.csv(values$table$complete_df, file)})
# Download 2
output$down_c1 <- downloadHandler(
filename = function() { paste("Columns1","csv", sep=".")},
content = function(file) {write.csv(values$table$column1, file)})
}
# Run-app
shinyApp(ui, server)
Once I merge the two functions and pass input$choice from radio button I get an empty file
library(shiny)
library(DT)
temp_func <- function(){
x <- mtcars
y = x[,1]
return(list(complete_df = as.data.frame(x), column1 = as.data.frame(y)))
}
# UI
ui <- shinyUI({
fluidPage(
actionButton("fetch", label = "Fetch data first"),
mainPanel(DT::dataTableOutput("table")),
radioButtons("rd", c("Select"), choices = c("All Columns" = "complete_df","Column 1" = "column1"),
selected = "complete_df"),
downloadButton("down", "Download")
)})
# Server
server <- Main_Server <- function(input,output,session){
# Reactive Values
values <- reactiveValues(table = NULL)
# fetchEvent (Consider temp_func() is fetching data from website)
observeEvent(input$fetch, {values$table <- temp_func()})
# Rendering table for display
output$table <- renderDT({datatable(values$table$complete_df)})
# Combined Download
output$down <- downloadHandler(
filename = function() { paste("File","csv", sep=".")},
content = function(file) {write.csv(values$table$input$rd, file)})
}
# Run-app
shinyApp(ui, server)
Consider temp_func() is fetching data from other website
Try to use :
# Combined Download
output$down <- downloadHandler(
filename = function() { paste("File","csv", sep=".")},
content = function(file) {write.csv(values$table[[input$rd]], file)})
The syntax you used returns NULL because values$table doesn't have an input field.
With the updated syntax, the downloaded file isn't empty anymore.
I need to give users a set of 60 observations. I have a master table that I want to to subset these 60 observations from. So, (1) I host the master table as a published csv file on google drive. (2) Write a shiny code to subset 60 values in R studio. The user will have to enter a group ID that I use as set.seed and ensure that the user sees the same subset every time he / she attempts to get the 60 observations. And, it also helps me keep track of the observations that the user has.
The code works fine and I am able to show the subset table. But, I am not able to get the download to work. I saw a post that says renderTable create an HTML table that cannot be downloaded and I should create the table outside it. I tried using reactive to do this, but it did not work and kept giving various errors. For example:
"cannot coerce class ‘c("reactiveExpr", "reactive", "function")’ to a data.frame"
Will appreciate any help of this - even if someone can please point out to what I should read and try to make this work.
library(shiny)
db1 <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vS94xYLix6bDUNNXAgHejdQ-CcWi-G4t25nfxuhRZF57TloC8NwVgnperBB9-U-IuDvMcOnvdc9iavU/pub?gid=0&single=true&output=csv")
# Define UI
ui <- fluidPage(
# Application title
titlePanel("MnM"),
# Sidebar to take input of group ID
sidebarLayout(
sidebarPanel(
numericInput("seed","Group ID:", value = 100, min = 100, max = 999),
downloadButton("downloadData", "Download")
),
# Show the table
mainPanel(
tableOutput("table")
)
)
)
# Define server logic for the table
server <- function(input, output) {
output$table <- renderTable({
set.seed(input$seed)
zz <- sample(1:nrow(db1), size = 60, replace = TRUE)
data.frame(db1[zz,])})
output$downloadData <- downloadHandler(
filename = "test.csv",
content = function(file) {
write.csv(output$table, file, row.names = FALSE)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
Create your table once, and then use it in your renderTable and downloadHandler. Create it as a reactive, so its available everywhere.
Note that downloadHandler doesn't work in RStudio's preview, view it in a browser instead. There is a button labelled 'Open in Browser' that will do this.
Here is your code with that applied:
library(shiny)
db1 <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vS94xYLix6bDUNNXAgHejdQ-CcWi-G4t25nfxuhRZF57TloC8NwVgnperBB9-U-IuDvMcOnvdc9iavU/pub?gid=0&single=true&output=csv")
# Define UI
ui <- fluidPage(
# Application title
titlePanel("MnM"),
# Sidebar to take input of group ID
sidebarLayout(
sidebarPanel(
numericInput("seed","Group ID:", value = 100, min = 100, max = 999),
downloadButton("downloadData", "Download")
),
# Show the table
mainPanel(
tableOutput("table")
)
)
)
# Define server logic for the table
server <- function(input, output) {
#Create dataframe
mytable <- reactive({
set.seed(input$seed)
zz <- sample(1:nrow(db1), size = 60, replace = TRUE)
data.frame(db1[zz,])
})
#Display dataframe in table
output$table <- renderTable({
mytable()
})
#Download dataframe
output$downloadData <- downloadHandler(
filename = "test.csv",
content = function(file) {
write.csv(mytable(), file, row.names = FALSE)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
Using stackoverflow, I created a shiny app which uploads a csv file and then displays a datatable.
After selecting columns dynamically, where some columns have "_down" end.
I require help in shortening the dataframe (as in the code below) and also remove duplicates by ID column (if present).
# install.packages("shiny")
# install.packages("DT")
# install.packages("shinycssloaders")
library(DT)
library(shiny)
library(shinycssloaders)
UI code
##Creating the UI as a fluidPage,
##fluidPage allows scaling components of the browser in realtime to fill all available broswer width
##This is standard
ui <- fluidPage(
# Title of app
titlePanel("Upload file to table"),
# Main panel for displaying outputs
mainPanel(
#fileInput with acceptance of text/csv and more
fileInput('file', 'Choose file to upload',
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv',
'.html'
)),
# Output: datatable
DT::dataTableOutput("data_as_table")%>%withSpinner(),
#Download button
downloadButton("downloadData", "Download")
)
)
Server Code
Creating server
server <- function(input, output) {
#Data is a reactive element meaning it will update when the reactive input inside it change
#Data will update when input$file changes
#input$file is the uploaded file (se fileInput in ui)
data <-reactive({
#Store input$file as inFile
inFile <- input$file
#if its empty return nothing
if (is.null(inFile))
return(NULL)
#read in the file as a csv, with headers, comma seperated
dd = read.csv(inFile$datapath, header = T,
sep = ",")
dd = as.data.frame(dd)
#Shortening dataframe
#dd= dd[apply(dd[, endsWith(colnames(dd), "_down")], 1, function(x) any(x == "TRUE")), ]
#Remove duplicates by ID column, and show unique
#xxx
return(dd)
})
#Make the output data_as_table a datatable containing the reactive element data
output$data_as_table<-DT::renderDataTable({
data()
})
# Downloadable csv of reactive data() object
output$downloadData <- downloadHandler(
filename = function() {
paste("Download", Sys.date(), ".csv", sep = "")
},
content = function(file) {
write.csv(data(), file, row.names = FALSE)
}
)
}
#Launch shiny app
shinyApp(ui = ui, server = server)
You can remove duplicates using dplyr::distinct. It'll only keep the first instance of the ID and remove others. In your case add this before return(dd) in data reactive -
if("ID" %in% names(dd)) {
dd <- dplyr::distinct(dd, ID, .keep_all = T)
}
I wanted to create a shiny app that gets data from user and saves it in .csv formats. But each time the user enters the data past data is not removed so when appending all the previous data(not necessarily for same user) gets appended with the new data that has been entered. I wanted to create a new "new" button which deletes past input data and gives a new table for input.
library(shiny)
ui <- fluidPage(
titlePanel("Creating a database"),
sidebarLayout(
sidebarPanel(
textInput("name", "Company Name"),
textInput("income", "Income"),
textInput("expenditure", "Expenditure"),
dateInput("date", h3("Date input"),value = Sys.Date() ,min = "0000-01-01",
max = Sys.Date(), format = "dd/mm/yy"),
actionButton("Action", "Submit"),#Submit Button
actionButton("new", "New")),
mainPanel(
tableOutput("table"), #Table showing what is there in the data frame
textInput("filename", "Enter Filename for download"), #filename
helpText(strong("Warning: Append if want to update existing data.")),
downloadButton('downloadData', 'Download'), #Button to save the file
downloadButton('Appenddata', 'Append') #Button to update a file
)
)
)
# Define server logic
server <- function(input, output){
#Global variable to save the data
Data <- data.frame()
Results <- reactive(data.frame(input$name, input$income, input$expenditure,
as.character(input$date),
as.character(Sys.Date())))
#To append the row and display in the table when the submit button is clicked
observeEvent(input$Action,{
#Append the row in the dataframe
Data <<- rbind(Data,Results())
#Display the output in the table
output$table <- renderTable(Data)
})
observeEvent(input$new, {
UpdateInputs(CreateDefaultRecord(), session)
})
output$downloadData <- downloadHandler(
# Create the download file name
filename = function() {
paste(input$filename , ".csv", sep="")
},
content = function(file) {
write.csv(Data, file,row.names = FALSE) # put Data() into the download file
})
output$Appenddata <- downloadHandler(
# Append data
filename = function() {
paste(input$filename, ".csv", sep="")
},
content = function(file) {
write.table( Data, file=file.choose(),append = T, sep=',',
row.names = FALSE, col.names = FALSE)
})
}`enter code here`
# Run the application
shinyApp(ui = ui, server = server)
The code works perfectly and i can enter new data as and when i want fine but when i press the "new" button so as to clear the data so that i want to enter new set of details the app closes down giving the following error
Warning: Error in UpdateInputs: could not find function "UpdateInputs"
The warning does not close the app until the new button is pressed. What is it that i am missing? Please help. Thank You.
If you just want to clear the data you could substitute
observeEvent(input$new, {
UpdateInputs(CreateDefaultRecord(), session)
})
with
observeEvent(input$new, {
#Append the row in the dataframe
Data <<- NULL
#Display the output in the table
output$table <- renderTable(Data)
})
although I'm not sure why you are saving Data globally using <<- and this method would obviously clear it.