I have created a demo app that allows the user to edit an existing df and then download the updated table as a .csv. The app runs almost fine, as when I click the download button, I get the following error:
Warning: Error in as.data.frame.default: cannot coerce class ‘"dataTableProxy"’ to a data.frame
[No stack trace available]
How can this be fixed?
Code
# Double click in a table cell to edit its value and then download the updated table
library(shiny)
library(DT)
library(tidyverse)
# Define UI for application that edits the table
ui = fluidPage(
DTOutput('x1'),
# App title
titlePanel("Downloading Data"),
# Sidebar layout with input and output definitions
sidebarLayout(
# Sidebar panel for inputs
sidebarPanel(
# Input: Choose dataset
selectInput("dataset", "Choose a dataset:",
choices = c("Demo Table")),
# Button
downloadButton("downloadData", "Download")
),
# Main panel for displaying outputs
mainPanel(
tableOutput("table")
)
))
# Define server logic required
server = function(input, output) {
x = iris
x$Date = Sys.time() + seq_len(nrow(x))
output$x1 = renderDT(x, selection = 'none', editable = TRUE)
proxy = dataTableProxy('x1')
observeEvent(input$x1_cell_edit, {
info = input$x1_cell_edit
str(info)
i = info$row
j = info$col
v = info$value
x[i, j] <<- DT::coerceValue(v, x[i, j])
replaceData(proxy, x, resetPaging = FALSE) # important
})
# Downloadable table (df) as csv
output$downloadData = downloadHandler(
filename = function() {
paste(input$dataset, ".csv", sep = "")
},
content = function(file) {
write.csv(proxy, file, row.names = FALSE)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
Replace
write.csv(proxy, file, row.names = FALSE)
with
write.csv(x, file, row.names = FALSE)
This is not a full answer!
I know where the error occurs and I can fix it. But I can not make the downloadhandler to download the datatable (instead an empty csv file is occuring).
This might be due to:
input$dataset is not defined in your code. Here is a similar post handling this issue:
Shiny App Upload and Download Data Dynamically Not Working
And I think more important as stated by Stephane Laurent in his answer:
Replace data in R formattable datatable using replaceData function
replaceData requires a dataframe in the second argument, not a datatable.
This is the reason you get the error.
When you have a datatable proxy, the dataframe is in proxy$x$data.
Using this code removes the error, but a blank .csv file is downloaded
# Downloadable table (df) as csv
output$downloadData = downloadHandler(
filename = function() {
paste(input$dataset, ".csv", sep = "")
},
content = function(file) {
write.csv(proxy$proxy, file, row.names = FALSE)
}
)
}
Related
In running the below "abbreviated code", I'm trying to create a file upload validation that the csv to be uploaded must have "Scenario 1" and "Scenario 1" in cells A1 and B1 of the csv. Otherwise the file isn't uploaded and it is flagged "invalid". Any ideas of how to do this?
If you run the below, click the single action button and save the matrix inputs by clicking the button in the bottom of the modal dialog, look at the downloaded csv, and see how cells A1 and B1 show "Scenario 1" and "Scenario 1" from the downloaded matrix. This is good. If you delete these, and save the csv, you'll see that this modified csv can still be uploaded when running the App. I'd like those 2 csv fields to serve as a validation flag.
I really like the try() function as a catch-all test in this App.
Abbreviated code:
library(dplyr)
library(shiny)
library(shinyFeedback)
library(shinyMatrix)
sumMat <- function(x){return(rep(sum(x,na.rm = TRUE), 10))}
ui <- fluidPage(
useShinyFeedback(),
sidebarLayout(
sidebarPanel(
actionButton("matrix3show","Click for matrix input"),
),
mainPanel(plotOutput("plot"))
)
)
server <- function(input, output, session) {
uploadMat3Data <- reactive({
req(input$uploadMat3)
validate(need(identical(tools::file_ext(input$uploadMat3$datapath),"csv"),"Invalid"))
try(read.csv(input$uploadMat3$datapath, header = TRUE))
})
observeEvent(uploadMat3Data(), {
if(is.data.frame(uploadMat3Data())){
updateMatrixInput(session,"matrix3",as.matrix(uploadMat3Data()))
hideFeedback("file")
}
else {showFeedbackWarning("file", "Invalid")}
})
observeEvent(input$matrix3show,{
showModal(
modalDialog(
fileInput(inputId = "uploadMat3",label = NULL,accept = ".csv"),
matrixInput(
inputId = "matrix3",
value = if(is.null(input$matrix3)){matrix(c(1,5),ncol=2,dimnames=list(NULL,rep("Scenario 1",2)))}
else {input$matrix3},
rows = list(extend = TRUE, delete = TRUE),
cols = list(extend = TRUE, delta = 2, delete = TRUE, multiheader = TRUE),
class = "numeric"),
output$verbMat3 <- renderPrint(class(uploadMat3Data())),
footer =
tagList(
downloadButton("saveMat3","Save",style = "width:80px;"),
modalButton("Exit box")
) # close tag list
))
})
observeEvent(input$matrix3, {
tmpMat3 <- input$matrix3
colnames(tmpMat3) <- paste("Scenario",rep(1:ncol(tmpMat3),each=2,length.out=ncol(tmpMat3)))
rownames(tmpMat3) <- paste("Row", seq_len(nrow(input$matrix3)))
updateMatrixInput(session,inputId="matrix3",value=tmpMat3)
})
data <- function(){tibble(X = seq_len(10),Y = sumMat(input$matrix3))}
output$plot<-renderPlot({plot(data(),type="l")})
output$saveMat3 <- downloadHandler(
filename = function(){paste("Inputs","csv",sep=".")},
content = function(file){write.csv(input$matrix3, file,row.names=FALSE)}
)
}
shinyApp(ui, server)
To resolve I changed the observeEvent for uploadMat3Data() to the following:
observeEvent(uploadMat3Data(), {
if(is.data.frame(uploadMat3Data())
&& colnames(uploadMat3Data()[1]) == "Scenario.1"
&& colnames(uploadMat3Data()[2]) == "Scenario.1.1"
){
updateMatrixInput(session,"matrix3",as.matrix(uploadMat3Data()))
hideFeedback("uploadMat3")
}
else {showFeedbackWarning("uploadMat3", "Invalid")}
})
Note the additions of && colnames(... where the code peeks into the headers for the uploaded data frame and checks for the required headers. Also note that in the original code the id references in hideFeedback() and showFeedbackWarning() were incorrect; they are now corrected to "uploadMat3"
I am using a downloadHandler in a shiny app which let's me download 4 reactive dataframes that has been created. I want to include a functionality if either one of the dataframes has not been created then that will be printed in renderPrint/renderText saying one of the dataframes is missing. A minimal example from the server code is given below but it does not work the way I want it to.
output$dl <-
if((df1()!= '') || (df2()!='') || (df3()!= '') || (df4()!= '') ){
downloadHandler(
filename = "New_Data.xlsx",
content = function(file){
write_xlsx(list("S1" = df1(), "S2" = df2(), "S3" = df3(), "S4" = df4()), path = file)
}else{
output$dl_error <- renderPrint({'One of the dataframes is missing'})
}
)
or something like this
data_list <- reactive({
list("S1" = df1(),
"S2" = df2(),
"S3" = df3(),
"S4" = df4())
})
output$dl <- reactive({
if(length(data_list()==4)){
downloadHandler(
filename = "New_Data.xlsx",
content = function(file){
write_xlsx(data_list(), path = file)
}
)
}else{
output$dl_error <- renderPrint({'One of the dataframes is missing'})
}
})
It will be great if someone can help me out with this. Also, it would be better if the error message in the renderPrint/renderText shows which dataframe is missing.
As said in my comment, you could disable the button. You can use the shinyjs package to do that easily. Another option is to hide the download button with a conditionalPanel. Here is a third option. I hide the download button with a conditionalPanel and instead I display a "fake" download button. When the dataframe is available, the fake data button is hidden and the true download button becomes visible. If the user clicks on the fake download button, he is told that the dataframe is not available with an alert.
library(shiny)
library(shinyalert)
library(writexl)
ui <- fluidPage(
useShinyalert(),
br(),
conditionalPanel(
condition = "!output.ok",
actionButton("fake", "Download", icon = icon("save"))
),
conditionalPanel(
condition = "output.ok",
style = "display: none;",
downloadButton("dwnld", "Download", icon = icon("save"))
),
br(),
actionButton("databtn", "Generate dataframe")
)
server <- function(input, output, session){
observeEvent(input[["fake"]], {
shinyalert(
title = "Error!",
text = "The dataframe is not ready yet",
type = "error"
)
})
df <- reactiveVal(NULL)
observeEvent(input[["databtn"]], {
df(iris)
})
output[["ok"]] <- reactive({
!is.null(df())
})
outputOptions(output, "ok", suspendWhenHidden = FALSE)
output[["dwnld"]] <- downloadHandler(
filename = "iris.xlsx",
content = function(file){
write_xlsx(list(iris = df()), path = file)
}
)
}
shinyApp(ui, server)
I'm building a little applet that will run locally, where people can upload a csv and a fillable pdf and the tool will execute a loop that will fill out the pdfs with names from the csv and save them as png files in an /output directory.
I am having trouble with the pdf portion. Using shinyFiles they navigate to the pdf and get its path, but am getting an invalid path error trying to get the pdf fields (staplr). I think it is happening with get_fields but I can't think of another way to get the pdf location.
Warning: Error in path.expand: invalid 'path' argument
[No stack trace available]
Code snip below. Any ideas welcome!
library(tidyverse)
library(staplr)
library(DT)
library(shinyFiles)
library(pdftools)
ui <- fluidPage(
titlePanel(p("Award PDF Creation App", style = "color:#3474A7")),
sidebarLayout(
sidebarPanel(
p("Award Creation Tool"),
# Horizontal line ----
tags$hr(),
fileInput(inputId = "filedata",
label = "Choose your CSV file",
accept = c(".csv")),
shinyFilesButton("pdf", "PDF select", "Please select a PDF", multiple = TRUE, viewtype = "detail"),
tags$p(),
tags$p('Please choose the fillable PDF for award creation.'),
tags$hr()
),
mainPanel(h3("Review your CSV format before you Create PDFs"),
DTOutput(outputId = "table"),
tableOutput("contents"),
actionButton("go", "Create PDFs")
)
)
)
server <- shinyServer(function(input, output, session){
volumes <- c(Home = fs::path_home(), "R Installation" = R.home(), getVolumes()())
shinyFileChoose(input, "pdf", roots = volumes, session = session,
filetypes = c('', 'pdf'))
# by setting `allowDirCreate = FALSE` a user will not be able to create a new directory
pdf <- reactive(input$pdf)
data <- reactive({
req(input$filedata)
read.csv(input$filedata$datapath)
})
pdfpath <- reactive({
req(input$pdf)
as.character(parseFilePaths(volumes,pdf())$datapath)
})
output$table <- renderDT(
data()
)
observeEvent(input$go,{
req(input$filedata)
req(input$pdf)
data <- data()
pdffields <-get_fields(input_filepath = pdfpath, convert_field_names = F)
withProgress(message = 'Making PDFs', value = 0, {
for(i in 1:nrow(data)){
pdffields$`Date`$value <- paste(format(data$AWARD_DATE[i], "%B %d, %Y"))
pdffields$`First Name Last Name`$value <- paste0(data$FIRST_NAME[i], " ", data$LAST_NAME[i])
filename <- paste0('./output/', Sys.Date(),
'_', data$LAST_NAME[i],
'_', data$AWARD[i], '.png')
set_fields(pdf, filename, pdffields)
bitmap <- pdf_render_page(filename, page = 1, dpi = 300)
png::writePNG(bitmap, filename)
# Increment the progress bar, and update the detail text.
incProgress(1/nrow(data), detail = paste("Processing"))
# Pause
Sys.sleep(0.1)
}
})
})
})
shinyApp(ui = ui, server = server)
I'm pretty new to shiny (being playing around for about a week). And I'm trying to create an app that takes and input tab-separated text file and perform several exploratory functions. In this case I'm presenting a very simplified version of that app just to highlight what I want to do in a specific case:
Problem:
If you try the app with the sample data (or any data in the same format) you can notice that the app effectively performs the default summary table (if selectInput="summarize", then output$sumfile), but when you try to select "explore", the previous table gets removed from the mainPanel, and outputs the full file (selectInput="explore",then output$gridfile) in the place where it would be as if selectInput="summarize" was still selected.
If you re-select "summarize", excelOutput("sumfile") gets duplicated on the mainPanel.
My goal is simple:
excelOutput("sumfile") when selectInput="summarize" ONLY and
excelOutput("gridfile") when selectInput="explore" ONLY
without placement issues or duplications on the mainPanel
So far I've tried:
inFile=input$df
if(is.null(inFile))
return(NULL)
if(input$show=="summarize")
return(NULL)
or
inFile=input$df
if(is.null(inFile))
return(NULL)
if(input$show=="explore")
return(NULL)
To control what shows up on the mainPanel, but with placement and duplication issues.
sample data:
#Build test data
testdat<-data.frame(W=c(rep("A",3),
rep("B",3),
rep("C",3)),
X=c(letters[1:9]),
Y=c(11:19),
Z=c(letters[1:7],"",NA),
stringsAsFactors = FALSE)
#Export test data
write.table(testdat,
"your/path/file.txt",
row.names = FALSE,
sep = "\t",
quote = FALSE,
na="")
shiny app (app.R):
library(shiny)
library(excelR)
#function to summarize tables
Pivot<-function(df){
cclass<-as.character(sapply(df,
class))
df.1<-apply(df,
2,
function(x) unlist(list(nrows = as.numeric(NROW(x)),
nrows.unique = length(unique(x))-(sum(is.na(x))+length(which(x==""))),
nrows.empty = (sum(is.na(x))+length(which(x==""))))))
df.2<-data.frame(df.1,
stringsAsFactors = FALSE)
df.3<-data.frame(t(df.2),
stringsAsFactors = FALSE)
df.3$col.class<-cclass
df.3$col.name<-row.names(df.3)
row.names(df.3)<-NULL
df.3<-df.3[c(5,4,1,2,3)]
return(df.3)
}
ui <- fluidPage(
ui <- fluidPage(titlePanel(title=h1("Summary generator",
align="center")),
sidebarLayout(
sidebarPanel(
h3("Loading panel",
align="center"),
fileInput("df",
"Choose file (format: file.txt)",
accept = c("plain/text",
".txt")),
selectInput("show",
"Choose what to do with file",
choices=c("summarize","explore")),
p("**'summarize' will output a summary of the selected table"),
p("**'explore' will output the full selected editable table"),
tags$hr()
),
mainPanel(
excelOutput("gridfile"),
excelOutput("sumfile")
))))
server <- function(input, output) {
dat<-reactive({
fp<-input$df$datapath
read.delim(fp,
quote="",
na.strings="\"\"",
stringsAsFactors=FALSE)
})
#get summary
output$sumfile<-renderExcel({
inFile=input$df
if(is.null(inFile)) #if fileInput is empty return nothing
return(NULL)
if(input$show=="explore") #if selectInput = "explore" return nothing
return(NULL)
dat.1<-data.frame(dat())
dat.2<-Pivot(dat.1)
excelTable(dat.2,
defaultColWidth = 100,
search = TRUE)
})
#get full file
output$gridfile<-renderExcel({
inFile=input$df
if(is.null(inFile)) #if fileInput is empty return nothing
return(NULL)
if(input$show=="summarize") #if selectInput = "summarize" return nothing
return(NULL)
dat.1<-data.frame(dat())
excelTable(dat.1,
defaultColWidth = 100,
search = TRUE)
})
}
shinyApp(ui = ui, server = server)
One way to do what you want is to use observeEvent for your inputs input$show and input$df and renderExcel based on your selection of `input$show. Here is an updated version for your code:
library(shiny)
library(excelR)
#function to summarize tables
Pivot<-function(df){
cclass<-as.character(sapply(df,
class))
df.1<-apply(df,
2,
function(x) unlist(list(nrows = as.numeric(NROW(x)),
nrows.unique = length(unique(x))-(sum(is.na(x))+length(which(x==""))),
nrows.empty = (sum(is.na(x))+length(which(x==""))))))
df.2<-data.frame(df.1,
stringsAsFactors = FALSE)
df.3<-data.frame(t(df.2),
stringsAsFactors = FALSE)
df.3$col.class<-cclass
df.3$col.name<-row.names(df.3)
row.names(df.3)<-NULL
df.3<-df.3[c(5,4,1,2,3)]
return(df.3)
}
ui <- fluidPage(
ui <- fluidPage(titlePanel(title=h1("Summary generator",
align="center")),
sidebarLayout(
sidebarPanel(
h3("Loading panel",
align="center"),
fileInput("df",
"Choose file (format: file.txt)",
accept = c("plain/text",
".txt")),
selectInput("show",
"Choose what to do with file",
choices=c("summarize","explore")),
p("**'summarize' will output a summary of the selected table"),
p("**'explore' will output the full selected editable table"),
tags$hr()
),
mainPanel(
excelOutput("gridfile"),
excelOutput("sumfile")
))))
server <- function(input, output) {
dat<-reactive({
fp<-input$df$datapath
read.delim(fp,
quote="",
na.strings="\"\"",
stringsAsFactors=FALSE)
})
observeEvent({
input$show
input$df
}, {
inFile=input$df
if(is.null(inFile)) #if fileInput is empty return nothing
return(NULL)
if(input$show=="explore") {
output$gridfile<-renderExcel({
dat.1<-data.frame(dat())
excelTable(dat.1,
defaultColWidth = 100,
search = TRUE)
})
}
if(input$show=="summarize") {
output$sumfile<-renderExcel({
dat.1<-data.frame(dat())
dat.2<-Pivot(dat.1)
excelTable(dat.2,
defaultColWidth = 100,
search = TRUE)
})
}
})
}
shinyApp(ui = ui, server = server)
Hope it helps!
I have created an R shiny application to download dynamic reports using R Markdown. Previously I was downloading one report at a time by selecting the row in the data table in r shiny and clicking on download button, the selected row's column values would get filled in the report, this was working perfectly fine.
But now i am trying to download multiple reports, so that if I select multiple rows in a datatable in r shiny and click on download, the number of reports downloaded should be equal to number of rows selected.
For this I am trying to create a zip file which contains all my individual report but I am getting this
error: pandoc document conversion failed with error 1
I had researched for this error but couldn't find anything. Please help!
ui <- {
tagList(
div(id = "downloadBtn",
downloadButton("downloadData", "Download")),
DT::dataTableOutput('myTable1')
)
}
dataJ <- read.csv(file = "iris.csv", header = TRUE, stringsAsFactors =
FALSE)
server <- function(input, output)
{
output$myTable1 <- DT::renderDataTable({
DT::datatable(dataJ, options = list(orderClasses = TRUE), filter = 'top')})
output$downloadData <- downloadHandler(
filename = function()
{
paste("output", "zip", sep = ".")
},
content = function(file)
{
k = list(input$myTable1_rows_selected)
fs <- c()
for ( i in k)
{
params <- list(j=i)
path <- paste(i,".docx")
rmarkdown::render("R_markdown_script.Rmd", rmarkdown::word_document(),
output_file = path , params = params,
envir = new.env(parent = globalenv()))
fs <- c(fs,path)
}
zip(zipfile = file, files = fs)
if (file.exists(paste0(file, ".zip")))
file.rename(paste0(file, ".zip"), file)
},
contentType = "application/zip" )
}
runApp(list(ui = ui, server = server))
Here is a reproducible example (to make it work, create an rmarkdown file with the default content using RStudio, and save it as "test.rmd" in the same folder as your Shiny app).
Important:
You need to run the app externally inside your web browser. Somehow it does not work in the viewer pane or RStudio window (you get the download window but then no file is saved).
If you are on Windows, you need to make sure that you install RTools first, and also put the rtools/bin folder in your system path.
app.R
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
downloadButton("downloadData", "Download")
),
mainPanel(
DT::dataTableOutput('myTable1')
)
)
))
server <- shinyServer(function(input, output) {
output$myTable1 <- DT::renderDataTable(iris)
output$downloadData <- downloadHandler(
filename = function() {
paste0("output", ".zip")
},
content = function(file) {
k <- input$myTable1_rows_selected
fs <- c()
for (i in k) {
path <- paste0(i, ".docx")
rmarkdown::render("test.rmd", rmarkdown::word_document(), output_file = path)
fs <- c(fs, path)
}
zip(file, fs)
},
contentType = "application/zip"
)
})
shinyApp(ui = ui, server = server)
Hello I also installed Rtools/bin and was running the code on the web browser, but when I click on download button, download window doesn't comes up and shows '404 Not Found', but when I check the directory, the doc files report are saving directly to directory, no zip file is produced. Please see below code.
ui <- {
tagList(
div(id = "downloadBtn",
downloadButton("downloadData", "Download")),
DT::dataTableOutput('myTable1')
)
}
dataJ <- read.csv(file = "iris.csv", header = TRUE, stringsAsFactors =
FALSE)
server <- function(input, output)
{
output$myTable1 <- DT::renderDataTable({
DT::datatable(dataJ, options = list(orderClasses = TRUE), filter = 'top')})
output$downloadData <- downloadHandler(
filename = ("output.zip"),
content = function(file)
{
k <- (input$myTable1_rows_selected)
fs <- c()
for ( i in k)
{
path <- paste0(i,".docx")
rmarkdown::render("R_markdown_script.Rmd", output_file = path ,
params = list(j=i), envir = new.env(parent = globalenv()))
fs <- c(fs,file)
}
zip(zipfile = file, files = fs)
},
contentType = "application/zip" )
}
runApp(list(ui = ui, server = server))`