Saving output of splice() to a text file - r

I have the following script
library(zoo)
library(dplyr)
library(tidyverse)
dat<-read.csv("sep_2015.csv",header=F)
dat$date<-seq(from=as.Date("2015-09-01"),to=as.Date("2015-12-31"),by="day")
dat %>%
rownames_to_column() %>%
filter(V2 < 90 &
rollsum(V2 < 90, 7, fill = NA, align = "left") >=7 &
rollsum(V1 > 2, 7, fill = NA, align = "left") >=7 &
rollsum(V2 < 90, 20, fill = NA, align = "left") >= 15 ) %>%
slice(1)
The output is like this:
rowname V1 V2 date
1 41 5.583102 45.83082 2015-10-11
I am processing multiple files (from sep_1979 to sep_2020) with the following names:
.....
sep_2014.csv
sep_2015.csv
sep_2016.csv
....
Sample data (sep_2015.csv):
structure(list(V1 = c(1.781016934, 5.245650757, 5.253808428,
3.4500901, 1.904631502, 5.583155269, 5.29432007, 2.493240863,
2.356109739, 2.505279709, 3.100681377, 4.352358556, 9.021734968,
7.037204701, 1.946181317, 2.375415753, 4.818568252, 4.508796957,
5.009930139, 3.413858254, 2.642863447, 4.496943406, 1.101138556,
1.58130412, 5.907897257, 9.310602975, 12.8050039, 10.52127595,
10.58324383, 5.719938811, 5.65915423, 10.23184856, 8.916484733,
8.006767597, 7.623137477, 6.735120637, 3.913288259, 5.145787145,
5.516434408, 3.766181753, 5.583101736, 8.62718523, 8.55994889,
7.738160311, 9.428281922, 14.54588945, 21.1567956, 19.25095128,
11.18693546, 6.459297562, 8.113929381, 6.791538854, 2.30569946,
4.271416627, 7.043436661, 4.044279911, 4.841727481, 7.050044333,
8.646260752, 4.475859911, 9.545056312, 6.185563838, 7.545238783,
8.397502556, 6.124560982, 4.50970303, 6.682802556, 6.247809616,
5.986996325, 4.525555447, 8.719674593, 8.245588214, 6.028287485,
2.346912866, 2.842578055, 7.320520815, 6.337556706, 3.298882387,
4.512427524, 8.360934487, 9.222761141, 8.958611924, 7.982833144,
10.50259016, 8.866081434, 5.120412581, 11.46034903, 12.4205173,
10.25257175, 2.466221616, 2.692953769, 0.681709806, 1.362974813,
5.5663453, 9.601658711, 8.416295206, 3.412426219, 6.865540765,
11.76102145, 12.0902957, 6.870356614, 2.020952899, 4.363628479,
4.879838354, 4.059975788, 10.37719133, 10.37127041, 11.40995727,
10.7018141, 11.43079722, 6.58367129, 6.628593068, 2.18586382,
3.712579965, 2.874565445, 2.791741886, 4.21014224, 0.881548513,
8.251492289, 7.62408847, 5.940576571, 8.981119084), V2 =
c(121.0261069,
85.00304523, 81.87989778, 103.7505395, 210.2369147, 264.2405565,
282.9403406, 26.88123501, 19.2594093, 8.196100414, 45.26456729,
58.17439097, 87.16714037, 109.3834486, 114.840361, 38.91351028,
37.81463493, 24.49148024, 29.33726467, 13.51886983, 78.0736854,
41.91203091, 55.80655486, 338.9064404, 338.2241727, 4.297849857,
343.7324297, 266.8110122, 189.2300192, 118.7736246, 86.03606596,
120.0474015, 151.7655159, 85.86331853, 67.23147222, 51.7641341,
20.78357193, 271.0026858, 266.308435, 311.3070459, 45.83082398,
64.93928317, 63.04855403, 35.94151677, 43.73557838, 55.52061899,
55.9783368, 72.09428879, 81.11278896, 75.28379332, 56.17839757,
46.85929244, 27.56930947, 64.41308939, 75.61366497, 66.62037577,
43.60322015, 89.88800444, 101.8289993, 99.75479947, 80.43658344,
78.52117729, 83.81080108, 91.30824192, 96.42780607, 101.3614562,
118.3864844, 101.9711437, 103.8823967, 83.42613393, 74.27778556,
97.92986085, 113.0191678, 147.4666885, 134.7889272, 104.79725,
126.037476, 121.3500898, 83.21475735, 89.23421897, 92.92463803,
85.92837336, 80.02208464, 55.23356116, 52.95700516, 30.38190174,
64.76557575, 96.63946752, 86.73461083, 95.37929333, 62.17987768,
12.7633562, 273.7881121, 76.47255644, 113.7227032, 149.0157726,
78.67676081, 71.17172364, 89.33577561, 154.9133108, 221.6713695,
85.69030561, 78.51796584, 97.6857188, 78.00024078, 67.04777272,
66.22047934, 66.65994223, 68.62200021, 79.88214929, 84.2911525,
98.45874358, 175.7545009, 214.3253056, 277.4996992, 348.4682651,
97.83108644, 348.9214276, 49.4694104, 70.54130568, 79.26767621,
80.66615013), date = structure(c(16679, 16680, 16681, 16682,
16683, 16684, 16685, 16686, 16687, 16688, 16689, 16690, 16691,
16692, 16693, 16694, 16695, 16696, 16697, 16698, 16699, 16700,
16701, 16702, 16703, 16704, 16705, 16706, 16707, 16708, 16709,
16710, 16711, 16712, 16713, 16714, 16715, 16716, 16717, 16718,
16719, 16720, 16721, 16722, 16723, 16724, 16725, 16726, 16727,
16728, 16729, 16730, 16731, 16732, 16733, 16734, 16735, 16736,
16737, 16738, 16739, 16740, 16741, 16742, 16743, 16744, 16745,
16746, 16747, 16748, 16749, 16750, 16751, 16752, 16753, 16754,
16755, 16756, 16757, 16758, 16759, 16760, 16761, 16762, 16763,
16764, 16765, 16766, 16767, 16768, 16769, 16770, 16771, 16772,
16773, 16774, 16775, 16776, 16777, 16778, 16779, 16780, 16781,
16782, 16783, 16784, 16785, 16786, 16787, 16788, 16789, 16790,
16791, 16792, 16793, 16794, 16795, 16796, 16797, 16798, 16799,
16800), class = "Date")), row.names = c(NA, -122L), class = "data.frame")
I would like to:
(1) apply the above script for each file
(2) save the "date" from splice (1) into a text file
(3) the final output is a collection of dates from 1979 to 2020
How can I implement this in R? I'll appreciate any help on this.

You can try the following solution :
From each file name extract the year value.
Create sequence of dates for that year using paste and add it as a column in the data.
Filter the rows for the condition and select the 1st row from them.
Combine the result into one file.
library(tidyverse)
library(zoo)
all_files <- list.files(pattern = '^sep_.*\\.csv$')
map_df(all_files, function(file) {
year <- parse_number(file)
dates <- seq(as.Date(paste(year, 9, 1, sep = '-')),
as.Date(paste(year, 12, 31, sep = '-')), by = 'day')
dat <- read.csv(file,header=FALSE)
dat %>%
mutate(date = dates) %>%
filter(V2 < 90 &
rollsum(V2 < 90, 7, fill = NA, align = "left") >=7 &
rollsum(V1 > 2, 7, fill = NA, align = "left") >=7 &
rollsum(V2 < 90, 20, fill = NA, align = "left") >= 15) %>%
slice(1) %>%
transmute(date, file = file)
}) -> result
result should look something like this -
result
# date filename
#1979-10-10 sep_1979.csv
#1980-11-21 sep_1980.csv
#1981-09-10 sep_1981.csv
#
#
You can then write result into csv or text file as per your choice.
#CSV
write.csv(result, 'result.csv', row.names = FALSE)
#TEXT
write.table(result, 'result.txt', row.names = FALSE)

Related

Finding statistics after grouping in data.table

