Auto-Update Data for R Shiny Dashboard - r

so I thought I had figured this out thanks to previous help (Updating R shiny data), but I have come across more issues. I didn't notice these issues since I got busy doing other things in my job. But turns out it is not auto-updating the app on shinyapps.io. I'll post the test code I have made in the server section:
dftest<-reactiveFileReader(1000,session,"Crop Registration 2022.xlsx",readFunc = read_excel,sheet = "CSV")
output$table2<-renderTable(
{
dftest2<-filter(dftest(),Variety=="Test1", Week>25 )
dftest2
}
)
What I found is after publishing the app on shinyapps.io any changes made to the excel sheet "CSV" do not show up. What I want is I can add data to the original excel sheet the app on shiny apps auto updates with the new data. Am I approaching this problem completely wrong? Or is it even possible via shinyapps.io ?

Related

Integrating Seurat CellSelector function inside a shiny app

I'm writing a ShinyApp which presents Seurat data. I want the app users to be able to select specific cell for later comparison and analysis. I want the cellSelcetor to be activated when a button is pressed, so I tried:
observeEvent(input$selectCells, {
output$clusterCompPlot <- renderPlot({CellSelector(plot = chosenClusterPlot)})
})
The problem is, I think, that cellSelector is already based on shiny, so I'm currently getting the following error:
Error in shiny::runApp: Can't call runApp() from within runApp(). If your application code contains runApp(), please remove it.
Is there any way to integrate this feature into my app?
Thanks!

Is it possible to clear the Shiny cache when reloading an app?

Is it possible to clear the Shiny cache history when reloading an app? Specifically, I have a textInput for adding a title that doesn't clear when the app is restarted. In other words, all of the titles entered into the app on previous runs have been "remembered" and show up in a dropdown list when the app is rerun or restarted again. I have tried using shinyjs::reset, but that clears the current value and not the ones in the "history". I've also tried updateTextInput with an action button set to " ", but also clears only the current value and not the ones in the "history". I have also tried setting ui as a function using:
ui <- function(req) {
fluidPage(...)
}
as well as:
I found that cache of R Shiny server is updated when the date of creation for the file "app.R" is changed.
So, here is the trick I used:
server <- function(input, output, session) {
Trick file date creation update
onStop(function() {
# File name
p <- paste0(getwd(), "/app.R")
# Update file 'date creation'
Sys.setFileTime(p, now())
}) # onStop
} # server
The idea is to update the date of "app.R" creation after each session.
Neither of these solutions worked for me. Finally, I found this post:
I also found this post:
I have been struggling with this problem for quite a while, and thought I had tried everything, including putting a js button on the shiny sidebar to manually refresh (unfortunately that did not work either). There are two things that did work for me:
Make sure all code to read data from files is NOT in a code chunk with a name OTHER THAN global OR
Manually restart the shiny server when new data is uploaded
Obviously the first one is much more manageable, and a solution that I wish I had known weeks ago when I started playing with workarounds.
But I am not sure what is involved in implementing either, so if someone could clarify that, that would be great.
Any help would be much appreciated.

R Shiny - Run application in background and issue UI controls with code

I am writing a vignette for my Shiny application package. At the beginning of my vignette, I source a file called screenshots.R that produces nice screenshots of my application. I am producing them like so:
webshot::appshot(mypackage::run_datepicker_app(),
file = "man/figures/datepicker.png", vwidth = 500, vheight = 200)
This works great and it gives me a great screenshot of what is - in this case - a couple dateInput fields. However, I'd like to be able to get a screenshot of the dateInput in use (say, with the calendar selection exposed).
Is there a way to issue commands to the application object in a script so I can get screenshots of the application in use, rather than having to do it manually?
Have you tried using ShinyDriver from the shinytest package?
You can use shinytest to have a headless browser run the app, interact with it, and take screenshots programmatically. If you don't have phantomJS installed, you'll need to run shinytest::installDependencies() before using ShinyDriver. All you need to do is point it to a directory containing a shiny app (in my case, the folder is 'myApp').
install.packages("shinytest")
shinytest::installDependencies()
app <- shinytest::ShinyDriver$new("myApp")
app$takeScreenshot("screenshot1.png")
button <- app$findElement("#button")
button$click()
Sys.sleep(1)
app$takeScreenshot("screenshot2.png")
app$stop()
I am starting the app in a headless browser, taking a screenshot, finding the button with the id 'button', clicking it, and taking another screenshot, then closing the app. Navigate to specific elements using "#id", where id is just the id you gave the shiny input. You can specify a file path to a png file in the takeScreenshot calls, so that you can then use them in your code elsewhere. Note that you may need to use Sys.sleep to stop the screenshots from being taken before the UI updates.

Automatically reloading shiny app when error occurs

This is a follow-up for my previous question Automatically reloading shiny app when add changes.
The solution options(shiny.autoreload = TRUE) works perfect when you want to automatically see changes on the browser which you put in the code.
However, the potential problem occurs when you save unfinished/corrupted file. For example, the following ui code lacks a , sign after the titlePanel function:
fluidPage(
titlePanel("Old Faithful Geyser Datass")
sidebarLayout(...
When you save such a file, you will get an error on your browser
ERROR: Error sourcing your_path/ui.R
R console will help detect the problem with the , sign. My impression was that if I improve my code and save the file, it should reload the browser and show my app correctly. Unfortunately, it doesn't do it.
Interesting thing is that an error in the app does not terminate the connection with the browser. To confirm my word, just reload the app manually using reload button in the browser (after improving your code).
Therofore, I examined how this shiny.autoreload option works. As I expected, it checked the time of file modification and then execute reload function. Then reload function send a message via sendMessage to the addMessageHandler:
addMessageHandler('reload', function(message) {
window.location.reload();
});
So it seems that after improving your code the function should be reexecuted, but it's not gonna happen.
To summarise, I think it's not possible to change it without major changes in shiny but maybe I am wrong. Thanks for any suggestion.
PS.You can manipulate example code here to see the problem.

Saving text input from shiny permanently?

We are building a Shiny app and plan to share the link to shinyapps.io.
We are wondering if there is any way to collect feedback from users - e.g. is there a way to have a text input field and permanently save the inputs for us?
Many thanks!
There is this project: ShinyChat which can be used as starting point for user feedback collection system.
Link to app: Live Chat
So in theory you need to have global reactiveValues() where you store your log.Rds and then you add user input to that log file. You may want to use R package stringgr. Example code:
library(stringgr)
log <- reactiveValues() #This have to be outside shinyServer so that all users can see it
shinyServer(function(input, output, session) {
addFeedBack <- function(file, string) {
...
return(modifiedFile)
}
observe({
log$logfile <- addFeedBack(log$logfile, input$userFeedback)
})
}
EDIT:
I did some research and actually there is really nice article and example in an official shiny page: Share data So you will encounter some problems if you are planning to host your app on ShinyApps.io. and article gives solution for that.

Resources