Formatting number output of sliderInput in shiny - r

A good example of my question is found in the Movie Review example in the Shiny Gallery here:
http://shiny.rstudio.com/gallery/movie-explorer.html
There is a slider on the left hand side panel called "Year Released" which ranges between 1940 and 2014.
Here is the code for it from the ui.R
sliderInput("year", "Year released", 1940, 2014, value = c(1970, 2014))
You will notice that the formatting of the years when you use the slider are of the form:
"1,940" "2,014" with a comma separating the first digit from the last three.
I know that one can use dateRangeInputin Shiny but this requires the variable to be in a date format. For data variables that are simply numbers referring to years such as this example, is there a simple way to format the number output to remove the comma? I can't seem to figure this out, something that seems simple.

More recent releases of shiny throw the following error if one uses a format argument:
The format argument to sliderInput is deprecated. Use sep, pre,
and post instead. (Last used in version 0.10.2.2)
Now the preferred argument is evidently sep = ""

NOTE: This answer was written for older versions of shiny. If you are using version 0.11 or higher consult #duhaime 's answer instead.
In the sliderInput add a format="####" argument. That will remove the comma.
While it would not make sense for years, you can also use the format argument to do things like control how decimals are displayed.# indicates a number should be shown when present and 0 indicates a number should be shown if present and a 0 will be shown if it is absent.
For example "####.00" will show number with two decimal places, no comma, but will not show the thousands place if it is not there. With this format the number 32.6 would appear as 32.60.

Related

Reading dates as dates and not characters

The data I have been working with reads everything just fine, **except** for the date column. It always reads it as characters instead.
This would be fine except that, when I have lots of dates (like over 400 of them), then you can see something like this on a scatterplot:
Scatter Plot
In essence, I have two questions.
The first is, apart from using as.Date, which is fine when I'm needed temporary stuff, how do I permanently make R read the date column as legit dates? What I mean is, is there a way I can make that date column read as dates when I am using read.csv or read.excel?
When graphing, like the graph I have included here, how can I only include some of the labels throughout so that it won't be so cramped up? I still want all the data, but really do not want all those labels.
I was hoping to add the data file, but I am unaware of how to add excel/csv files on this website and my data set is quite long (n = 491). I do have 9 columns, 1 of which is the date column. The others are numbers or actual letters (the latter of which is in fact a character). I can add maybe a few rows just to help out.
Some of the data set

Ignoring errors when Converting Characters to Date

I have a Character Column that primarily contains dates in a DateNum pattern (e.g. 41269, 44294, 43057 etc). Unfortunately, due to a lack of validation at the source there are also some erroneous value in there, such as "-" or "03/012025"
I am trying to convert these into a standard date, while ignoring the invalid values. I don't want to lose the rows containing the data but I am happy to lose the value or replace it with NA
At the moment I have tried 2 methods to achieve this but have not been able to to get either to work properly
My approach at the moment is to find a way to only format those values that are Numeric, rather than havign to list all of the invalid characters (as the ones mentioned above may not cover all of them)
To this end I have been using the following code:
transformed_data$Expiry_Date<-format(as.Date(transformed_data$Expiry_Date ,optional=TRUE, tryFormats = "", origin="1899-12-30"))
However I do not seem able to find an example of the format to include in tryFormats that would only format Numeric values. All examples I have found so far refer to specific DDMMYY formats
I also looked at using NA_IF to cleanse the data before formatting it as a date (see below)
transformed_data$Expiry_Date<-na_if(transformed_data$Expiry_Date, is.numeric(transformed_data$Expiry_Date))
However, this does not appear to have any effect on the results.
I would like the values that are currently in a DateNum style to be displayed as a date, and any invalid data to be replaced by NA (or left blank)
As suggested here is a code snippet that produces illustrates the problem.
x <- c("41269","-","44294","03/012025","43057")
y<-format(as.Date(as.integer(x), origin="1899-12-30"))
The above includes the suggested change by #Rohit to include "as.integer". While this works it also generates a warning "NAs introduced by coercion"
Following the suggestion of #Rohit I have included "as.integer" to achieve the desired output, using the following code:
x <- c("41269","-","44294","03/012025","43057")
y<-format(as.Date(as.integer(x), origin="1899-12-30"))
While this produces a warning regarding "NAs introduced by coercion" this does not impact the outcome. If desired I can also use suppressWarnings to prevent the warning being displayed (see below)
y<-suppressWarnings(format(as.Date(as.integer(x), origin="1899-12-30")))
Thanks for helping me resolve this matter

