How to modify button output after printing the screen (R shiny)? - r

I put a button in my shiny app, which download a table (called "tableau_final"), that i created in my code.
Howevever, when i print (on paper) the window of my app, i can see that my downloading button has sessions informations.
Is there a way to delete the session informations and just to see the download button like on the shiny app?
download button with session informations after printing the app window
My code is pretty basic :
output$Download_Tableau.xlsx <- downloadHandler(
filename = function() {
paste(input$type, ".xlsx", sep = "")
},
content = function(file){
fname <- paste(file,"xlsx",sep=".")
wb <- loadWorkbook(fname, create = TRUE)
createSheet(wb, name = "Detail")
writeWorksheet(wb, tableau_final, sheet = "Detail")
saveWorkbook(wb)
file.rename(fname,file)
}
)

Related

How to create a download link in shiny for a local excel workbook (not dataframe) via downloadHandler

ref: How to download workbook via downloadHandler on Shiny?
** Workbook is already held in the app's directory**
Does anyone have experience with putting in a download link into a shiny app for an excel file held in the app's directory? I have an excel form that I need users to be able to download and use (it is specifically NOT a dataframe).
The following code worked for when I put in a powerpoint file in the downloadHandler function:
output$downloadinflationguidance <- downloadHandler(
filename = function() {
paste("www/inflation-guidance - ", Sys.Date(), '.pptx', sep='')
},
content = function(con) {
pptx <- read_pptx("www/inflation-guidance.pptx")
print(pptx, target = con)
}
)
But when I swap out the powerpoint stuff for the excel form (see below) it doesn't work. When I run the app and click on the download link, the user gets an error saying "file not found". The excel file is held locally on the app in a folder titled "www". Am I missing a trick here?
shinyUI(
downloadLink("dl_excel_calc", label = "Excel Version")
)
shinyServer(function(input, output, session) {
output$dl_excel_calc <- downloadHandler(
filename = function() {
paste0("indexation_tool_excel -", Sys.Date(), ".xlsx", sep='')
},
content = function(con) {
xlsx <- read_excel('indexation_tool_excel.xlsx')
print(xlsx, target = con)
}
)
}

Download a zip file and extract a specific file in Shiny App

I have a RShiny app where I fetch a zip file from a s3 bucket using aws.s3 library. I have a specific file within this zip archive that users will download upon clicking downloadButton.
Below is a snippet from my server part of the code
rvalues <- reactiveValues(r = file())
observe({
rvalues$r <- tempfile(fileext = paste0(".", tools::file_ext("MyArchive.zip")))
r <- save_object(bucket = MyBucket,
object = "MyArchive.zip",
file = rvalues$r,
key = accesskey,
secret = secretKey,
region = region)
})
output$download <- downloadHandler(
filename = function() {
"Sample.json"
},
content = function(file) {
unzip(rvalues$r,"Sample.json")
}
)
I am creating a temp file and saving the zip from s3 to this temp file. From this temp file, I am unzipping my specific file and passing it to the download handler function. For some reason, this doesn't work. Any help/guidance is much appreciated!
I would try the following code (I have not tried it since you don't provide a reproducible example):
output$download <- downloadHandler(
filename = function() {
"Sample.json"
},
content = function(file) {
filepath <- unzip(rvalues$r,"Sample.json")
file.copy(filepath, file)
}
)

Downloading a file generated by shiny

I'm trying to download a BSON file that I export to my shiny app from a MongoDB using the mongolite package. This is the code in my download button:
output$downloadTiming <- downloadHandler(
filename = "/keyTiming.bson",
content = function(fileToDownload){
mongolite::mongo(
collection = "keyTiming",
url = "mongodb://<User>:<Pass>#<url>"
)$export(fileToDownload, bson = TRUE)
}
)
When I try to download it, it says "Error: inherits(con, "connection") is not TRUE". I have spent a good amount of time researching and have found nothing, and hope that someone here can be of use.
I figured it out eventually. The final code looks like this
output$downloadTiming <- downloadHandler(
filename <- function(){
return("timingOut.bson")
},
content <- function(file){
outFile = file("timingOut.bson")
mongolite::mongo(
collection = "timings",
url = "mongodb://<user>:<pass>#<database>"
)$export(outFile, bson = TRUE)
file.copy("timingOut.bson", file, overwrite = TRUE)
}
)

R Shiny downloadHelper multiple write.csv + file.copy

I am using Shiny to produce a data.frame which the users can download by clicking on a button:
server.R
output$downloadresults <- downloadHandler(
filename= function() {
paste(country, " - Provision report.csv", sep = "")
},
content=function(file) {
write.csv(sheet1_report,file)
}
)
When the user clicks the download button, I (simultaneously) want to copy a second file from my server to the same location to where they are downloading the data.frame.
Importantly, I am trying to avoid a secondary button.
So the idea is to add the line file.copy("C:/temp/asdf.csv", file):
output$downloadresults <- downloadHandler(
filename= function() {
paste("User_chosen_filename", sep = "")
},
content=function(file) {
write.csv(sheet1_report,file)
file.copy("C:/temp/asdf.csv", file) #NEW LINE
}
)
This is not working, R only executes the first write.csv line and ignores the file.copy.
I have tested the file.copy in isolation and it works.
What am I doing wrong?

Change tklabel to file name opened by tkbutton function in R

I am working on creating a widget in R that can open a file and show the characteristics of that file within the same widget. I want this information to be updated automatically when loading the file. This means that I have the following function for the button that can open the file:
getfile <- function() {
name <- tclvalue(tkgetOpenFile(
filetypes = "{{raster files} {.tiff .tif .img .grd}} {{All files} *}"))
if (name == "") return;
Sys.sleep(10)
assign("Filename", name, envir = .GlobalEnv)
tclvalue(Filename) <- name
}
the function for the button that opens this file is:
button.widget <- tkbutton(tt, text = "Select File", command = getfile)
tkgrid(button.widget, pady=10, padx=10, columnspan=3)
The label is given by:
Filename <- tclVar("")
label.widget <- tklabel(tt, text=tclvalue(Filename))
tkgrid(label.widget, row=2, column=0)
However, when I create the widget everything works, Filename is changed and a file can be opened. But the text in the label is not changed. How can i fix this? Is there an event that can be run when I close the OpenFile dialog window?
Try:
tklabel (tt, textvariable=Filename)
There is this example where he configures the label as text first then reconfigures it with a textvariable. But you should be able to configure it as a textvariable initially. Caveat: I don't know R.
The alternative option is to put:
tkconfigure(label.widget,text=tclvalue(Filename))
At the end of the getfile() function.

Resources