I'm new to R but I was given the task to create a working download button that would download the data within the queried table, to a .csv file. Currently, I'm having trouble understanding what I should pass on to my write.csv() function. I know I have to make the queried table reactive but I'm also unsure how to do that. The error I'm getting with my current code is "
cannot coerce class ‘c("datatables", "htmlwidget")’ to a data.frame"
I've tried passing my df() function as an argument but it doesn't work.
#ui
column(3,
conditionalPanel("input.table.period != '1980'",
# only prompt for rcp if a future period (not historical)
# input: select rcp
selectInput("table.rcp", "Emissions Scenario:",
c("Medium" = "45",
"High" = "85")),
downloadButton('downloadData', 'Download')
)
#server
server <- function(input, output) {
currentdf <- reactive({
# build query based on user-selections
if (input$table.stype == "ann") {
col.name <- columns.annual[grep(input$table.var, columns.annual)]
if (input$table.period == "1980") {
query <- paste0("SELECT ", col.name, ", subbasin, gcm_id FROM hydro_ann WHERE (period = ", input$table.period,
")")
} else {
query <- paste0("SELECT ", col.name, ", subbasin, gcm_id FROM hydro_ann WHERE (period = ", input$table.period,
") AND (rcp = ", input$table.rcp, ")")
}
} else {
col.name <- columns.month[grep(input$table.var, columns.month)]
if (input$table.period == "1980") {
query <- paste0("SELECT ", col.name, ", subbasin, gcm_id, calendar_month FROM hydro_month WHERE (period = ", input$table.period,
")")
} else {
query <- paste0("SELECT ", col.name, ", subbasin, gcm_id, calendar_month FROM hydro_month WHERE (period = ", input$table.period,
") AND (rcp = ", input$table.rcp, ")")
}
}
df <- dbGetQuery(db, query)
DT::df
})
output$querytable <- renderTable({ DT::datatable(currentdf()) })
output$downloadData <- downloadHandler(
filename = function() {
paste("QueriedData", "csv", sep = ".")
},
content = function(file) {
write.csv(as.data.frame(currentdf()),file)
}
I have tried a solution width Iris dataset which can give you option to select datatype you want to save before downloading data as per option you selected...
library(shiny)
library(shinydashboard)
library(DT)
library(datasets)
library(xlsx)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
# Boxes need to be put in a row (or column)
fluidRow(
box(
DTOutput("dtable")
)
)
)
)
server <- function(input, output) {
myModal <- function() {
div(id = "Download_DATA",
modalDialog(downloadButton("download1","Download iris as CSV"),
br(),
br(),
downloadButton("download2","Download iris as XLSX"),
easyClose = TRUE, title = "Download Table")
)
}
output$dtable <- renderDT(
datatable(iris,
extensions = 'Buttons',
options = list(
dom = 'Bfrtip',
buttons = list(
list(
extend = "collection",
text = 'Download',
action = DT::JS("function ( e, dt, node, config ) {
Shiny.setInputValue('Download_DATA', true, {priority: 'event'});
}")
)
)
)
)
)
observeEvent(input$Download_DATA, {
showModal(myModal())
})
output$download1 <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(iris, file)
}
)
output$download2 <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".xlsx", sep="")
},
content = function(file) {
write.xlsx(iris, file)
}
)
}
shinyApp(ui, server)
Please let me know....
Related
I am trying to build a shiny platform that can take in an input file, paste the output into a table on display, and then have a download button to download the results of the file back to your computer. I've tried numerous different ways, and it hasn't worked.
UI Code:
tabItem(tabName = "home",
h2("Cancer Publications Compiler"),
sidebarLayout(position = "left",
# File Upload
sidebarPanel(
radioButtons("data_t", "Select the Data Type Here",
c(Excel = ",", "Text = "\t")),
fileInput("data_f", "Upload Your Data Here")),
# Download Input
mainPanel(width = 8,align = "center",
div(style = "border:1px black solid;width:90%;font-size:10px;",tableOutput("out_chart")),
downloadButton("downloadData", "Download")))
),
Server:
fileext = reactive({
switch(input$data_t,
"Excel" = "csv", "Text" = "txt")
})
## Create Output for file selector ##
data_file <- reactive({
if(is.null(input$data_f)){return()}
else{
file_spec <- input$data_f
aa <- read.table(file_spec$datapath, header = TRUE, sep = input$data_t)
return(aa)
}
})
# Connects the input and output to launch the data table
## Create Output for table from tool ##
output$out_chart <- renderTable({
if(is.null(input$data_f)){return()}
else {
data_file()
}
})
output$donwloadData <- downloadHandler(
filename = function(){
paste("data-", fileext(), sep = ".")
},
content = function(file){
sep <- switch(input$data_t, "Excel" = ",", "Text" = "\t")
write.table(data_file(), file, sep = sep,
row.names = FALSE)
})
Can anyone help me with a fix for this problem so that the download handler will work how I want it to?
UPDATE: I have edited the code and have updated it on here. My problem now is that when I click download to download the outputted table, I am getting a .htm download of a very low rendered and weird looking version of my webpage.
You had some typos, and other issues. Try this
ui <- fluidPage(
tabItem(tabName = "home",
h2("Cancer Publications Compiler"),
sidebarLayout(position = "left",
# File Upload
sidebarPanel(
radioButtons("data_t", "Select the Data Type Here", c("Excel" = "csv", "Text" = "txt")) ,
fileInput("data_f", "Upload Your Data Here")
),
# Download Input
mainPanel(width = 8,align = "center",
div(style = "border:1px black solid;width:90%;font-size:10px;",tableOutput("out_chart")),
downloadButton("downloadData", "Download"))
)
)
)
server<- function (input, output, session) {
sep <- reactive({
req(input$data_t)
switch(input$data_t,
"csv" = ",", "txt" = "\t")
})
## Create Output for file selector ##
data_file <- reactive({
if(is.null(input$data_f)){return()}
else{
file_spec <- input$data_f
aa <- read.table(file_spec$datapath, header = TRUE, sep = sep())
return(aa)
}
})
# Connects the input and output to launch the data table
## Create Output for table from tool ##
output$out_chart <- renderTable({
print(input$data_t)
if(is.null(input$data_f)){return()}
else {
data_file()
}
})
output$downloadData <- downloadHandler(
filename = function(){
paste("data-", input$data_t, sep = ".")
},
content = function(file){
write.table(data_file(), file, sep = sep(), row.names = FALSE)
}
)
}
shinyApp(ui = ui, server = server)
I'm modularizing a Shiny app I developed using shinydashboard packages. Despite it traditionally works when I use it without involving modules, I can't make it work when I try to divide it into modules and submodules. Here I would like to combine two UIs (one for the sidebar, one for the body) in order to upload a dataset from the sidebar and show it into the body.
I'd be very glad if anybody could provide me some help with this.
Here is the code of the general Shiny app:
library(shiny)
library(excelR)
library(vroom)
library(readxl)
library(janitor)
library(dplyr)
library(shinydashboard)
library(shinydashboardPlus)
# # load separate module and function scripts
source("modules.R")
# app_ui
app_ui <- function() {
tagList(
shinydashboardPlus::dashboardPagePlus(
header = shinydashboardPlus::dashboardHeaderPlus(title = "module_test",
enable_rightsidebar = FALSE),
sidebar = shinydashboard::dashboardSidebar(
shinydashboard::sidebarMenu(id = "tabs",
import_sidebar_ui("import"))
),
body = shinydashboard::dashboardBody(shinydashboard::tabItems(
import_body_ui("import"))
),
rightsidebar = NULL,
title = "Module App"
)
)
}
# app_server
app_server <- function(input, output, session) {
shiny::moduleServer(id = "import", module = import_server)
}
####################################################################
run_app <- function(...) {
shiny::shinyApp(
ui = app_ui,
server = app_server)
}
#---------------------------------
run_app()
and here is the modules.R file I wrote containing the UIs for sidebar and body, plus the server:
# Import module ####
#
# Import sidebar UI
import_sidebar_ui <- function(id) {
ns <- NS(id)
shinydashboard::menuItem("Module Testing",
tabName = "tab_testing_mod",
icon = icon("th"),
tagList(
selectInput(ns("input_type"),
"Type of file:",
choices = c("Choose one" = "",".csv" = "csv",
".txt" = "txt", ".xls/.xlsx" = "xlsx"),
selected = NULL),
uiOutput(ns("inputControls")),
fileInput(ns("file"), "Data", buttonLabel = "Upload..."),
checkboxInput(ns("rownames"), "Check if 1st column contains rownames"),
checkboxInput(ns("constant"), "Remove constant columns?"),
checkboxInput(ns("empty"), "Remove empty cols?"),
actionButton(ns("bttn_import"), "Import data")
)
)
}
# Import body UI
import_body_ui <- function(id) {
ns <- NS(id)
shinydashboard::tabItem(tabName = "tab_testing_mod",
fluidRow(
h3("Imported Data"),
excelR::excelOutput(ns("preview")))
)
}
# Import server
import_server <- function(input, output, session) {
ns <- session$ns
output$inputControls <- renderUI({
tagList(
switch(input$input_type,
"csv" = textInput("delim", "Delimiter (leave blank to guess)", ""),
"txt" = textInput("delim", "Delimiter (leave blank to guess)", "")
),
switch(input$input_type,
"xlsx" = numericInput("sheet", "Sheet number", value = 1))
)
})
raw <- reactive({
req(input$file)
if (input$input_type == "csv" || input$input_type == "txt") {
delim <- if (input$delim == "") NULL else input$delim
data <- vroom::vroom(input$file$datapath, delim = delim)
} else if (input$input_type == "xlsx") {
data <- tibble::as.tibble(readxl::read_excel(input$file$datapath, sheet = input$sheet, col_names = TRUE))
} else {
return(NULL)
}
raw <- data
raw
})
tidied <- eventReactive(input$bttn_import,{
out <- raw()
if (input$empty) {
out <- janitor::remove_empty(out, "cols")
}
if (input$constant) {
out <- janitor::remove_constant(out)
}
if (input$rownames) {
out <- tibble::column_to_rownames(out, var = colnames(out[1]))
}
out <- out %>% dplyr::mutate_if(is.character,as.factor)
out
})
output$preview <- excelR::renderExcel({
excelR::excelTable(data = raw(),
colHeaders = toupper(colnames(raw())),
fullscreen = FALSE,
columnDrag = TRUE,
rowDrag = TRUE,
wordWrap = FALSE,
search =TRUE,
showToolbar = TRUE,
minDimensions = c(ncol(raw()),10)
)
})
}
It seems to me I can upload the dataset (.csv, .txt or .xlsx) files but I can't show it into the body.
I'd be very glad if you can help me, thank you very much in advance for your assistance.
I am developing a shiny app which stores the arbitrary data. I have referred This link for the same. I can able to display all the responses including previous responses, but i want to display only the current response, not all response. My code snippet is as given below:
library(shiny)
outputDir <- "C:\\Users/dell/Desktop/"
saveData <- function(data) {
data <- as.data.frame(t(data))
if (exists("responsesiq")) {
responsesiq <<- rbind(responsesiq, data)
} else {
responsesiq <<- data
}
fileName <- "test_irty.csv"
write.csv(
x = responsesiq, sep = ",",
file = file.path(outputDir, fileName),
row.names = FALSE, quote = TRUE
)
}
loadData <- function() {
if (exists("responsesiq")) {
responsesiq
}
}
fields <- c("acc", "staff_name")
shinyApp(
ui = fluidPage(
titlePanel(" System"),
DT::dataTableOutput("responsesiq", width = 300), tags$hr(),
numericInput("acc", "Acc Number", ""),
selectInput("staff_name", "Staff Name",
c("Rajivaksh " = "RT",
"Arvind " = "AKS",
"Ashutosh " = "AS")),
actionButton("submit", "Submit")
),
server = function(input, output, session) {
formData <- reactive({
data <- sapply(fields, function(x) input[[x]])
data
})
observeEvent(input$submit, {
saveData(formData())
})
# Show the previous responses
# (update with current response when Submit is clicked)
output$responsesiq <- DT::renderDataTable({
input$submit
loadData()
})
}
)
Create reactiveValues to hold the current entry, which will trigger when clicking submit
shinyApp(
ui = fluidPage(
titlePanel(" System"),
DT::dataTableOutput("responsesiq", width = 300), tags$hr(),
numericInput("acc", "Acc Number", ""),
selectInput("staff_name", "Staff Name",
c("Rajivaksh " = "RT",
"Arvind " = "AKS",
"Ashutosh " = "AS")),
actionButton("submit", "Submit")
),
server = function(input, output, session) {
formData <- reactive({
data <- sapply(fields, function(x) input[[x]])
data
})
tmp <- reactiveValues(df=NULL)
observeEvent(input$submit, {
saveData(formData())
tmp$df <- t(formData())
})
# Show the previous responses
# (update with current response when Submit is clicked)
output$responsesiq <- DT::renderDataTable({
input$submit
#loadData()
data.frame(tmp$df)
})
}
)
I am developing a shiny application which save the data entered on the user interface. I have refered the url on shiny rstudio page so by using this page, the code i have written is as mentioned below:
outputDir <- "C:\\Users/dell/Desktop/"
saveData <- function(data) {
data <- t(data)
fileName <- sprintf("%s_%s.csv", as.integer(Sys.time()), digest::digest(data))
write.csv(
x = data, sep = ",",
file = file.path(outputDir, fileName),
row.names = FALSE, quote = TRUE
)
}
loadData <- function() {
files <- list.files(outputDir, full.names = TRUE)
data <- lapply(files, read.csv, stringsAsFactors = FALSE)
data <- do.call(rbind, data)
data
}
library(shiny)
fields <- c("name", "staff_name")
shinyApp(
ui = fluidPage(
titlePanel("attendance System"),
DT::dataTableOutput("responses", width = 300), tags$hr(),
textInput("name", "Accession Number", ""),
selectInput("staff_name", "Staff Name",
c("Rajiv" = "RT",
"Arvind " = "AKS",
"Ashutosh " = "AS")),
actionButton("submit", "Submit")
),
server = function(input, output, session) {
formData <- reactive({
data <- sapply(fields, function(x) input[[x]])
data
})
observeEvent(input$submit, {
saveData(formData())
})
output$responses <- DT::renderDataTable({
input$submit
loadData()
})
}
)
The above code create a new file for each entry. I am looking for a single file in which all entry to be added.
This will give you a unique file name based on time of save and content of the file:
fileName <- sprintf("%s_%s.csv", as.integer(Sys.time()), digest::digest(data))
You can give it a single name like:
fileName <- 'input_bu.csv'
Like #ismirsehregal, I'd recommend bookmarking for this though.
after looking various solutions. I reached at below code to save the data in a single file as it is entered.
library(shiny)
outputDir <- "C:\\Users/dell/Desktop/"
saveData <- function(data) {
data <- as.data.frame(t(data))
if (exists("responsesiq")) {
responsesiq <<- rbind(responsesiq, data)
} else {
responsesiq <<- data
}
fileName <- "test_igntu.csv"
write.csv(
x = responsesiq, sep = ",",
file = file.path(outputDir, fileName),
row.names = FALSE, quote = TRUE
)
}
fields <- c("acc", "staff_name")
shinyApp(
ui = fluidPage(
titlePanel("Attendance System"),
DT::dataTableOutput("responsesiq", width = 300), tags$hr(),
numericInput("acc", "AccNumber", ""),
selectInput("staff_name", "Staff Name",
c("Rajiv" = "RT",
"Arvind" = "AKS",
"Ashutosh" = "AS")),
actionButton("submit", "Submit")
),
server = function(input, output, session) {
# Whenever a field is filled, aggregate all form data
formData <- reactive({
data <- sapply(fields, function(x) input[[x]])
data
})
# When the Submit button is clicked, save the form data
observeEvent(input$submit, {
saveData(formData())
})
}
)
Quite New to R here and not an experienced coder. I want to create a simple function to upload an external file via R shiny, but the file will not have fixed number of columns and may or may not have a column name.
In the case where the raw file does not have a header, I wish to force the name of the first column to be "Date" and the remaining columns 2,3,4...,n to be "Investment 1","Investment 2","Investment 3",...,"Investment n-1" respectively
Here's my current code, server side,
server <- function(input, output) {
rawdata <- reactive({
file_to_read = input$file
if(is.null(file_to_read)) {
return()
}
data <- read.table(file_to_read$datapath, sep = input$sep, header =
input$dataheader)
})
addcolumn <- reactive({
if(input$dataheader = FALSE) {
paste("Date",colnames(rawdata()[,1]))
for (i in 2:ncol(rawdata())) {
paste("Investment " + i, colnames(rawdata()[,i]))
}
}
})
output$datatable <- renderTable({
If(input$dataheader = FALSE) {
addcolumn(rawdata())
} else {
rawdata()
}
})
and ui side,
dashboardBody(
tabItems(
tabItem(
tabName = "import",
fluidRow(
box(
title = "Instructions",
solidHeader = TRUE,
width = 12,
status = "warning",
height = 120,
textOutput("instructionsImport")
),
box(
solidHeader = FALSE,
width = 3,
status = "primary",
fileInput("file","Choose a file to upload"),
radioButtons("sep","Separator",choices = c(Comma = ",", Space = " ",Period = ".", Tilde = "~", minus = "-")),
checkboxInput("dataheader","File has header?")
),
box(
title = "Uploaded Data",
solidHeader = TRUE,
width = 9,
status = "primary",
tableOutput("datatable")
)
)
),
Ideally, I would like the operation of adding column name to be done once after import and the resulting data table created instead of making a reactive function for adding column.
Thanks
Just change your server.R code to that :
server <- function(input, output) {
rawdata <- reactive({
file_to_read = input$file
if(is.null(file_to_read)) {
return()
}
data <- read.table(file_to_read$datapath, sep = input$sep, header =
input$dataheader)
if(!input$dataheader){
colnames(data)<-c("Date",paste("Investment",1:(ncol(data)-1)))
}
return(data)
})
output$datatable <- renderTable({
rawdata()
})
}