Let's take a look at one of the demos.
runExample("09_upload")
I am using the supplied file to perform some computations and to display an aggregated performance across all uploaded files. Therefore, I use something like
tryCatch(compute.smth(), error=function(e){})
so that the displayed result is not affected by the bad input. However, I'd like to indicate somehow that uploading the bad file lead to an error, notifying the user about the problem with his input. It'll be something like
tryCatch(compute.smth(), error=badFile())
where badFile() should modify some displayable output. Any ideas?
As a last resort, this is probably an option, but I'd like some native Shiny.
You can show alerts like below with the ShinySky package: https://github.com/AnalytixWare/ShinySky
You can install the package using
install.packages("devtools")#if not alrady installed
devtools::install_github("ShinySky","AnalytixWare")
Place a shinyalert(id) in the ui.R for where you want the alert to appear.
In your server.R
Make sure you have a 3 parameters funciton being passed to shinyServer e.g.shinyServer(function(input, output,session) {
use showshinyalert(id,HTMLText,session) to show the alert. Clicking on the alert will dismiss it.
See this gist for example https://gist.github.com/xiaodaigh/7707701
Run this too see an example
shinysky::run.shinysky.example()
Related
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.
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.
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.
I would like to have the Shiny app remember which tab was selected at the time of a session$reload() (refreshing the page) to clear all inputs except the value of the current tab selected in a navbarPage().
The only idea that comes to mind, though I don't know how I could implement this, would be to assign a global variable tabIndicator as the value of the current tab selected (how do you get this value?), then on refresh, rm() all variables in global environment except tabIndicator, and set selected = tabIndicator in navbarPage().
How can I accomplish this?
You have two options, both are beyond the basics of shiny.
You could use cookies. Every time a new tab is entered, send a message to javascript and set a cookie. When the app initializes, check for the presence of the cookie, and if it's set then read its value and change to the appropriate tab
Use the shinyStore package, which gives you the ability of leveraging HTML5's local storage (but this won't work on older browsers because HTML5 is relatively new)
I don't have time to provide full code right now, but hopefully this helps
It's doable, I think, but tricky. The issue is that session$reload() is equivalent to hitting the refresh button in the browser, so you're creating a new session, and losing all of the context. I guess you could use cookies somehow to do it, but I'd recommend using an actionButton that resets all of your inputs using updateWhatever, instead of actually doing the session$reload().
I was able to figure it out using a global variable.
I put this code at the top of my app (before ui and server)
# Clear global environment except 'currentTab'
rm(list=setdiff(ls(), 'currentTab'))
# If current tab exists, restore to that tab. Otherwise, start at home screen
if(!exists('currentTab')){
currentTab <- 'HOMEPAGE'
}
And this code at the bottom of my server
# Current tab
observe({
currentTab <<- input$navbar
})
Works like a charm. Thanks to all for the help.
I have a shell command that I am running in a RealBasic App, and until now I've just been reading the output, but it requires user input. Is there something I could use to embed something like a terminal or a console application that could run a command, show the output, and take the input, maybe in a widget looking like a text area, like many IDEs and code editors have?
There is no pre-built control in RealStudio to accomplish this. However it's trivial to implement with a TextArea control and a Shell object set to Mode=2.
An example of this is included in the RealStudio Examples directory in your RealStudio install directory (by default on Windows, C:\Program Files\REAL Software\Real Studio\Examples\Shell\Interactive Shell.rbp.)
Could you separate the output and input, or would this not make sense for your use case?
If you could, then you could simply use a TextArea to display the console app's output, just keep appending to the TextArea's text. Then use a TextField for single line input underneath the TextArea, or whatever else makes sense for entering the params you need to send to the console app.
Then you can use a button (or catch [RETURN] key up in TextField) to grab the input and pass on to the console app.