R, shiny, prevent error until field is calculated - r

I am building a shiny application, and I would like there to be a field like this that displays the probability someone will return (given a bunch of underlying models):
And it pretty much works, except its in decimal form:
The code pasting into that box looks like this:
paste(a$result)
I can get it to look "correct" and say '83%' for instance, instead of 0.83000.....
by using this code:
paste( c( round(a$result, digits=2))*100,"%")
But the problem is.... while this code does work, until you hit the "calculate" button, it looks like this:
I wish I could provide some sample data to try but given the interactiveness of the shiny app, that'd be very hard. Is there a simple solution?

Use the function shiny::req to make sure required values are present before performing the calculation. For example
paste(c( round(req(a$result), digits=2))*100,"%")

Related

Upload a .xlsx file to Shiny and load in all the tabs as a list [duplicate]

I have a shiny app and when I run it I get an error saying that an object of type ‘closure’ is not subsettable. What is that and how can I fix it?
Note: I wrote this question as this comes up a lot, and the possible dupes are either not shiny related or so specific that it is not obvious that the answers are broadly applicable.
See also this question which covers this error in a non-Shiny context.
How to fix this:
This is a very common error in shiny apps. This most typically appears when you create an object such as a list, data.frame or vector using the reactive() function – that is, your object reacts to some kind of input. If you do this, when you refer to your object afterwards, you must include parentheses.
For example, let’s say you make a reactive data.frame like so:
MyDF<-reactive({ code that makes a data.frame with a column called “X” })
If you then wish to refer to the data.frame and you call it MyDF or MyDF$X you will get the error. Instead it should be MyDF() or MyDF()$X You need to use this naming convention with any object you create using reactive().
Why this happens:
When you make a reactive object, such as a data.frame, using reactive() it is tempting to think of it as just like any other non-reactive data.frame and write your code accordingly. However, what you have created is not really a data.frame. Rather, what you have made is instructions, in the form of a function, which tell shiny how to make the data.frame when it is needed. When you wish to actually use this function to get the data.frame you have to use the parenthesis, just like you would any other function in R. If you forget to use the parenthesis, R thinks you are trying to use part of a function and gives you the error. Try typing:
plot$x
at the command line and you will get the same error.
You may not see this error right when your app starts. Reactive objects have what is called “lazy” evaluation. They are not evaluated until they are needed for some output. So if your data.frame is only used to make a plot, the data.frame will not exist until the user sees the plot for the first time. If when the app starts up the user is required to click a button or change tabs to see the plot, the code for the data.frame will not be evaluated until that happens. Once that happens, then and only then will shiny use the current values of the inputs to run the function that constructs the data.frame needed to make the plot. If you have forgotten to use the parentheses, this is when shiny will give you the error. Note that if the inputs change, but the user is not looking at the plot, the function that makes the data.frame will not be re-run until the user looks at the plot again.

How to get data from website with interaction

I'd like to get data with R from following website: Website MFarm
I'm wondering if there's a chance to get them from R or I must use something else (e.g. Selenium, or Python) ?
Note that:
Needed data is the plot trend. This shouldn't be an issue, as it's only about getting a html attribute, and then to re-elaborate numbers within R.
Data are shown in the page not immediately, but only some seconds after the page is loaded. So some waitfor() function shall be used
There are 7 tabs. I need data from all of them. An interaction is therefore needed.
There's a combo box and a text box with parameters I've to choose

bokeh update high level chart with javascript callback

I am using bokeh to plot high level chart (Line) with a dataframe .
I have also widget to filter data showed by the graph.
I know how to do it with basic glyphs but not with high level charts.
Indeed basic plots are using ColumnDataSource as input and a javascript callback can take it as argument and trigger it. But for a dataframe as input, it does not seem possible.
I have the following error if I want to pass in args the dataframe df:
ValueError: expected an element of Dict(String, Instance(Model)), got {'df': ......
Any idea?
Thanks.
David
I think the only way to do that is by re-creating the entire plot each time and replacing it. See https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/Hn14aDN_5lk for example. I would stick with using a ColumnDataSource.
I have also to precise that I don't want to use a bokeh server but a standalone solution.
As a workaround I tried to replace the dataframe by a dataframe generated by a columndatasource like this :
plot=Line(source.data,x='x',y='y')
or
plot=Line(source.to_df(),x='x',y='y')
with source=ColumnDataSource(df)
No more errors but nothing happens when triggering source in javascript callback.
Is it normal?
Thanks.
David
If you use
plot=Line(source.data,x='x',y='y') or plot=Line(source.to_df(),x='x',y='y')
then it is normal that your javascript callback doesn't trigger anything. Because you didn't pass any source to the line, you gave it the dictionary "data" from the source as defined in your python code at that time, and it will never change.
As said in the link of Okonomiyaki's answer, you should use bokeh.plotting if you want more interactions with js callbacks.

In R, looking for a more detailed str() showing full names or a tree

I want to change parts of a ggplot2 object made by a function and returned as a result, to remove the Y-axis label. No, the function does not allow that to be specified in the first place so I want to change it after the fact.
str(theObject) ## shows the nested structure with parts shortened to ".." and I want to be able to type something like:
theObject$A$B$C$myLabel <- ""
So how can I either make an str -like listing with full paths like that or perhaps draw a tree structure showing the inner working of the object?
Yes, I can figure things out using names(theObject) and finding which branch leads to what I am looking for, then switching to that branch and repeating but it looks like there could be a better automated way to find a leaf node such as:
leaf_str(obj=theObject, leaf="myLabel")
might return zero or more lines like:
theObject$A$B$C$myLabel
theObject$A$X$Y$Z$myLabel
Or, the entire structure could be put out as a series of such lines.
I have searched and found nothing quite like this. I can see lots of uses especially in teaching what an object is. Yes, S4 objects might also use # as well as $.
The
tree
function in the xfun package may be useful.
See here for more details
https://yihui.org/xfun/

Get Scilab to calculate without printing result

This sounds like a silly question, but I really can't find an answer around.
I'm using Scilab to evaluate two methods in terms of performace. However, every time I tell Scilab to calculate anything, it will print the results. Since I'm using large matrices, it spends much more time printing the results than doing the calculations, so I'm having a hard time telling how long is each method actually taking.
Can I get Scilab to compute something without printing the result?
That is, instead of
-->B = A'*A
A =
1. 2. 3.
2. 4. 6.
3. 6. 9.
-->
I'd like it to do
-->B = A'*A
-->
Also simply adding a semicolon works
-->B = A'*A;
-->
Well, I finally found the right query. When I searched for 'scilab silent', one of the results (not the first) was this:
http://help.scilab.org/docs/5.3.3/en_US/mode.html
Function mode(k) lets you choose how Scilab will behave in terms of variable display. The following call will temporarily hide results:
mode(-1)
Whereas this will get you back to the default option:
mode(2)
The documentation is confusing, though.
Please notices that mode does not used at prompt, only in an exec-file or a scilab function.
Aside from the awful English, this notice seems to be outdated. This function worked perfectly for me on the prompt.

Resources