r coding issue with frequency and date recognition

I'm having a few issues with my r scripting. I have 2 issues:
I'm able to read my data as a read.csv file, but it does not recognize the dates, it recognizes them as numeric.
When I ask for the frequency it says that the frequency is one when it is 365.
D.H.ASTRA <- ts(read.csv("highastra.csv"),start=c(1993,5, frequency=365))
the frequency is one when it is 365
Take a look at the parentheses in the code start=c(1993,5, frequency=365). frequency should be outside the parentheses, since it is an argument all by itself, not part of start.

r shiny slider range to graph

edit: to clarify, the function to make my choropleth graph require "region" (state) and "value" columns, and simply color codes the region based on where value falls on the scale.
I want to make the "value" column dynamic and have the graph reference the dynamic column
I am not sure if I can read from an output object
I have a slider range from 2000-2014, and columns x2000-x2014.
I want the slider to change the data being graphed, so if I choose 2002-2010 it shows that data, etc.
It's a choropleth graph showing % change between the two years, so if I choose 2004 and 2007 on the slider I want it to pull (x2007-x2004)/x2004. I can get it to change to X2004 (low<- paste0("X", input$range[1])) but I cant really do df$low.
If I read the question correctly then you are able to create a character string with the name of the column in the data frame that you want to access (low is the name of the column in data frame df), but your attempts to access that column using df$low is not working. Is that correct?
If so, then there is actually a fortune about this:
> library(fortunes)
> fortune('toad')
The problem here is that the $ notation is a magical shortcut and
like any other magic if used incorrectly is likely to do the
programmatic equivalent of turning yourself into a toad.
-- Greg Snow (in response to a user that wanted to access a
column whose name is stored in y via x$y rather than x[[y]])
R-help (February 2012)
The answer to your question is in the bottom part of that quote and is detailed on the help page help('$') and in section 6.1 of An Introduction to R.

Plotting Time Series

I'm working on 16 world indices over three year and i want to make a plot from these 16 indices.
all<-read.table("C.../16indices.txt")
dimnames(all)[[2]]<-c("Date","BEL 20","CAC 40","AEX","DAX","FTSE 100","IBEXx 35","ATX","SMI","FTSE MIB","RTX","HSI","NIKKEI 225","S&P 500","NASDAQ","Dow Jones","BOVESPA")
attach(all)
Problems
My dates are written in the form "2009-01-05". I want only "2009" to appear otherwise i would have to many jumps.
For example the prices from the BOVESPA go from 40.000,15 to 60.000,137. How do I get nice y-labels? For instance 40.000, 45.000,...,60.000.
How do i get 16 of these plots in one nice figure/plot?
I'm not used to work with R. I tried something like this but that didn't work...
plot(all[1,],all[,2])
Biggest problem is no sample data> Here is advice based on guesswork:
I tried something like this but that didn't work... plot(all[1,],all[,2])
You need to format your date values as R Date class. If they are in YYYY-MM-DD format it will be as simple as:
all$Date <- as.Date(all.Date)
To your specific questions:
1) My dates are written in the form "2009-01-05". I want only "2009" to appear otherwise i would have to many jumps.
You will need to suppress axis plotting in the plot call and then need to add an axis() call.
2) For example the prices from the BOVESPA go from 40.000,15 to 60.000,137. How do I get nice y-labels? For instance 40.000, 45.000,...,60.000.
You appear to be in a European locale and that mean your initial read.table call probably mangled the data input and you need to read the documentation for read.csv2 which will properly handle the reversal of the decimal point and comma meanings for numeric data. You should also use colClasses.
3) How do i get 16 of these plots in one nice figure/plot?
You should probably calculate ratios from an initial starting point for each series so there can be a common scale for display.

Resources