I had a small question in regards to data.table. Since i'm not so good at it i'm not quite sure how I can do this in data.table.
Basically I have 3 columns and want to group by the first two columns ( key and date ) and then for each key and each date, find the maximum and minimum that occurred in the third column ( fare)
I tried doing this but it gives me an error
flights[, c("max_day", "min_day") := unlist(lapply(gross_fare, findr)), by = c("key", "created_date")]
Error in `[.data.table`(flights, , `:=`(c("max_day", "min_day"), unlist(lapply(gross_fare, :
Supplied 18 items to be assigned to group 1 of size 9 in column 'max_day'. The RHS length must either be 1 (single values are ok) or match the LHS length exactly. If you wish to 'recycle' the RHS please use rep() explicitly to make this intent clear to readers of your code.
findr is a function which just finds the max and min i.e.
findr <- function(x) {list(max = max(x), min = min(x)}
I've done what I want to do in dplyr and I'll attach the code for that, but since i have millions of rows, dplyr eats up my ram so data.table would help
test <- flights %>%
select(key, created_date, gross_fare) %>%
group_by(key, created_date) %>%
summarise(
max_day = max(gross_fare),
min_day = min(gross_fare),
diff = max_day - min_day) %>%
arrange(created_date)
I've put the dput output if anyone wants to use that
If anyone can help that'd be great, thank you :)
data.table::setDT(structure(list(key = c("LHE_KHI_LHE+KHI_PA-405_15.0_1", "KHI_ISB_KHI+ISB_PK-370_20.0_0",
"LHE_KHI_LHE+KHI_PK-307_20.0_0", "ISB_KHI_ISB+KHI_PF-124_20.0_1",
"LHE_KHI_LHE+KHI_PK-307_20.0_0", "LHE_KHI_LHE+KHI_PA-405_15.0_1",
"KHI_LHE_KHI+LHE_PK-304_20.0_0", "KHI_ISB_KHI+ISB_PA-204_15.0_1",
"ISB_KHI_ISB+KHI_PA-207_15.0_1", "KHI_ISB_KHI+ISB_PA-200_20.0_1",
"KHI_LHE_KHI+LHE_PK-304_40.0_0", "ISB_KHI_ISB+KHI_PA-201_35.0_1",
"ISB_KHI_ISB+KHI_ER-501_20.0_1", "KHI_LHE_KHI+LHE_PF-145_20.0_2",
"KHI_ISB_KHI+ISB_PA-204_20.0_1", "LHE_KHI_LHE+KHI_PA-401_0.0_0",
"ISB_KHI_ISB+KHI_PK-309_40.0_0", "KHI_ISB_KHI+ISB_PF-123_20.0_2",
"ISB_KHI_ISB+KHI_PA-205_15.0_1", "LHE_KHI_LHE+KHI_PF-142_0.0_0",
"ISB_KHI_ISB+KHI_PA-223_15.0_1", "ISB_KHI_ISB+KHI_PF-126_20.0_2",
"ISB_KHI_ISB+KHI_PK-309_20.0_0", "KHI_ISB_KHI+ISB_PF-121_20.0_2",
"ISB_KHI_ISB+KHI_PK-373_20.0_0", "KHI_LHE_KHI+LHE_PF-145_20.0_2",
"KHI_LHE_KHI+LHE_PA-402_15.0_1", "LHE_KHI_LHE+KHI_PA-407_20.0_1",
"KHI_ISB_KHI+ISB_PK-308_40.0_0", "KHI_LHE_KHI+LHE_PF-145_20.0_2",
"LHE_KHI_LHE+KHI_PF-144_0.0_0", "ISB_KHI_ISB+KHI_PK-369_40.0_0",
"ISB_KHI_ISB+KHI_PF-124_20.0_2", "KHI_ISB_KHI+ISB_PA-204_15.0_1",
"KHI_ISB_KHI+ISB_PA-200_15.0_1", "ISB_KHI_ISB+KHI_PF-124_20.0_1",
"KHI_ISB_KHI+ISB_PK-300_20.0_0", "ISB_KHI_ISB+KHI_PF-122_20.0_2",
"KHI_ISB_KHI+ISB_PK-368_20.0_0", "KHI_ISB_KHI+ISB_PA-204_15.0_1",
"ISB_KHI_ISB+KHI_ER-503_20.0_1", "ISB_KHI_ISB+KHI_PA-209_15.0_1",
"KHI_ISB_KHI+ISB_PK-308_40.0_0", "ISB_KHI_ISB+KHI_PF-124_20.0_1",
"ISB_KHI_ISB+KHI_PK-301_40.0_0", "KHI_LHE_KHI+LHE_PA-408_35.0_1",
"LHE_KHI_LHE+KHI_PF-144_20.0_2", "KHI_ISB_KHI+ISB_PF-121_20.0_2",
"KHI_ISB_KHI+ISB_PA-204_35.0_1", "ISB_KHI_ISB+KHI_PK-309_40.0_0",
"ISB_KHI_ISB+KHI_PA-223_20.0_1", "KHI_ISB_KHI+ISB_PA-206_35.0_1",
"LHE_KHI_LHE+KHI_PF-142_32.0_1", "LHE_KHI_LHE+KHI_PF-142_20.0_1",
"KHI_ISB_KHI+ISB_PF-123_20.0_2", "ISB_KHI_ISB+KHI_PA-209_15.0_1",
"KHI_ISB_KHI+ISB_PA-204_35.0_1", "ISB_KHI_ISB+KHI_PA-201_20.0_1",
"KHI_ISB_KHI+ISB_PK-368_20.0_0", "ISB_KHI_ISB+KHI_PA-205_20.0_1",
"KHI_ISB_KHI+ISB_PF-121_20.0_1", "ISB_KHI_ISB+KHI_PF-124_20.0_1",
"ISB_KHI_ISB+KHI_PA-205_15.0_1", "KHI_LHE_KHI+LHE_PF-145_20.0_2",
"KHI_LHE_KHI+LHE_PA-406_35.0_1", "KHI_ISB_KHI+ISB_PK-308_20.0_0",
"LHE_KHI_LHE+KHI_PA-401_20.0_1", "LHE_KHI_LHE+KHI_PA-401_15.0_1",
"KHI_ISB_KHI+ISB_PA-204_35.0_1", "KHI_LHE_KHI+LHE_PA-406_35.0_1",
"KHI_ISB_KHI+ISB_PA-206_35.0_1", "KHI_ISB_KHI+ISB_PF-121_20.0_1",
"ISB_KHI_ISB+KHI_PA-205_20.0_1", "LHE_KHI_LHE+KHI_PF-142_20.0_1",
"LHE_KHI_LHE+KHI_PF-146_20.0_2", "LHE_KHI_LHE+KHI_PA-401_35.0_1",
"ISB_KHI_ISB+KHI_PA-209_15.0_1", "ISB_KHI_ISB+KHI_PK-301_40.0_0",
"ISB_KHI_ISB+KHI_PA-205_35.0_1", "KHI_LHE_KHI+LHE_PA-406_15.0_1",
"KHI_ISB_KHI+ISB_PF-123_20.0_1", "ISB_KHI_ISB+KHI_PA-201_35.0_1",
"KHI_ISB_KHI+ISB_PK-300_40.0_0", "KHI_LHE_KHI+LHE_PA-402_35.0_1",
"ISB_KHI_ISB+KHI_ER-505_20.0_1", "ISB_KHI_ISB+KHI_PF-122_20.0_2",
"ISB_KHI_ISB+KHI_PA-207_15.0_1", "KHI_LHE_KHI+LHE_PA-404_35.0_1",
"KHI_ISB_KHI+ISB_PF-123_20.0_1", "ISB_KHI_ISB+KHI_ER-503_20.0_1",
"ISB_GIL_ISB+GIL_PK-605_20.0_0", "KHI_ISB_KHI+ISB_PF-123_20.0_1",
"KHI_ISB_KHI+ISB_PA-200_15.0_1", "ISB_KHI_ISB+KHI_PF-122_20.0_2",
"KHI_LHE_KHI+LHE_PA-404_35.0_1", "ISB_KHI_ISB+KHI_PF-122_20.0_2",
"PEW_KHI_PEW+KHI_PF-152_20.0_1", "LHE_KHI_LHE+KHI_PK-303_20.0_0",
"KHI_ISB_KHI+ISB_PA-222_35.0_1", "ISB_KHI_ISB+KHI_PF-124_20.0_1"
), created_date = c("2021-04-20", "2021-05-27", "2021-02-13",
"2021-08-14", "2021-08-11", "2021-08-21", "2021-01-26", "2021-08-21",
"2021-05-24", "2021-09-15", "2021-06-05", "2021-07-19", "2021-09-29",
"2021-07-02", "2021-08-10", "2021-01-04", "2021-07-15", "2021-07-14",
"2021-08-13", "2021-01-11", "2021-09-13", "2021-09-20", "2021-05-27",
"2021-02-20", "2021-08-15", "2021-07-27", "2021-08-26", "2021-09-15",
"2021-08-02", "2021-06-25", "2021-05-15", "2021-08-26", "2021-07-30",
"2021-06-27", "2021-08-07", "2021-03-19", "2021-03-02", "2021-06-06",
"2021-08-15", "2021-06-27", "2021-09-19", "2021-07-28", "2021-08-09",
"2021-08-16", "2021-09-09", "2021-06-04", "2021-08-12", "2021-05-15",
"2021-07-26", "2021-05-27", "2021-08-12", "2021-08-02", "2021-01-26",
"2021-04-20", "2021-08-26", "2021-08-26", "2021-03-21", "2021-01-09",
"2021-04-23", "2021-01-04", "2021-08-13", "2021-06-22", "2021-05-31",
"2021-08-18", "2021-06-16", "2021-08-14", "2021-08-10", "2021-06-16",
"2021-04-08", "2021-05-20", "2021-06-22", "2021-04-20", "2021-01-05",
"2021-02-27", "2021-07-07", "2021-03-26", "2021-08-16", "2021-05-01",
"2021-07-31", "2021-06-14", "2021-06-16", "2021-03-25", "2021-09-14",
"2021-06-06", "2021-09-02", "2021-08-06", "2021-07-18", "2021-02-28",
"2021-04-28", "2021-09-19", "2021-08-25", "2021-06-17", "2021-06-07",
"2021-06-17", "2021-07-07", "2021-08-23", "2021-07-09", "2021-07-19",
"2021-07-14", "2021-05-21"), gross_fare = c(7796, 7427, 11504,
6870, 6580, 14945, 8697, 7524, 7124, 6785, 11858, 7524, 11500,
9525, 6785, 8739, 8200, 13560, 9045, 7400, 7524, 12500, 7458,
14000, 6570, 9525, 6220, 10545, 8310, 7900, 7820, 8410, 11285,
19892, 6810, 9800, 11441, 11900, 6570, 13592, 11500, 8300, 20380,
8525, 7340, 9707, 7870, 10655, 10545, 11798, 14645, 10545, 8650,
8650, 7870, 12945, 10799, 10227, 6765, 10227, 20120, 11045, 9403,
7870, 7124, 6570, 6810, 6531, 8605, 7124, 11072, 7390, 10227,
13435, 10530, 12280, 18945, 11147, 10545, 6531, 6620, 10799,
18480, 32702, 5606, 13560, 23895, 8027, 9655, 11500, 11990, 6620,
9403, 7620, 14645, 19105, 9000, 6440, 12645, 8025)), row.names = c(NA,
-100L), class = c("data.table", "data.frame")))
I guess this line of code should do the job:
library(data.table)
flights[, .(min_day = min(gross_fare), max_day = max(gross_fare), diff = max(gross_fare) - min(gross_fare)), by = .(key, created_date)][]
Since the function findr returns a list, there's no need to complicate things:
findr <- function(x) {list(max = max(x), min = min(x))}
flights[, c("max_day", "min_day") := findr(gross_fare), by = list(key, created_date)][]
To also return the difference between max and min, use
findr2 <- function(x) {
list(max = max(x), min = min(x), diff = diff(range(x)))
}
flights[, c("max_day", "min_day", "diff_day") := findr2(gross_fare), by = list(key, created_date)][]

maintain date class for x-axis with geom_smooth

I am trying to smooth three lines and shade the area between the min and max lines using this approach. My x-axis is a date class at the start, but I'm getting an error in the last step.
Invalid input: date_trans works with objects of class Date only
I'm losing the date class when smoothing into p1. How can I prevent this or otherwise pass a date to geom_ribbon in the final step?
library(tidyverse)
mmr <- structure(list(year = structure(c(3834, 3926, 3987, 4108, 4169,
4230, 4352, 4442, 4503, 4595, 4687, 4748, 4929, 4991, 5052, 5204,
5265, 5326, 5448, 5538, 5599, 5752, 5813, 5844, 6025, 6087, 6148,
6299, 6360, 6574, 6634, 6695, 6848, 6909, 7091, 7183, 7213, 7395,
7456, 7517, 7639, 7729, 7790, 7943, 8035, 8187, 8248, 8309, 8460,
8552, 8613, 8735, 8825, 8886, 9009, 9100, 9131, 9282, 9343, 9404,
9556, 9617, 9678, 9831, 9862, 9952, 10105, 10135, 10196, 10347,
10408, 10470, 10592, 10682, 10896, 10957, 11078, 11170, 11231,
11382, 11474, 11596, 11657, 11688, 11869, 11931, 11961, 12112,
12173, 12234, 12357, 12418, 12478, 12600, 12692, 12874, 12935,
12996, 13118, 13208, 13239, 13392, 13483, 13665, 13757, 13787,
13939), class = "Date"), mmrU = c(13.8231596, 13.7529708, 13.7148858,
13.6118164, 13.5638772, 13.5478975, 13.519933, 13.5012899, 13.474657,
13.4014166, 13.4173963, 13.3787786, 13.2882268, 13.2589306, 13.2354049,
13.1763686, 13.1470724, 13.1255442, 13.0618471, 13.032551, 13.0108008,
13.0139079, 13.0232294, 13.0352143, 13.0498623, 13.0498623, 13.0376556,
13.0232294, 13.0205662, 12.987275, 12.9739586, 12.9033814, 12.9792852,
12.99127, 13.0059181, 13.0059181, 13.0059181, 13.0205662, 13.0130202,
13.0034767, 12.9606421, 12.9406675, 12.9277949, 12.8727536, 12.8203756,
12.7728802, 12.7582321, 12.7569005, 12.7422524, 12.7382575, 12.7171414,
12.6397157, 12.601098, 12.5737993, 12.6583588, 12.7382575, 12.7979151,
12.9473257, 12.9965966, 13.0327729, 13.1191079, 13.1617205, 13.2061087,
13.332171, 13.39609, 13.4586773, 13.6477709, 13.7676189, 13.8408594,
14.0619124, 14.1604541, 14.2246394, 14.3815071, 14.4851535, 14.7996436,
14.9234866, 15.0436305, 15.3022444, 15.4277361, 15.7198102, 15.9745982,
16.2684478, 16.4362351, 16.5409911, 16.8277386, 16.9728879, 17.0634398,
17.3204472, 17.4616016, 17.5578129, 17.819814, 17.9623, 18.0631721,
18.3045327, 18.4784455, 18.6694034, 18.7612869, 18.8153517, 18.9250792,
18.9770134, 19.0189602, 19.0675652, 19.07955, 19.1008563, 19.0582437,
19.0292138, 18.9410589), mmr = c(12.1431454, 12.11401197, 12.05086241,
12.02612341, 11.96590348, 11.93823486, 11.8828976, 11.8601117,
11.8275604, 11.8047745, 11.7784079, 11.7461821, 11.6955796, 11.7256748,
11.6689467, 11.6396505, 11.5997012, 11.6033632, 11.5384455, 11.5144759,
11.4532202, 11.4532202, 11.5411088, 11.5198025, 11.4971645, 11.5118126,
11.5118126, 11.5345985, 11.5586865, 11.4532202, 11.4532202, 11.4692,
11.5144759, 11.5201829, 11.5494791, 11.525129, 11.5662198, 11.570405,
11.4665367, 11.4984962, 11.4345772, 11.4692, 11.4092759, 11.3893013,
11.3653317, 11.3799798, 11.4406647, 11.3014127, 11.2721165, 11.2833024,
11.2720179, 11.2161875, 11.1675824, 11.1749065, 11.1928097, 11.2546572,
11.3295252, 11.397883, 11.568574, 11.5020472, 11.5437721, 11.5792826,
11.6846601, 11.7494372, 11.8112847, 11.8868038, 12.0684401, 12.1749717,
12.2844995, 12.3747184, 12.4756571, 12.5664753, 12.7422524, 12.8274777,
13.114314, 13.2216445, 13.3164576, 13.575862, 13.7156847, 13.8555074,
14.1513989, 14.2922869, 14.4147983, 14.5559526, 14.8196183, 14.9421296,
15.0779574, 15.203132, 15.3283066, 15.4521496, 15.686519, 15.7970456,
15.9248835, 16.0292844, 16.21678, 16.3945102, 16.4693782, 16.5442463,
16.6028386, 16.661431, 16.7265336, 16.7734075, 16.8304019, 16.8407888,
16.781464, 16.7411817, 16.6532932), mmrL = c(10.68619282, 10.63692196,
10.5963068, 10.49976255, 10.45848156, 10.44250182, 10.4092107,
10.39189932, 10.38390945, 10.31732721, 10.28270445, 10.25695931,
10.20813234, 10.21612221, 10.22278043, 10.25207662, 10.26139813,
10.24719392, 10.21079563, 10.19481589, 10.17883615, 10.17883615,
10.17883615, 10.17883615, 10.1921526, 10.19481589, 10.20813234,
10.20946398, 10.22011714, 10.19481589, 10.18283109, 10.17883615,
10.17883615, 10.17883615, 10.17883615, 10.17484122, 10.16418806,
10.14554503, 10.13356023, 10.12024378, 10.09893747, 10.08828431,
10.0762995, 10.06165141, 10.04700332, 10.02436536, 9.999064105,
9.9810869, 9.936476799, 9.911175548, 9.890756995, 9.840598374,
9.815297123, 9.79798574, 9.880547718, 9.947129958, 9.997199802,
10.1149172, 10.14021845, 10.14953997, 10.18948931, 10.21345892,
10.24963527, 10.36659807, 10.42519044, 10.47179801, 10.63958525,
10.74478519, 10.82335224, 10.99261909, 11.11726527, 11.15293432,
11.27611147, 11.33869877, 11.52113411, 11.58638471, 11.6377862,
11.82075419, 11.96190854, 12.31346277, 12.45195383, 12.52985505,
12.74491568, 12.85810549, 12.93633962, 13.16971038, 13.29887992,
13.38676848, 13.61581138, 13.71834803, 13.78226698, 13.95005423,
14.03927443, 14.10159541, 14.27497556, 14.42971269, 14.57725893,
14.60788676, 14.63185637, 14.69843861, 14.72373986, 14.73439302,
14.73439302, 14.73439302, 14.73683436, 14.674469, 14.60744288
)), row.names = c(NA, -107L), class = "data.frame")
# smooth lines
p1 <-
mmr %>%
ggplot(.) +
geom_smooth(aes(x=year, y=mmrL)) +
geom_smooth(aes(x=year, y=mmr)) +
geom_smooth(aes(x=year, y=mmrU)) +
ylim(0, 25) +
scale_x_date(date_breaks="2 years", date_labels = "%Y")
# build plot object for rendering
pp1 <- ggplot_build(p1)
# extract data from the upper and lower lines
df2 <- data.frame(x = pp1$data[[1]]$x,
ymin = pp1$data[[2]]$y,
ymax = pp1$data[[3]]$y)
# use the lm data to add the ribbon to the plot
p1 + geom_ribbon(data = df2, aes(x = x, ymin = ymin, ymax = ymax), fill = "grey", alpha = 0.4)
The issue is that pp1$data[[1]]$x is no longer of date format. We may instead use
df2 <- data.frame(x = as.Date(pp1$data[[1]]$x, origin = "1970-01-01"),
ymin = pp1$data[[1]]$y,
ymax = pp1$data[[3]]$y)
where I've also adjusted ymin. This gives

Extract dataframe name in interactive R shiny app

I have a dataframe df that I use to interactively plot in an R shiny app. To define the axis xlim and ylim ranges of the plot I use an observeEvent function to define the xlim based on a dateRange field.
observeEvent(input$Number,{
ranges1$x <- input$dateRange_df
ranges1$y <- NULL
ranges2$x <- input$dateRange_df
ranges2$y <- NULL
})
How can I set the ylim (ranges1$y which is currently set to NULL in the example) to range e.g. over the minimum value of df -0.5 and maximum value of df + 0.5? Normally in a plot I would define the ylim like this:
ylim <- c(min(df$'0'-0.5), max(df$'0'+0.5))
but in order to make it interactive I have to make use of the placeholder input$Number
ranges1$y <- c(min(df$input$Number)-0.5, max(df$input$Number)+0.5)
does not work. How can I access the dataframe with this placeholder?
EDIT
Based on Stéphane Laurent's comment I changed my code to the following:
ranges1$y <- c(min(df[[input$Number]]$Y)-0.5, max(df[[input$Number]]$Y)+0.5)
df <- list(`0` = structure(data.frame(Date = structure(c(17737, 17738, 17739, 17740, 17741, 17742, 17743, 17744, 17745, 17746, 17747, 17748, 17749, 17750, 17751, 17752, 17753, 17754, 17755, 17757, 17758, 17759, 17760, 17761, 17762, 17763, 17764, 17766, 17767, 17768, 17769, 17770, 17772, 17773, 17774, 17775, 17776, 17777, 17778, 17779, 17780, 17781, 17782, 17783, 17784, 17785, 17786, 17787, 17789, 17790, 17791, 17792, 17793, 17794, 17795, 17796, 17797, 17798, 17799, 17800, 17801, 17802, 17803, 17804, 17805, 17806, 17807, 17808, 17809, 17810, 17811, 17812, 17813, 17814, 17815, 17816, 17817, 17818, 17819, 17820, 17821, 17822, 17823, 17824, 17825, 17826, 17827, 17828, 17829, 17830, 17831, 17833, 17834, 17835, 17836, 17837, 17838, 17839, 17840, 17841, 17843, 17844, 17846, 17847, 17848, 17849, 17850, 17851, 17852, 17853, 17854, 17856, 17858, 17860, 17862, 17864, 17866, 17868, 17870, 17872, 17874, 17876, 17878, 17880, 17882, 17884, 17888, 17922, 17924), class = "Date"),
X = c(2791162.3550637, 2791162.35592537, 2791162.34294051, 2791162.34864573, 2791162.34433394, 2791162.34740308, 2791162.34313007, 2791162.34487983, 2791162.34646726, 2791162.34200201, 2791162.34749474, 2791162.34255676, 2791162.34304754, 2791162.3425003, 2791162.34330067, 2791162.34278867, 2791162.34246682, 2791162.3438409, 2791162.34466809, 2791162.34875856, 2791162.34477708, 2791162.34662692, 2791162.34633941, 2791162.34701495, 2791162.3437676, 2791162.34370942, 2791162.34553501, 2791162.34325701, 2791162.34759088, 2791162.34038494, 2791162.33862812, 2791162.33991438, 2791162.34720632, 2791162.34631027, 2791164.24372202, 2791162.34461334, 2791162.34142775, 2791162.33764027, 2791162.33759566, 2791162.34250817, 2791162.33088489, 2791162.33793524, 2791162.3389292, 2791162.33744799, 2791162.33913297, 2791162.34362382, 2791162.33397091, 2791162.33889572, 2791162.3420609, 2791162.34256534, 2791162.34295388, 2791162.34360599, 2791162.33906351, 2791162.3410493, 2791162.34179139, 2791162.33807938, 2791162.34030308, 2791162.33740247, 2791162.33686627, 2791162.33465613, 2791162.33364715, 2791162.33327762, 2791162.33233307, 2791162.33092723, 2791162.32979484, 2791162.33032923, 2791162.32840836, 2791160.1523217, 2791162.33247584, 2791162.3277548, 2791162.32251429, 2791162.32674239, 2791162.33025114, 2791162.32962545, 2791162.32878667, 2791162.32817898, 2791162.32769788, 2791162.32821961, 2791162.32480343, 2791162.31161777, 2791162.32399605, 2791162.32336013, 2791162.32222764, 2791162.32393696, 2791162.3249891, 2791162.32406214, 2791162.32114112, 2791162.32277927, 2791162.32680753, 2791162.32359466, 2791162.31715986, 2791162.43273995, 2791162.43609669, 2791162.43228038, 2791162.43026514, 2791162.43019254, 2791162.42065433, 2791165.78288008, 2791162.42082946, 2791162.42138195, 2791162.41389611, 2791162.41563357, 2791162.41845497, 2791162.41773703, 2791162.41787637, 2791162.4187504, 2791162.41651641, 2791162.4153813, 2791162.41653668, 2791162.41481415, 2791162.41964469, 2791162.41477361, 2791162.41328494, 2791162.41301816, 2791162.41301417, 2791162.4045612, 2791162.40596568, 2791162.38755189, 2791162.39311443, 2791162.40093757, 2791162.38179792, 2791162.38089839, 2791162.38206784, 2791162.38178915, 2791162.3808269, 2791162.38363636, 2791162.38168147, 2791162.38247946, 2791162.38038856),
Y = c(1152645.1057075, 1152645.10716654, 1152645.0986932, 1152645.10973225, 1152645.10268387, 1152645.10860166, 1152645.10285369, 1152645.10415144, 1152645.10949053, 1152645.10592819, 1152645.11141594, 1152645.10374256, 1152645.10746764, 1152645.1063395, 1152645.10800735, 1152645.10815158, 1152645.10766793, 1152645.10480103, 1152645.11599135, 1152645.11151243, 1152645.10463071, 1152645.10960652, 1152645.10975319, 1152645.11164281, 1152645.11013769, 1152645.11160747, 1152645.11252802, 1152645.10673275, 1152645.11099244, 1152645.10262804, 1152645.10838721, 1152645.11149681, 1152645.11408256,1152645.11280042, 1152644.37773542, 1152645.11690802, 1152645.11770136, 1152645.1125467, 1152645.11574864, 1152645.11438099, 1152645.12331471, 1152645.1152969, 1152645.11450451, 1152645.11354352, 1152645.11153381, 1152645.11427941, 1152645.11192852, 1152645.11176822, 1152645.12555403, 1152645.12672117, 1152645.12572363, 1152645.12744387, 1152645.12433327, 1152645.12610127, 1152645.12706131, 1152645.12156846, 1152645.12355019, 1152645.12250923, 1152645.11961065, 1152645.11727393, 1152645.12012378, 1152645.1200921, 1152645.11721301, 1152645.11850732, 1152645.11513263, 1152645.12240832, 1152645.12009354, 1152645.187559, 1152645.12752105, 1152645.12267048, 1152645.11869222, 1152645.12166756, 1152645.12614825, 1152645.12081258, 1152645.12522161, 1152645.12731805, 1152645.1251948, 1152645.1250067, 1152645.11414578, 1152645.12608457, 1152645.11437481, 1152645.11610481, 1152645.119979, 1152645.11283147, 1152645.1149272, 1152645.11784599, 1152645.11696344, 1152645.1241658, 1152645.12738042, 1152645.12265048, 1152645.13113493, 1152644.90542479, 1152644.90531955, 1152644.9137409, 1152644.91079601, 1152644.91118295, 1152644.93155628, 1152644.85931671, 1152644.92505568, 1152644.92365586, 1152644.9062049, 1152644.9072195, 1152644.91269895, 1152644.91120019, 1152644.91528026, 1152644.91661213, 1152644.91545375, 1152644.91216822, 1152644.91436215, 1152644.91334168, 1152644.9132155, 1152644.91259903, 1152644.91009809, 1152644.90909567, 1152644.91015364, 1152644.90557376, 1152644.90806689, 1152644.94956111, 1152644.94274402, 1152644.91456334, 1152644.96352573, 1152644.95826583, 1152644.95863937, 1152644.95850146, 1152644.96287929, 1152644.9612743, 1152644.96621915, 1152644.94007565, 1152644.96446478))),
`4` = structure(data.frame(Date = structure(c(17739, 17740, 17741,17742, 17743, 17744, 17745, 17746, 17747, 17748, 17749, 17750, 17751, 17752, 17753, 17754, 17755, 17757, 17758, 17759, 17760, 17761, 17762, 17763, 17764, 17766, 17767, 17768, 17769, 17770, 17772, 17773, 17774, 17775, 17776, 17777, 17778, 17779, 17780, 17781, 17782, 17783, 17784, 17785, 17786, 17787, 17789, 17790, 17791, 17792, 17793, 17794, 17795, 17796, 17797, 17798, 17799, 17800, 17801, 17802, 17803, 17804, 17805, 17806, 17807, 17808, 17809, 17810, 17811, 17812, 17813, 17814, 17815, 17816, 17817, 17818, 17819, 17820, 17821, 17822, 17823, 17824, 17825, 17826, 17827, 17828, 17829, 17830, 17831, 17832, 17833, 17834, 17835, 17836, 17837, 17838, 17839, 17840, 17841, 17843, 17844, 17846, 17847, 17848, 17849, 17850, 17851, 17852, 17853, 17854, 17856, 17858, 17860, 17862, 17864, 17866, 17868, 17870, 17872, 17874, 17876, 17878, 17880, 17882, 17884, 17888, 17890, 17892, 17894, 17896, 17898, 17900, 17904, 17910, 17912, 17914, 17916, 17918, 17920, 17922, 17924), class = "Date"),
X = c(2790824.18374809, 2790824.18858669, 2790824.18446804, 2790824.1888521, 2790824.18384519, 2790824.18617796, 2790824.18771725, 2790824.18443985, 2790824.18748079, 2790824.18308143, 2790824.18520752, 2790824.18257565, 2790824.18457724, 2790824.18600788, 2790824.18375888, 2790824.1847667, 2790824.18760882, 2790824.19052547, 2790824.18875072, 2790824.19017255, 2790824.19245601, 2790824.19081026, 2790824.19017255, 2790824.18495934, 2790824.18864228, 2790824.18773663, 2790824.18766243, 2790824.18412512, 2790824.18165805, 2790824.18411731, 2790824.1886425, 2790824.18711752, 2790825.7650905, 2790824.1887942, 2790824.18584957, 2790824.18555477, 2790824.18344282, 2790824.18663601, 2790824.18287909, 2790824.18046056, 2790824.18249738, 2790824.18103211, 2790824.18272872, 2790824.18214583, 2790824.18129456, 2790824.18119768, 2790824.19036244, 2790824.19282126, 2790824.19331333, 2790824.19533039, 2790824.18985039, 2790824.19113479, 2790824.1932507, 2790824.18973607, 2790824.18726316, 2790824.18820932, 2790824.18617466, 2790824.18598988, 2790824.18462304, 2790824.18537468, 2790824.18511984, 2790824.1834283, 2790824.1861561, 2790824.18377319, 2790824.18455121, 2790823.77616338, 2790824.18794645, 2790824.18470526, 2790824.18367412, 2790824.18490804, 2790824.18672408, 2790824.18804839, 2790824.18506072, 2790824.18476163, 2790824.18430831, 2790824.18437524, 2790824.18377374, 2790824.17576408, 2790824.18188471, 2790824.17497523, 2790824.17361097, 2790824.17991796, 2790824.18053179, 2790824.18515782, 2790824.18380764, 2790824.18674445, 2790824.18748905, 2790824.18360762, 2790824.17562747, 2790824.17512053, 2790824.29807747, 2790824.29977056, 2790824.29613796, 2790824.29415652, 2790824.28809071, 2790824.28362793, 2790824.28864387, 2790824.28424219, 2790824.27961904, 2790824.28421589, 2790824.28469113, 2790824.28450756, 2790824.28310669, 2790824.28250442, 2790824.28508087, 2790824.28348406, 2790824.28309668, 2790824.28288763, 2790824.28368468, 2790824.28541184, 2790824.2850932, 2790824.28369521, 2790824.27851666, 2790824.28289038, 2790824.28139771, 2790824.27608701, 2790824.25548963, 2790824.2638138, 2790824.27164658, 2790824.25281592, 2790824.25130503, 2790824.25128763, 2790824.25101947, 2790824.2521007, 2790824.25603123, 2790824.25821979, 2790824.25660884, 2790824.27185727, 2790824.26655444, 2790824.25487382, 2790824.24117284, 2790824.24537759, 2790824.23911296, 2790824.23060396, 2790824.25309687, 2790824.26019989, 2790824.26512535, 2790824.2679404, 2790824.26690672, 2790824.25377196, 2790824.26113459),
Y = c(1152771.25205373, 1152771.26044974, 1152771.25753943, 1152771.25978308, 1152771.2558363, 1152771.25737316, 1152771.26221167, 1152771.26008521, 1152771.26175994, 1152771.25406342, 1152771.25888205, 1152771.25536511, 1152771.25642348, 1152771.25771622, 1152771.25637725, 1152771.25125447, 1152771.26784701, 1152771.26538719, 1152771.25912007, 1152771.26242374, 1152771.2644979, 1152771.26553601, 1152771.26377529, 1152771.26209083, 1152771.26141445, 1152771.26246472, 1152771.2604169, 1152771.26242945, 1152771.25709542, 1152771.26461715, 1152771.26770187, 1152771.26539248, 1152770.44825641, 1152771.26541274, 1152771.26600154, 1152771.26392009, 1152771.26404865, 1152771.26264474, 1152771.26493265, 1152771.25979324, 1152771.26063346, 1152771.26042017, 1152771.25733911, 1152771.25714285, 1152771.25710758, 1152771.25825841, 1152771.26857731, 1152771.26889403, 1152771.26763328, 1152771.27012269, 1152771.26569212, 1152771.26643379, 1152771.26831453, 1152771.26453117, 1152771.2659026, 1152771.26211273, 1152771.26160017, 1152771.25949032, 1152771.25648998, 1152771.25802036, 1152771.2564697, 1152771.25572519, 1152771.26994309, 1152771.25856009, 1152771.25577385, 1152771.21190108, 1152771.26022874, 1152771.25793114, 1152771.25677626, 1152771.2583099, 1152771.26039744, 1152771.25265629, 1152771.25645023, 1152771.25709743, 1152771.25675924, 1152771.25475927, 1152771.25495515, 1152771.25653707, 1152771.25558733, 1152771.24623797, 1152771.24770836, 1152771.2549661, 1152771.25507885, 1152771.25924985, 1152771.2628605, 1152771.26486285, 1152771.26706399, 1152771.26320436, 1152771.25670899, 1152771.25969191, 1152771.04282095, 1152771.0422443, 1152771.04935005, 1152771.0453424, 1152771.03681296, 1152771.04392157, 1152771.04540486, 1152771.04963515, 1152771.02783388, 1152771.03612569, 1152771.03824284, 1152771.04113289, 1152771.04169045, 1152771.04252987, 1152771.04051328, 1152771.04079713, 1152771.03747317, 1152771.03739855, 1152771.03942934, 1152771.03740992, 1152771.0364367, 1152771.03294287, 1152771.03230096, 1152771.03823554, 1152771.03634142, 1152771.03405967, 1152771.07550426, 1152771.06546547, 1152771.03697854, 1152771.08281639, 1152771.07903828, 1152771.08392586, 1152771.0829198, 1152771.08307592, 1152771.0774657, 1152771.09114143, 1152771.07792026, 1152771.05372315, 1152771.0585986, 1152771.07327235, 1152771.08565988, 1152771.08055774, 1152771.09170355, 1152771.09050087, 1152771.05223544, 1152771.05222645, 1152771.05033638, 1152771.04887938, 1152771.04816407, 1152771.05337247, 1152771.06691153))))
fmt_decimals <- function(decimals=0){
function(x) format(x,nsmall = decimals,scientific = FALSE)
}
ui <- fluidPage(
titlePanel("TestApp", windowTitle = "Test Shiny App"),
tabsetPanel(tabPanel("Tab1",
wellPanel(fluidRow(column(selectInput("Number", "Datset number:", names(df)), width = 1),
column(dateRangeInput('dateRange_df',
label = 'Select the time interval for plots',
start = "2018-07-20",
separator = " - ", format = "dd/mm/yy",
startview = 'year' , weekstart = 1), width = 4, offset = 3))),
fluidRow(column(plotOutput("plot1", height = 500, dblclick = "plot1_dblclick", brush = brushOpts(id = "plot1_brush", resetOnNew = TRUE)), width = 6),
column(plotOutput("plot2", height = 500, dblclick = "plot2_dblclick", brush = brushOpts(id = "plot2_brush", resetOnNew = TRUE)), width = 6)))))
server <- function(input,output, session){
observeEvent(input$dateRange_df, {
ranges1$x <- input$dateRange_df
ranges2$x <- input$dateRange_df
})
observeEvent(input$Number,{
ranges1$x <- input$dateRange_df
ranges1$y <- c(min(df[[input$Number]]$Y)-0.5, max(df[[input$Number]])+0.5)
ranges2$x <- input$dateRange_df
ranges2$y <- c(min(df[[input$Number]]$Y)-0.5, max(df[[input$Number]])+0.5)
})
# Plot1
ranges1 <- reactiveValues(x = NULL, y = NULL)
output$plot1 <- renderPlot({
if (!is.null(ranges1$x)) {
ranges1$x <- as.Date(ranges1$x, origin = "1970-01-01")
}
if(input$Number == "0"){
col = rgb(165,0,38, max = 255)
}
else if(input$Number == "4"){
col = rgb(215,48,39, max = 255)
}
labels_dec1 <- fmt_decimals(2)
p <- ggplot(df[[input$Number]], aes(x = Date, y = X)) +
geom_point(colour = col) + coord_cartesian(xlim = ranges1$x, ylim = ranges1$y, expand =FALSE)
p <- p + labs(x = "", y = "X [m]", title = paste("Plot1 - Number ", input$Number))
p <- p + theme(panel.background = element_rect(fill = "grey99"))
p <- p + scale_y_continuous(labels = labels_dec1)
p + ggtitle(paste("Plot1 - Dataset number", input$Number))
})
observeEvent(input$plot1_dblclick, {
brush <- input$plot1_brush
if (!is.null(brush)) {
ranges1$x <- c(brush$xmin, brush$xmax)
ranges1$y <- c(brush$ymin, brush$ymax)
} else {
ranges1$x <- input$dateRange_df
ranges1$y <- NULL
}
})
# Plot2
ranges2 <- reactiveValues(x = NULL, y = NULL)
output$plot2 <- renderPlot({
if (!is.null(ranges2$x)) {
ranges2$x <- as.Date(ranges2$x, origin = "1970-01-01")
}
if(input$Number == "0"){
col = rgb(165,0,38, max = 255)
}
else if(input$Number == "4"){
col = rgb(215,48,39, max = 255)
}
labels_dec2 <- fmt_decimals(2)
p <- ggplot(df[[input$Number]], aes(x = Date, y = Y)) +
geom_point(colour = col) + coord_cartesian(xlim = ranges2$x, ylim = ranges2$y, expand =FALSE)
p <- p + labs(x = "", y = "Y [m]", title = paste("Plot 2 - Number ", input$Number))
p <- p + theme(panel.background = element_rect(fill = "grey99"))
p <- p + scale_y_continuous(labels = labels_dec2)
p + ggtitle(paste("Plot2 - Dataset number ", input$Number))
})
observeEvent(input$plot2_dblclick, {
brush <- input$plot2_brush
if (!is.null(brush)) {
ranges2$x <- c(brush$xmin, brush$xmax)
ranges2$y <- c(brush$ymin, brush$ymax)
} else {
ranges2$x <- input$dateRange_df
ranges2$y <- NULL
}
})
output$brush_info1 <- renderPrint({
brushedPoints(df[[input$Number]], input$plot1_brush)
})
}
shinyApp(ui, server)

Control the number of decimals in renderPrint panel in R Shiny app

In my shiny app in the server.R part I am using a renderPrint statement for displaying brushedPoints that can be selected by the user on a plot.
output$brush_info1 <- renderPrint({
brushedPoints(df[[input$Number]], input$plot1_brush)
})
However this panel does not display decimals for columns X and Y.
I tried to wrap df[[input$Number]] in as.character but this failed.
output$brush_info1 <- renderPrint({
brushedPoints(c(df[[input$Number]]$Date, as.character(df[[input$Number]]$X), as.character(df[[input$Number]]$Y)) , input$plot1_brush)
})
How could I display the columns X and Y with 2 decimals?
My app code can be found below:
df <- list(`0` = structure(data.frame(Date = structure(c(17737, 17738, 17739, 17740, 17741, 17742, 17743, 17744, 17745, 17746, 17747, 17748, 17749, 17750, 17751, 17752, 17753, 17754, 17755, 17757, 17758, 17759, 17760, 17761, 17762, 17763, 17764, 17766, 17767, 17768, 17769, 17770, 17772, 17773, 17774, 17775, 17776, 17777, 17778, 17779, 17780, 17781, 17782, 17783, 17784, 17785, 17786, 17787, 17789, 17790, 17791, 17792, 17793, 17794, 17795, 17796, 17797, 17798, 17799, 17800, 17801, 17802, 17803, 17804, 17805, 17806, 17807, 17808, 17809, 17810, 17811, 17812, 17813, 17814, 17815, 17816, 17817, 17818, 17819, 17820, 17821, 17822, 17823, 17824, 17825, 17826, 17827, 17828, 17829, 17830, 17831, 17833, 17834, 17835, 17836, 17837, 17838, 17839, 17840, 17841, 17843, 17844, 17846, 17847, 17848, 17849, 17850, 17851, 17852, 17853, 17854, 17856, 17858, 17860, 17862, 17864, 17866, 17868, 17870, 17872, 17874, 17876, 17878, 17880, 17882, 17884, 17888, 17922, 17924), class = "Date"),
X = c(2791162.3550637, 2791162.35592537, 2791162.34294051, 2791162.34864573, 2791162.34433394, 2791162.34740308, 2791162.34313007, 2791162.34487983, 2791162.34646726, 2791162.34200201, 2791162.34749474, 2791162.34255676, 2791162.34304754, 2791162.3425003, 2791162.34330067, 2791162.34278867, 2791162.34246682, 2791162.3438409, 2791162.34466809, 2791162.34875856, 2791162.34477708, 2791162.34662692, 2791162.34633941, 2791162.34701495, 2791162.3437676, 2791162.34370942, 2791162.34553501, 2791162.34325701, 2791162.34759088, 2791162.34038494, 2791162.33862812, 2791162.33991438, 2791162.34720632, 2791162.34631027, 2791164.24372202, 2791162.34461334, 2791162.34142775, 2791162.33764027, 2791162.33759566, 2791162.34250817, 2791162.33088489, 2791162.33793524, 2791162.3389292, 2791162.33744799, 2791162.33913297, 2791162.34362382, 2791162.33397091, 2791162.33889572, 2791162.3420609, 2791162.34256534, 2791162.34295388, 2791162.34360599, 2791162.33906351, 2791162.3410493, 2791162.34179139, 2791162.33807938, 2791162.34030308, 2791162.33740247, 2791162.33686627, 2791162.33465613, 2791162.33364715, 2791162.33327762, 2791162.33233307, 2791162.33092723, 2791162.32979484, 2791162.33032923, 2791162.32840836, 2791160.1523217, 2791162.33247584, 2791162.3277548, 2791162.32251429, 2791162.32674239, 2791162.33025114, 2791162.32962545, 2791162.32878667, 2791162.32817898, 2791162.32769788, 2791162.32821961, 2791162.32480343, 2791162.31161777, 2791162.32399605, 2791162.32336013, 2791162.32222764, 2791162.32393696, 2791162.3249891, 2791162.32406214, 2791162.32114112, 2791162.32277927, 2791162.32680753, 2791162.32359466, 2791162.31715986, 2791162.43273995, 2791162.43609669, 2791162.43228038, 2791162.43026514, 2791162.43019254, 2791162.42065433, 2791165.78288008, 2791162.42082946, 2791162.42138195, 2791162.41389611, 2791162.41563357, 2791162.41845497, 2791162.41773703, 2791162.41787637, 2791162.4187504, 2791162.41651641, 2791162.4153813, 2791162.41653668, 2791162.41481415, 2791162.41964469, 2791162.41477361, 2791162.41328494, 2791162.41301816, 2791162.41301417, 2791162.4045612, 2791162.40596568, 2791162.38755189, 2791162.39311443, 2791162.40093757, 2791162.38179792, 2791162.38089839, 2791162.38206784, 2791162.38178915, 2791162.3808269, 2791162.38363636, 2791162.38168147, 2791162.38247946, 2791162.38038856),
Y = c(1152645.1057075, 1152645.10716654, 1152645.0986932, 1152645.10973225, 1152645.10268387, 1152645.10860166, 1152645.10285369, 1152645.10415144, 1152645.10949053, 1152645.10592819, 1152645.11141594, 1152645.10374256, 1152645.10746764, 1152645.1063395, 1152645.10800735, 1152645.10815158, 1152645.10766793, 1152645.10480103, 1152645.11599135, 1152645.11151243, 1152645.10463071, 1152645.10960652, 1152645.10975319, 1152645.11164281, 1152645.11013769, 1152645.11160747, 1152645.11252802, 1152645.10673275, 1152645.11099244, 1152645.10262804, 1152645.10838721, 1152645.11149681, 1152645.11408256,1152645.11280042, 1152644.37773542, 1152645.11690802, 1152645.11770136, 1152645.1125467, 1152645.11574864, 1152645.11438099, 1152645.12331471, 1152645.1152969, 1152645.11450451, 1152645.11354352, 1152645.11153381, 1152645.11427941, 1152645.11192852, 1152645.11176822, 1152645.12555403, 1152645.12672117, 1152645.12572363, 1152645.12744387, 1152645.12433327, 1152645.12610127, 1152645.12706131, 1152645.12156846, 1152645.12355019, 1152645.12250923, 1152645.11961065, 1152645.11727393, 1152645.12012378, 1152645.1200921, 1152645.11721301, 1152645.11850732, 1152645.11513263, 1152645.12240832, 1152645.12009354, 1152645.187559, 1152645.12752105, 1152645.12267048, 1152645.11869222, 1152645.12166756, 1152645.12614825, 1152645.12081258, 1152645.12522161, 1152645.12731805, 1152645.1251948, 1152645.1250067, 1152645.11414578, 1152645.12608457, 1152645.11437481, 1152645.11610481, 1152645.119979, 1152645.11283147, 1152645.1149272, 1152645.11784599, 1152645.11696344, 1152645.1241658, 1152645.12738042, 1152645.12265048, 1152645.13113493, 1152644.90542479, 1152644.90531955, 1152644.9137409, 1152644.91079601, 1152644.91118295, 1152644.93155628, 1152644.85931671, 1152644.92505568, 1152644.92365586, 1152644.9062049, 1152644.9072195, 1152644.91269895, 1152644.91120019, 1152644.91528026, 1152644.91661213, 1152644.91545375, 1152644.91216822, 1152644.91436215, 1152644.91334168, 1152644.9132155, 1152644.91259903, 1152644.91009809, 1152644.90909567, 1152644.91015364, 1152644.90557376, 1152644.90806689, 1152644.94956111, 1152644.94274402, 1152644.91456334, 1152644.96352573, 1152644.95826583, 1152644.95863937, 1152644.95850146, 1152644.96287929, 1152644.9612743, 1152644.96621915, 1152644.94007565, 1152644.96446478))),
`4` = structure(data.frame(Date = structure(c(17739, 17740, 17741,17742, 17743, 17744, 17745, 17746, 17747, 17748, 17749, 17750, 17751, 17752, 17753, 17754, 17755, 17757, 17758, 17759, 17760, 17761, 17762, 17763, 17764, 17766, 17767, 17768, 17769, 17770, 17772, 17773, 17774, 17775, 17776, 17777, 17778, 17779, 17780, 17781, 17782, 17783, 17784, 17785, 17786, 17787, 17789, 17790, 17791, 17792, 17793, 17794, 17795, 17796, 17797, 17798, 17799, 17800, 17801, 17802, 17803, 17804, 17805, 17806, 17807, 17808, 17809, 17810, 17811, 17812, 17813, 17814, 17815, 17816, 17817, 17818, 17819, 17820, 17821, 17822, 17823, 17824, 17825, 17826, 17827, 17828, 17829, 17830, 17831, 17832, 17833, 17834, 17835, 17836, 17837, 17838, 17839, 17840, 17841, 17843, 17844, 17846, 17847, 17848, 17849, 17850, 17851, 17852, 17853, 17854, 17856, 17858, 17860, 17862, 17864, 17866, 17868, 17870, 17872, 17874, 17876, 17878, 17880, 17882, 17884, 17888, 17890, 17892, 17894, 17896, 17898, 17900, 17904, 17910, 17912, 17914, 17916, 17918, 17920, 17922, 17924), class = "Date"),
X = c(2790824.18374809, 2790824.18858669, 2790824.18446804, 2790824.1888521, 2790824.18384519, 2790824.18617796, 2790824.18771725, 2790824.18443985, 2790824.18748079, 2790824.18308143, 2790824.18520752, 2790824.18257565, 2790824.18457724, 2790824.18600788, 2790824.18375888, 2790824.1847667, 2790824.18760882, 2790824.19052547, 2790824.18875072, 2790824.19017255, 2790824.19245601, 2790824.19081026, 2790824.19017255, 2790824.18495934, 2790824.18864228, 2790824.18773663, 2790824.18766243, 2790824.18412512, 2790824.18165805, 2790824.18411731, 2790824.1886425, 2790824.18711752, 2790825.7650905, 2790824.1887942, 2790824.18584957, 2790824.18555477, 2790824.18344282, 2790824.18663601, 2790824.18287909, 2790824.18046056, 2790824.18249738, 2790824.18103211, 2790824.18272872, 2790824.18214583, 2790824.18129456, 2790824.18119768, 2790824.19036244, 2790824.19282126, 2790824.19331333, 2790824.19533039, 2790824.18985039, 2790824.19113479, 2790824.1932507, 2790824.18973607, 2790824.18726316, 2790824.18820932, 2790824.18617466, 2790824.18598988, 2790824.18462304, 2790824.18537468, 2790824.18511984, 2790824.1834283, 2790824.1861561, 2790824.18377319, 2790824.18455121, 2790823.77616338, 2790824.18794645, 2790824.18470526, 2790824.18367412, 2790824.18490804, 2790824.18672408, 2790824.18804839, 2790824.18506072, 2790824.18476163, 2790824.18430831, 2790824.18437524, 2790824.18377374, 2790824.17576408, 2790824.18188471, 2790824.17497523, 2790824.17361097, 2790824.17991796, 2790824.18053179, 2790824.18515782, 2790824.18380764, 2790824.18674445, 2790824.18748905, 2790824.18360762, 2790824.17562747, 2790824.17512053, 2790824.29807747, 2790824.29977056, 2790824.29613796, 2790824.29415652, 2790824.28809071, 2790824.28362793, 2790824.28864387, 2790824.28424219, 2790824.27961904, 2790824.28421589, 2790824.28469113, 2790824.28450756, 2790824.28310669, 2790824.28250442, 2790824.28508087, 2790824.28348406, 2790824.28309668, 2790824.28288763, 2790824.28368468, 2790824.28541184, 2790824.2850932, 2790824.28369521, 2790824.27851666, 2790824.28289038, 2790824.28139771, 2790824.27608701, 2790824.25548963, 2790824.2638138, 2790824.27164658, 2790824.25281592, 2790824.25130503, 2790824.25128763, 2790824.25101947, 2790824.2521007, 2790824.25603123, 2790824.25821979, 2790824.25660884, 2790824.27185727, 2790824.26655444, 2790824.25487382, 2790824.24117284, 2790824.24537759, 2790824.23911296, 2790824.23060396, 2790824.25309687, 2790824.26019989, 2790824.26512535, 2790824.2679404, 2790824.26690672, 2790824.25377196, 2790824.26113459),
Y = c(1152771.25205373, 1152771.26044974, 1152771.25753943, 1152771.25978308, 1152771.2558363, 1152771.25737316, 1152771.26221167, 1152771.26008521, 1152771.26175994, 1152771.25406342, 1152771.25888205, 1152771.25536511, 1152771.25642348, 1152771.25771622, 1152771.25637725, 1152771.25125447, 1152771.26784701, 1152771.26538719, 1152771.25912007, 1152771.26242374, 1152771.2644979, 1152771.26553601, 1152771.26377529, 1152771.26209083, 1152771.26141445, 1152771.26246472, 1152771.2604169, 1152771.26242945, 1152771.25709542, 1152771.26461715, 1152771.26770187, 1152771.26539248, 1152770.44825641, 1152771.26541274, 1152771.26600154, 1152771.26392009, 1152771.26404865, 1152771.26264474, 1152771.26493265, 1152771.25979324, 1152771.26063346, 1152771.26042017, 1152771.25733911, 1152771.25714285, 1152771.25710758, 1152771.25825841, 1152771.26857731, 1152771.26889403, 1152771.26763328, 1152771.27012269, 1152771.26569212, 1152771.26643379, 1152771.26831453, 1152771.26453117, 1152771.2659026, 1152771.26211273, 1152771.26160017, 1152771.25949032, 1152771.25648998, 1152771.25802036, 1152771.2564697, 1152771.25572519, 1152771.26994309, 1152771.25856009, 1152771.25577385, 1152771.21190108, 1152771.26022874, 1152771.25793114, 1152771.25677626, 1152771.2583099, 1152771.26039744, 1152771.25265629, 1152771.25645023, 1152771.25709743, 1152771.25675924, 1152771.25475927, 1152771.25495515, 1152771.25653707, 1152771.25558733, 1152771.24623797, 1152771.24770836, 1152771.2549661, 1152771.25507885, 1152771.25924985, 1152771.2628605, 1152771.26486285, 1152771.26706399, 1152771.26320436, 1152771.25670899, 1152771.25969191, 1152771.04282095, 1152771.0422443, 1152771.04935005, 1152771.0453424, 1152771.03681296, 1152771.04392157, 1152771.04540486, 1152771.04963515, 1152771.02783388, 1152771.03612569, 1152771.03824284, 1152771.04113289, 1152771.04169045, 1152771.04252987, 1152771.04051328, 1152771.04079713, 1152771.03747317, 1152771.03739855, 1152771.03942934, 1152771.03740992, 1152771.0364367, 1152771.03294287, 1152771.03230096, 1152771.03823554, 1152771.03634142, 1152771.03405967, 1152771.07550426, 1152771.06546547, 1152771.03697854, 1152771.08281639, 1152771.07903828, 1152771.08392586, 1152771.0829198, 1152771.08307592, 1152771.0774657, 1152771.09114143, 1152771.07792026, 1152771.05372315, 1152771.0585986, 1152771.07327235, 1152771.08565988, 1152771.08055774, 1152771.09170355, 1152771.09050087, 1152771.05223544, 1152771.05222645, 1152771.05033638, 1152771.04887938, 1152771.04816407, 1152771.05337247, 1152771.06691153))))
fmt_decimals <- function(decimals=0){
function(x) format(x,nsmall = decimals,scientific = FALSE)
}
ui <- fluidPage(
titlePanel("TestApp", windowTitle = "Test Shiny App"),
tabsetPanel(tabPanel("Tab1",
wellPanel(fluidRow(column(selectInput("Number", "Datset number:", names(df)), width = 1),
column(dateRangeInput('dateRange_df',
label = 'Select the time interval for plots',
start = "2018-07-20",
separator = " - ", format = "dd/mm/yy",
startview = 'year' , weekstart = 1), width = 4, offset = 3))),
fluidRow(column(plotOutput("plot1", height = 500, dblclick = "plot1_dblclick", brush = brushOpts(id = "plot1_brush", resetOnNew = TRUE)), width = 6),
column(plotOutput("plot2", height = 500, dblclick = "plot2_dblclick", brush = brushOpts(id = "plot2_brush", resetOnNew = TRUE)), width = 6)),
fluidRow(column(width = 6, h4("Selection 1"), verbatimTextOutput("brush_info1")),
column(width = 6, h4("Selection 2"), verbatimTextOutput("brush_info2")
)))))
server <- function(input,output, session){
observeEvent(input$dateRange_df, {
ranges1$x <- input$dateRange_df
ranges2$x <- input$dateRange_df
})
observeEvent(input$Number,{
ranges1$x <- input$dateRange_df
ranges1$y <- NULL
ranges2$x <- input$dateRange_df
ranges2$y <- NULL
})
# Plot1
ranges1 <- reactiveValues(x = NULL, y = NULL)
output$plot1 <- renderPlot({
if (!is.null(ranges1$x)) {
ranges1$x <- as.Date(ranges1$x, origin = "1970-01-01")
}
if(input$Number == "0"){
col = rgb(165,0,38, max = 255)
}
else if(input$Number == "4"){
col = rgb(215,48,39, max = 255)
}
labels_dec1 <- fmt_decimals(2)
p <- ggplot(df[[input$Number]], aes(x = Date, y = X)) +
geom_point(colour = col) + coord_cartesian(xlim = ranges1$x, ylim = ranges1$y, expand =FALSE)
p <- p + labs(x = "", y = "X [m]", title = paste("Plot1 - Number ", input$Number))
p <- p + theme(panel.background = element_rect(fill = "grey99"))
p <- p + scale_y_continuous(labels = labels_dec1)
p + ggtitle(paste("Plot1 - Dataset number", input$Number))
})
observeEvent(input$plot1_dblclick, {
brush <- input$plot1_brush
if (!is.null(brush)) {
ranges1$x <- c(brush$xmin, brush$xmax)
ranges1$y <- c(brush$ymin, brush$ymax)
} else {
ranges1$x <- input$dateRange_df
ranges1$y <- NULL
}
})
output$brush_info1 <- renderPrint({
brushedPoints(df[[input$Number]], input$plot1_brush)
})
# Plot2
ranges2 <- reactiveValues(x = NULL, y = NULL)
output$plot2 <- renderPlot({
if (!is.null(ranges2$x)) {
ranges2$x <- as.Date(ranges2$x, origin = "1970-01-01")
}
if(input$Number == "0"){
col = rgb(165,0,38, max = 255)
}
else if(input$Number == "4"){
col = rgb(215,48,39, max = 255)
}
labels_dec2 <- fmt_decimals(2)
p <- ggplot(df[[input$Number]], aes(x = Date, y = Y)) +
geom_point(colour = col) + coord_cartesian(xlim = ranges2$x, ylim = ranges2$y, expand =FALSE)
p <- p + labs(x = "", y = "Y [m]", title = paste("Plot 2 - Number ", input$Number))
p <- p + theme(panel.background = element_rect(fill = "grey99"))
p <- p + scale_y_continuous(labels = labels_dec2)
p + ggtitle(paste("Plot2 - Dataset number ", input$Number))
})
observeEvent(input$plot2_dblclick, {
brush <- input$plot2_brush
if (!is.null(brush)) {
ranges2$x <- c(brush$xmin, brush$xmax)
ranges2$y <- c(brush$ymin, brush$ymax)
} else {
ranges2$x <- input$dateRange_df
ranges2$y <- NULL
}
})
output$brush_info2 <- renderPrint({
brushedPoints(df[[input$Number]], input$plot2_brush)
})
}
shinyApp(ui, server)
You just need to format the output the way you want. Here I rounded it and then only preserved 2 digits after the decimal point.
output$brush_info2 <- renderPrint({
result <- brushedPoints(df[[input$Number]], input$plot2_brush)
result$X <- format(round(result$X, 2), nsmall = 2)
result$Y <- format(round(result$Y, 2), nsmall = 2)
result
})

Set default value of Reactive Values in an R Shiny app

I have built a shiny app that displays time series data in a series of plots (simplified version of the app can be found below). I made the plots zoomable using this template: https://gallery.shinyapps.io/105-plot-interaction-zoom/
This works all fine, however I was wondering if it is possible to set default values for reactiveValues (e.g. for ranges1 and ranges2 examples below)?
In other words: I would like to set the initial default values for the x axis to the values specified in the dateRangeInput field. When the user interacts with the plot (e.g. zooms in or out) the axis values should then be changed according to the reactive value of ranges below enabling the zoom interaction.
Is this possible to achieve? I attempted to create a simplified, reproducible version of my app. The code can be found below:
df <- list(`0` = structure(data.frame(Date = structure(c(17737, 17738, 17739, 17740, 17741, 17742, 17743, 17744, 17745, 17746, 17747, 17748, 17749, 17750, 17751, 17752, 17753, 17754, 17755, 17757, 17758, 17759, 17760, 17761, 17762, 17763, 17764, 17766, 17767, 17768, 17769, 17770, 17772, 17773, 17774, 17775, 17776, 17777, 17778, 17779, 17780, 17781, 17782, 17783, 17784, 17785, 17786, 17787, 17789, 17790, 17791, 17792, 17793, 17794, 17795, 17796, 17797, 17798, 17799, 17800, 17801, 17802, 17803, 17804, 17805, 17806, 17807, 17808, 17809, 17810, 17811, 17812, 17813, 17814, 17815, 17816, 17817, 17818, 17819, 17820, 17821, 17822, 17823, 17824, 17825, 17826, 17827, 17828, 17829, 17830, 17831, 17833, 17834, 17835, 17836, 17837, 17838, 17839, 17840, 17841, 17843, 17844, 17846, 17847, 17848, 17849, 17850, 17851, 17852, 17853, 17854, 17856, 17858, 17860, 17862, 17864, 17866, 17868, 17870, 17872, 17874, 17876, 17878, 17880, 17882, 17884, 17888, 17922, 17924), class = "Date"),
X = c(2791162.3550637, 2791162.35592537, 2791162.34294051, 2791162.34864573, 2791162.34433394, 2791162.34740308, 2791162.34313007, 2791162.34487983, 2791162.34646726, 2791162.34200201, 2791162.34749474, 2791162.34255676, 2791162.34304754, 2791162.3425003, 2791162.34330067, 2791162.34278867, 2791162.34246682, 2791162.3438409, 2791162.34466809, 2791162.34875856, 2791162.34477708, 2791162.34662692, 2791162.34633941, 2791162.34701495, 2791162.3437676, 2791162.34370942, 2791162.34553501, 2791162.34325701, 2791162.34759088, 2791162.34038494, 2791162.33862812, 2791162.33991438, 2791162.34720632, 2791162.34631027, 2791164.24372202, 2791162.34461334, 2791162.34142775, 2791162.33764027, 2791162.33759566, 2791162.34250817, 2791162.33088489, 2791162.33793524, 2791162.3389292, 2791162.33744799, 2791162.33913297, 2791162.34362382, 2791162.33397091, 2791162.33889572, 2791162.3420609, 2791162.34256534, 2791162.34295388, 2791162.34360599, 2791162.33906351, 2791162.3410493, 2791162.34179139, 2791162.33807938, 2791162.34030308, 2791162.33740247, 2791162.33686627, 2791162.33465613, 2791162.33364715, 2791162.33327762, 2791162.33233307, 2791162.33092723, 2791162.32979484, 2791162.33032923, 2791162.32840836, 2791160.1523217, 2791162.33247584, 2791162.3277548, 2791162.32251429, 2791162.32674239, 2791162.33025114, 2791162.32962545, 2791162.32878667, 2791162.32817898, 2791162.32769788, 2791162.32821961, 2791162.32480343, 2791162.31161777, 2791162.32399605, 2791162.32336013, 2791162.32222764, 2791162.32393696, 2791162.3249891, 2791162.32406214, 2791162.32114112, 2791162.32277927, 2791162.32680753, 2791162.32359466, 2791162.31715986, 2791162.43273995, 2791162.43609669, 2791162.43228038, 2791162.43026514, 2791162.43019254, 2791162.42065433, 2791165.78288008, 2791162.42082946, 2791162.42138195, 2791162.41389611, 2791162.41563357, 2791162.41845497, 2791162.41773703, 2791162.41787637, 2791162.4187504, 2791162.41651641, 2791162.4153813, 2791162.41653668, 2791162.41481415, 2791162.41964469, 2791162.41477361, 2791162.41328494, 2791162.41301816, 2791162.41301417, 2791162.4045612, 2791162.40596568, 2791162.38755189, 2791162.39311443, 2791162.40093757, 2791162.38179792, 2791162.38089839, 2791162.38206784, 2791162.38178915, 2791162.3808269, 2791162.38363636, 2791162.38168147, 2791162.38247946, 2791162.38038856),
Y = c(1152645.1057075, 1152645.10716654, 1152645.0986932, 1152645.10973225, 1152645.10268387, 1152645.10860166, 1152645.10285369, 1152645.10415144, 1152645.10949053, 1152645.10592819, 1152645.11141594, 1152645.10374256, 1152645.10746764, 1152645.1063395, 1152645.10800735, 1152645.10815158, 1152645.10766793, 1152645.10480103, 1152645.11599135, 1152645.11151243, 1152645.10463071, 1152645.10960652, 1152645.10975319, 1152645.11164281, 1152645.11013769, 1152645.11160747, 1152645.11252802, 1152645.10673275, 1152645.11099244, 1152645.10262804, 1152645.10838721, 1152645.11149681, 1152645.11408256,1152645.11280042, 1152644.37773542, 1152645.11690802, 1152645.11770136, 1152645.1125467, 1152645.11574864, 1152645.11438099, 1152645.12331471, 1152645.1152969, 1152645.11450451, 1152645.11354352, 1152645.11153381, 1152645.11427941, 1152645.11192852, 1152645.11176822, 1152645.12555403, 1152645.12672117, 1152645.12572363, 1152645.12744387, 1152645.12433327, 1152645.12610127, 1152645.12706131, 1152645.12156846, 1152645.12355019, 1152645.12250923, 1152645.11961065, 1152645.11727393, 1152645.12012378, 1152645.1200921, 1152645.11721301, 1152645.11850732, 1152645.11513263, 1152645.12240832, 1152645.12009354, 1152645.187559, 1152645.12752105, 1152645.12267048, 1152645.11869222, 1152645.12166756, 1152645.12614825, 1152645.12081258, 1152645.12522161, 1152645.12731805, 1152645.1251948, 1152645.1250067, 1152645.11414578, 1152645.12608457, 1152645.11437481, 1152645.11610481, 1152645.119979, 1152645.11283147, 1152645.1149272, 1152645.11784599, 1152645.11696344, 1152645.1241658, 1152645.12738042, 1152645.12265048, 1152645.13113493, 1152644.90542479, 1152644.90531955, 1152644.9137409, 1152644.91079601, 1152644.91118295, 1152644.93155628, 1152644.85931671, 1152644.92505568, 1152644.92365586, 1152644.9062049, 1152644.9072195, 1152644.91269895, 1152644.91120019, 1152644.91528026, 1152644.91661213, 1152644.91545375, 1152644.91216822, 1152644.91436215, 1152644.91334168, 1152644.9132155, 1152644.91259903, 1152644.91009809, 1152644.90909567, 1152644.91015364, 1152644.90557376, 1152644.90806689, 1152644.94956111, 1152644.94274402, 1152644.91456334, 1152644.96352573, 1152644.95826583, 1152644.95863937, 1152644.95850146, 1152644.96287929, 1152644.9612743, 1152644.96621915, 1152644.94007565, 1152644.96446478))),
`4` = structure(data.frame(Date = structure(c(17739, 17740, 17741,17742, 17743, 17744, 17745, 17746, 17747, 17748, 17749, 17750, 17751, 17752, 17753, 17754, 17755, 17757, 17758, 17759, 17760, 17761, 17762, 17763, 17764, 17766, 17767, 17768, 17769, 17770, 17772, 17773, 17774, 17775, 17776, 17777, 17778, 17779, 17780, 17781, 17782, 17783, 17784, 17785, 17786, 17787, 17789, 17790, 17791, 17792, 17793, 17794, 17795, 17796, 17797, 17798, 17799, 17800, 17801, 17802, 17803, 17804, 17805, 17806, 17807, 17808, 17809, 17810, 17811, 17812, 17813, 17814, 17815, 17816, 17817, 17818, 17819, 17820, 17821, 17822, 17823, 17824, 17825, 17826, 17827, 17828, 17829, 17830, 17831, 17832, 17833, 17834, 17835, 17836, 17837, 17838, 17839, 17840, 17841, 17843, 17844, 17846, 17847, 17848, 17849, 17850, 17851, 17852, 17853, 17854, 17856, 17858, 17860, 17862, 17864, 17866, 17868, 17870, 17872, 17874, 17876, 17878, 17880, 17882, 17884, 17888, 17890, 17892, 17894, 17896, 17898, 17900, 17904, 17910, 17912, 17914, 17916, 17918, 17920, 17922, 17924), class = "Date"),
X = c(2790824.18374809, 2790824.18858669, 2790824.18446804, 2790824.1888521, 2790824.18384519, 2790824.18617796, 2790824.18771725, 2790824.18443985, 2790824.18748079, 2790824.18308143, 2790824.18520752, 2790824.18257565, 2790824.18457724, 2790824.18600788, 2790824.18375888, 2790824.1847667, 2790824.18760882, 2790824.19052547, 2790824.18875072, 2790824.19017255, 2790824.19245601, 2790824.19081026, 2790824.19017255, 2790824.18495934, 2790824.18864228, 2790824.18773663, 2790824.18766243, 2790824.18412512, 2790824.18165805, 2790824.18411731, 2790824.1886425, 2790824.18711752, 2790825.7650905, 2790824.1887942, 2790824.18584957, 2790824.18555477, 2790824.18344282, 2790824.18663601, 2790824.18287909, 2790824.18046056, 2790824.18249738, 2790824.18103211, 2790824.18272872, 2790824.18214583, 2790824.18129456, 2790824.18119768, 2790824.19036244, 2790824.19282126, 2790824.19331333, 2790824.19533039, 2790824.18985039, 2790824.19113479, 2790824.1932507, 2790824.18973607, 2790824.18726316, 2790824.18820932, 2790824.18617466, 2790824.18598988, 2790824.18462304, 2790824.18537468, 2790824.18511984, 2790824.1834283, 2790824.1861561, 2790824.18377319, 2790824.18455121, 2790823.77616338, 2790824.18794645, 2790824.18470526, 2790824.18367412, 2790824.18490804, 2790824.18672408, 2790824.18804839, 2790824.18506072, 2790824.18476163, 2790824.18430831, 2790824.18437524, 2790824.18377374, 2790824.17576408, 2790824.18188471, 2790824.17497523, 2790824.17361097, 2790824.17991796, 2790824.18053179, 2790824.18515782, 2790824.18380764, 2790824.18674445, 2790824.18748905, 2790824.18360762, 2790824.17562747, 2790824.17512053, 2790824.29807747, 2790824.29977056, 2790824.29613796, 2790824.29415652, 2790824.28809071, 2790824.28362793, 2790824.28864387, 2790824.28424219, 2790824.27961904, 2790824.28421589, 2790824.28469113, 2790824.28450756, 2790824.28310669, 2790824.28250442, 2790824.28508087, 2790824.28348406, 2790824.28309668, 2790824.28288763, 2790824.28368468, 2790824.28541184, 2790824.2850932, 2790824.28369521, 2790824.27851666, 2790824.28289038, 2790824.28139771, 2790824.27608701, 2790824.25548963, 2790824.2638138, 2790824.27164658, 2790824.25281592, 2790824.25130503, 2790824.25128763, 2790824.25101947, 2790824.2521007, 2790824.25603123, 2790824.25821979, 2790824.25660884, 2790824.27185727, 2790824.26655444, 2790824.25487382, 2790824.24117284, 2790824.24537759, 2790824.23911296, 2790824.23060396, 2790824.25309687, 2790824.26019989, 2790824.26512535, 2790824.2679404, 2790824.26690672, 2790824.25377196, 2790824.26113459),
Y = c(1152771.25205373, 1152771.26044974, 1152771.25753943, 1152771.25978308, 1152771.2558363, 1152771.25737316, 1152771.26221167, 1152771.26008521, 1152771.26175994, 1152771.25406342, 1152771.25888205, 1152771.25536511, 1152771.25642348, 1152771.25771622, 1152771.25637725, 1152771.25125447, 1152771.26784701, 1152771.26538719, 1152771.25912007, 1152771.26242374, 1152771.2644979, 1152771.26553601, 1152771.26377529, 1152771.26209083, 1152771.26141445, 1152771.26246472, 1152771.2604169, 1152771.26242945, 1152771.25709542, 1152771.26461715, 1152771.26770187, 1152771.26539248, 1152770.44825641, 1152771.26541274, 1152771.26600154, 1152771.26392009, 1152771.26404865, 1152771.26264474, 1152771.26493265, 1152771.25979324, 1152771.26063346, 1152771.26042017, 1152771.25733911, 1152771.25714285, 1152771.25710758, 1152771.25825841, 1152771.26857731, 1152771.26889403, 1152771.26763328, 1152771.27012269, 1152771.26569212, 1152771.26643379, 1152771.26831453, 1152771.26453117, 1152771.2659026, 1152771.26211273, 1152771.26160017, 1152771.25949032, 1152771.25648998, 1152771.25802036, 1152771.2564697, 1152771.25572519, 1152771.26994309, 1152771.25856009, 1152771.25577385, 1152771.21190108, 1152771.26022874, 1152771.25793114, 1152771.25677626, 1152771.2583099, 1152771.26039744, 1152771.25265629, 1152771.25645023, 1152771.25709743, 1152771.25675924, 1152771.25475927, 1152771.25495515, 1152771.25653707, 1152771.25558733, 1152771.24623797, 1152771.24770836, 1152771.2549661, 1152771.25507885, 1152771.25924985, 1152771.2628605, 1152771.26486285, 1152771.26706399, 1152771.26320436, 1152771.25670899, 1152771.25969191, 1152771.04282095, 1152771.0422443, 1152771.04935005, 1152771.0453424, 1152771.03681296, 1152771.04392157, 1152771.04540486, 1152771.04963515, 1152771.02783388, 1152771.03612569, 1152771.03824284, 1152771.04113289, 1152771.04169045, 1152771.04252987, 1152771.04051328, 1152771.04079713, 1152771.03747317, 1152771.03739855, 1152771.03942934, 1152771.03740992, 1152771.0364367, 1152771.03294287, 1152771.03230096, 1152771.03823554, 1152771.03634142, 1152771.03405967, 1152771.07550426, 1152771.06546547, 1152771.03697854, 1152771.08281639, 1152771.07903828, 1152771.08392586, 1152771.0829198, 1152771.08307592, 1152771.0774657, 1152771.09114143, 1152771.07792026, 1152771.05372315, 1152771.0585986, 1152771.07327235, 1152771.08565988, 1152771.08055774, 1152771.09170355, 1152771.09050087, 1152771.05223544, 1152771.05222645, 1152771.05033638, 1152771.04887938, 1152771.04816407, 1152771.05337247, 1152771.06691153))))
ui <- fluidPage(
titlePanel("TestApp", windowTitle = "Test Shiny App"),
tabsetPanel(tabPanel("Tab1",
wellPanel(fluidRow(column(selectInput("Number", "Datset number:", names(df)), width = 1),
column(dateRangeInput('dateRange_df',
label = 'Select the time interval for plots',
start = "2018-07-25",
separator = " - ", format = "dd/mm/yy",
startview = 'year' , weekstart = 1), width = 4, offset = 3))),
fluidRow(column(plotOutput("plot1", height = 500, dblclick = "plot1_dblclick", brush = brushOpts(id = "plot1_brush", resetOnNew = TRUE)), width = 6),
column(plotOutput("plot2", height = 500, dblclick = "plot7_dblclick", brush = brushOpts(id = "plot7_brush", resetOnNew = TRUE)), width = 6)))))
server <- function(input,output, session){
# Plot1
ranges1 <- reactiveValues(x = NULL, y = NULL)
output$plot1 <- renderPlot({
if (!is.null(ranges1$x)) {
ranges1$x <- as.Date(ranges1$x, origin = "1970-01-01")
}
if(input$Number == "0"){
col = rgb(165,0,38, max = 255)
}
else if(input$Number == "4"){
col = rgb(215,48,39, max = 255)
}
p <- ggplot(df[[input$Number]], aes(x = Date, y = X)) +
geom_point(colour = col) + coord_cartesian(xlim = ranges1$x, ylim = ranges1$y, expand =FALSE)
p + labs(x = "", y = "X [m]", title = paste("Plot1 - Number ", input$Number))
p + theme(panel.background = element_rect(fill = "grey99"))
p + ggtitle(paste("Plot1 - Dataset number", input$Number))
})
observeEvent(input$plot1_dblclick, {
brush <- input$plot1_brush
if (!is.null(brush)) {
ranges1$x <- c(brush$xmin, brush$xmax)
ranges1$y <- c(brush$ymin, brush$ymax)
} else {
ranges1$x <- NULL
ranges1$y <- NULL
}
})
# Plot2
ranges2 <- reactiveValues(x = NULL, y = NULL)
output$plot2 <- renderPlot({
if (!is.null(ranges2$x)) {
ranges2$x <- as.Date(ranges2$x, origin = "1970-01-01")
}
if(input$Number == "0"){
col = rgb(165,0,38, max = 255)
}
else if(input$Number == "4"){
col = rgb(215,48,39, max = 255)
}
p <- ggplot(df[[input$Number]], aes(x = Date, y = Y)) +
geom_point(colour = col) + coord_cartesian(xlim = ranges2$x, ylim = ranges2$y, expand =FALSE)
p + labs(x = "", y = "Y [m]", title = paste("Plot 2 - Number ", input$Number))
p + theme(panel.background = element_rect(fill = "grey99"))
p + ggtitle(paste("Plot2 - Dataset number ", input$Number))
})
observeEvent(input$plot2_dblclick, {
brush <- input$plot2_brush
if (!is.null(brush)) {
ranges2$x <- c(brush$xmin, brush$xmax)
ranges2$y <- c(brush$ymin, brush$ymax)
} else {
ranges2$x <- NULL
ranges2$y <- NULL
}
})
}
shinyApp(ui, server)
Add an observer to your dateRangeInput that changes the reactive values to match the user input. As long as the user input does not change, the reactive values can change through plot manipulation; when the user inputs a new date range, the zoom resets.
observeEvent(input$dateRange_df, {
ranges1$x <- input$dateRange_df
ranges2$x <- input$dateRange_df
})

Resources