Extract dataframe name in interactive R shiny app - r

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)

Related

Saving output of splice() to a text file

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)

Problems specifying limits and breaks in scale_y_continuous

I'm trying to specify the limits and breaks for primary y-axis and secondary y-axis. Even when I specify the limit and range it is not reflected in the plot.
This is my data:
structure(c(39.571, 36.571, 34, 36.143, 36, 36.571, 38.286, 34.429,
34.429, 34.714, 33, 30.857, 29.429, 26.429, 25.571, 23.714, 22,
21.714, 24.286, 29.857, 38, 48.429, 58.714, 70.857, 79, 87, 92,
99.714, 98.714, 99.571, 101.571, 104.714, 108.857, 112.286, 109,
109.714, 112.714, 112.143, 114.857, 110.857, 107.143, 107.286,
108.143, 107.857, 104, 101.286, 99.571, 105.143, 103.143, 103.286,
98.714, 96, 92.714, 91.286, 84, 83.143, 81.429, 81.286, 82.571,
82.857, 80.429, 77.857, 75.429, 75.143, 73.286, 74.571, 74.429,
77, 86.286, 94.857, 96.286, 112.143, 125.286, 137.714, 150.571,
155, 159.286, 177.714, 175.429, 181.286, 191.143, 181.429, 181.714,
186.857, 176.857, 174, 158.714, 137.143, 137.429, 131.571, 116.571,
103.571, 90.857, 81.571, 74.571, 67.143, 54.286, 46.571, 44,
42.571, 38.286, 37, 34.429, 32.857, 30, 27, 50779.714, 56225.286,
44278.714, 45877.857, 47517.571, 54886.857, 53798.714, 41703.429,
34562.286, 46345.429, 42748.571, 37479, 29665, 28040.714, 26415.857,
29700, 28627.286, 26928.429, 26814, 24773, 22764.143, 22051.571,
16954.286, 15219.571, 14114.571, 12616, 11874.143, 11393.143,
11292, 11069.571, 10708.714, 10678.143, 10497.714, 10555.571,
10725, 10590.286, 11400.714, 11464.571, 11619.571, 11902.143,
10061.857, 12137.714, 12106.429, 11766.143, 12201.143, 12847.714,
13207.143, 15696.857, 13885.286, 14280.714, 14310.143, 13927,
14401.286, 15009.429, 12961.571, 12393.429, 11985.714, 11933.286,
13572.714, 13333.286, 13202.714, 13256, 13780.714, 13920.429,
15253.429, 15070.286, 12243.429, 15389.857, 17777.143, 18269.857,
18231.571, 18068, 17882.286, 20907.571, 18093.286, 15252.286,
18318.143, 18315.571, 18917.714, 19338.429, 19521.143, 19757,
23133.286, 20423.429, 20495.143, 20244.857, 20255.429, 17110.571,
20420.714, 20438, 20552.143, 20564.714, 20735.286, 20657.429,
20560.857, 20434, 19940.143, 20008.714, 20002.286, 19366.429,
19262.429, 22270.429, 15850.143, 12969.286, 10271.857, 18361.714,
17048.571, 16244.571, 18427.143, 18974, 19465.143, 19474.857,
19754, 17078.286, 19794.571, 18497.571, 18808.571, 18728.714,
18802.714, 17188.857, 18340, 18294.857, 18484.286, 18384.714,
18432.143, 18322.429, 18488.714, 18470.857, 17062.143, 18342.143,
18043.571, 17790.143, 17786.286, 17617.714, 17584.286, 18783.714,
17439.143, 17673.429, 16980.714, 18735.143, 19837, 18584.857,
19227.286, 19486.429, 20792.571, 22399.286, 21780.143, 22322.857,
22465, 23116.714, 24210.286, 23845.286, 24647.143, 24631, 23450.286,
25279.571, 25612.143, 25757, 25226.714, 20842.429, 19674.571,
20525.286, 19775, 17391.143, 19574.857, 18560.571, 18557.286,
18185.714, 17893.857, 17653.286, 17493.714, 14296.429, 18005.857,
20428.714, 22131.714, 20787.714, 20700, 21965.857, 21809.143,
18382, 16884.286, 15740.429, 17245.571, 16895.143, 14755.571,
15246.571, 13380, 14490.429, 14149.429, 13948.571, 13610.571,
13204, 11294.429, 12238.143, 11733.143, 12460, 11127.143, 11095.857,
10853.429, 10403.571, 10139.143, 9854, 8584.571, 9430.571, 9142.286,
8862, 8399, 7955.571, 7721.571, 7414.714, 7153, 7221.857, 26971.286,
27864.429, 28658.714, 29754.857, 30715.286, 31865.714, 32252.714,
32683.571, 33662, 34006.714, 34503.143, 34946, 34580.286, 34775,
35072.571, 34658.429, 34846.857, 34589.571, 34066.286, 33711.286,
32905.571, 32273.429, 30992.857, 29788.857, 28763.857, 27492.857,
26285.857, 25187.143, 24251.286, 23697, 22962.286, 22136.286,
21531.714, 20779.857, 20528.571, 20143.286, 19499, 18362.714,
17474, 16704.857, 16540.571, 16404.857, 16168.571, 16168.857,
16856.429, 17032.857, 16557.857, 16044, 15639, 15473.857, 15256.286,
14733.429, 14705.714, 15225.143, 14382.286, 13501.571, 13174.714,
12873.857, 13201.571, 13978.429, 14431.714, 14634.571, 15392.857,
15709.571, 16304.571, 16893.714, 16170.714, 15502, 16666.429,
17292.143, 17539.571, 17377.714, 16727.286, 16559.714, 16361.857,
15838.143, 14969.429, 14440.143, 13905, 13590, 13134.571, 12775.857,
12350.286, 12219.429, 12181.429, 12194, 12425.857, 12469.429,
12459.429, 12371.143, 12317, 12226.429, 12093.143, 11806.143,
11705.571, 11798.429, 11902.143, 11958, 11964.286, 12101.571,
12068, 12279.571, 12233.857, 12245.857, 12163, 12073.714, 20339.571,
20375.429, 21129.286, 20890.429, 20450.571, 20450.571, 20450.571,
20074.429, 17407.429, 19043, 18700.571, 18537, 18537, 18537,
16520.857, 18400.714, 15376, 14907.714, 14019.857, 14019.857,
14019.857, 12250.286, 12117.286, 11389.286, 10825.857, 10211.143,
10211.143, 10211.143, 9367.286, 8800, 8672.714, 8363.857, 8062.714,
8062.714, 8062.714, 7734.429, 6554.857, 6618, 6307.714, 6561.143,
6561.143, 6561.143, 7079.429, 8554.857, 8741.286, 9337.857, 9523,
9523, 9523, 9623.571, 9670.143, 9857, 9932.857, 8245, 8245, 8245,
8594.857, 9085.571, 9704.143, 10473.429, 10473.429, 10473.429,
10473.429, 11347.286, 12720.286, 10332.286, 13805.571, 17442.143,
17442.143, 17442.143, 21848.286, 22096.571, 27649.286, 26723.286,
28829.143, 28829.143, 28829.143, 32095.571, 33360.286, 33747,
34958.286, 35342.286, 35342.286, 35342.286, 36704.429, 37010.714,
36826.286, 35475.143, 34794.143, 34794.143, 34794.143, 32774.714,
31721.714, 30480.429, 29774.857, 28410.143, 28410.143, 28410.143,
23754.286, 21945.429, 20019.429, 18289.857, 16292.143, 16292.143,
16292.143, 13885.857, 22754.429, 22347.857, 22418.143, 22573.286,
22416.714, 22853.857, 22470.857, 22818, 22871.286, 22556.143,
23892.286, 24462.857, 24733.857, 25362, 25364.571, 25308.714,
24829.429, 23317.857, 22311.857, 21318.286, 20416.857, 19566.857,
18321.714, 18124, 17355.571, 16576.571, 16000.857, 15071.857,
14638, 14945.571, 14656.429, 14284.143, 14507.143, 14462.571,
15206.571, 15541.714, 15383, 15439.714, 16313.286, 17085, 17938.571,
18105.571, 18906, 19789.286, 21027.286, 23093.857, 24061.857,
24875.429, 27381.571, 29258.714, 31881.857, 33894.286, 34410.857,
35017, 36094, 35581, 36715.714, 39050.571, 40600.857, 43012.429,
45963.143, 49258.143, 52495.857, 54990.571, 56106.571, 57866.143,
57403, 59507.857, 59828.571, 59809.857, 58003, 55797.429, 53669.714,
53100.714, 51342.571, 48675.429, 46338.857, 45101, 43359, 42120.429,
40573.714, 38350.286, 37239.429, 36016.714, 33821.571, 31923.429,
29977.286, 28667.857, 27071.286, 25606.714, 24332.286, 23821.429,
23355.571, 22476.429, 21322.571, 19902.429, 19182.857, 18427.714,
17780.571, 17142.714, 16265.857, 15238.571, 14666.286, 13950.857,
13249, 12625.857, 35721.1428, 38597.5428, 40037.2572, 42225.7716,
43869.8856, 46015.0284, 46602.9144, 48622.9716, 49383.6, 51783.2,
53799.6572, 56796, 59100.8, 60304.7428, 62707.2, 64033.2572,
65546.9144, 67076.4572, 68091.8856, 68753.6572, 69353.6572, 70009.6,
70672.8, 71233.8284, 66688.6284, 67262.9716, 65909.3716, 65532.0572,
64746.4, 65458.9144, 66555.3144, 72879.3144, 74252.4, 77641.3144,
79969.7144, 81933.0284, 84010.4572, 85141.2, 85618.1716, 85997.3716,
86092.9144, 86472.0572, 86389.7716, 85551.0284, 86927.0284, 87409.7716,
88116.1144, 86699.0856, 86705.9428, 87017.7716, 86334.9716, 85364.7428,
82740.9716, 73923.6, 75936.6856, 74116.0572, 72719.7716, 72813.4856,
72999.4856, 75328.3428, 78506.3428, 82702.8, 85723.8284, 86273.3716,
88253.1428, 89504.6284, 91951.6, 99934.4572, 97745.4284, 98015.4284,
99739.6, 99323.1428, 97949.0856, 95562.6284, 92629.0856, 89131.3716,
87081.2572, 83003.6572, 80189.1428, 77486.7428, 75091.3144, 72195.6,
70445.5428, 67798.1716, 68283.3144, 66546.9144, 64826.8572, 63430.5716,
62063.7716, 60461.0856, 59365.1428, 58407.6572, 56569.9428, 54798,
52201.8856, 50341.6, 48165.8284, 46890.6856, 44341.4284, 43251.3144,
41721.8856, 40702.7428, 38757.2, 37574.6856, 36166.5144, 34118.2856
), .Dim = c(106L, 7L), .Dimnames = list(NULL, c("China", "France",
"Germany", "Italy", "Spain", "United Kingdom", "US")), index = structure(c(18568,
18569, 18570, 18571, 18572, 18573, 18574, 18575, 18576, 18577,
18578, 18579, 18580, 18581, 18582, 18583, 18584, 18585, 18586,
18587, 18588, 18589, 18590, 18591, 18592, 18593, 18594, 18595,
18596, 18597, 18598, 18599, 18600, 18601, 18602, 18603, 18604,
18605, 18606, 18607, 18608, 18609, 18610, 18611, 18612, 18613,
18614, 18615, 18616, 18617, 18618, 18619, 18620, 18621, 18622,
18623, 18624, 18625, 18626, 18627, 18628, 18629, 18630, 18631,
18632, 18633, 18634, 18635, 18636, 18637, 18638, 18639, 18640,
18641, 18642, 18643, 18644, 18645, 18646, 18647, 18648, 18649,
18650, 18651, 18652, 18653, 18654, 18655, 18656, 18657, 18658,
18659, 18660, 18661, 18662, 18663, 18664, 18665, 18666, 18667,
18668, 18669, 18670, 18671, 18672, 18673), class = c("IDate",
"Date")), class = "zoo")
Ant this is my code (data=n) :
ggplot(aes(x = Index, y = Value, group = Series, colour = Series, linetype = Series),
data = fortify(n, melt = TRUE)) + geom_line(linetype = "solid" , size= 1) + xlab("") + ylab("")+
scale_y_continuous(labels = scales::label_number(accuracy =1,breaks = seq(from = 0,to= 80000, by=20000)), expand = c(0,0),
sec.axis = sec_axis(~ . * 2.5, labels = scales::label_number(accuracy =1),breaks= seq(from = 0,to = 250000, by=50000)))+
scale_x_date(date_label="%b/%y",date_breaks ="2 months")+
theme_classic()
This is the outcome
As you can see in the plot the limits for the primary y-axis goes from 25 000 to 75 000 , instead of going from 0 to 80 000 with 20 000 (increment of sequence). The same thing happen with the secondary y-axis, the limits for the secondary-y axis goes from 40 000 to 240 000 with 40 000(increment of sequence) instead of going from 0 to 250 000 with 50 000(increment of sequence).
Is there another way to specify the breaks and limits so that it is display in my plot? Thank you in advance.
You could specify the y-axis limits with coord_cartesian and modify your breaks in the primary y-axis.
df <- n %>% fortify(melt = TRUE) %>% as_tibble()
df %>%
ggplot(aes(x = Index, y = Value, group = Series, colour = Series, linetype = Series)) +
geom_line(linetype = "solid" , size= 1) +
xlab("") + ylab("") +
scale_y_continuous( breaks = seq(from = 0,to = 100000, by = 20000), ## Modified breaks instructions
labels = scales::label_number(accuracy = 1),
sec.axis = sec_axis(~ . * 2.5,
labels = scales::label_number(accuracy = 1),
breaks= seq(from = 0,to = 250000, by = 50000))) +
scale_x_date(date_label="%b/%y",date_breaks ="2 months") +
coord_cartesian(ylim = c(0,100000)) + ## Here is what you need to add
theme_classic()

Add a 3d surface at point 0 in plotly

I have a 3D plot using plotly which can take on negative and positive values. I would like to add a see through 3D flat surface at point 0 (see through here means setting alpha = 0.3 as in ggplot2) in order to emphasize the positive/negative values a little more. Alternatively adding a "grid" as in the background through the point 0 would be useful.
Code:
library(plotly)
library(dplyr)
df %>%
select(date, everything()) %>%
plot_ly(
x = colnames(subset(., select = c(2:4))), # probably not the most efficient method
y = ~date,
z = data.matrix(subset(., select = c(2:4))),
type = "surface",
colors = c("darkblue", "yellow", "darkred")
)
Data:
df <- structure(list(wind = c(0.938535690307617, 0.976551234722137,
0.954551994800568, 0.920722126960754, 0.889751732349396, 0.701366603374481,
0.718878328800201, 0.685763895511627, 0.677822828292847, 0.876205325126648,
0.67054146528244, 0.738650739192963, 0.725420415401459, 0.851324200630188,
0.6589714884758, 0.880357265472412, 0.997677683830261, 0.968335390090942,
0.882899045944214, 1.09453165531158, 1.06842839717865, 0.819347560405731,
0.995919525623322, 0.695173263549805, 0.860199570655823, 0.977508246898651,
0.807886302471161, 0.778182446956635, 0.886279463768005, 0.879809498786926,
0.903579652309418, 1.09579062461853, 1.03768181800842, 0.802022874355316,
0.928451955318451, 0.744936227798462, 0.621560990810394, 0.706887602806091,
0.749234974384308, 0.746754884719849, 0.65381270647049, 0.710927248001099,
0.517793655395508, 0.477172255516052, 0.631662607192993, 0.55011123418808,
0.584086775779724, 0.617783904075623, 0.438043504953384, 0.577566087245941,
0.684398949146271, 1.00848543643951, 0.957233726978302, 0.84071296453476,
0.90149587392807, 0.888661623001099, 0.923080563545227, 0.945727407932281,
0.965020060539246, 1.07903909683228, 0.870955109596252, 0.934546709060669,
0.93314516544342, 0.926109850406647, 0.981102645397186, 0.770903468132019,
0.829349219799042, 0.980291783809662, 1.00809383392334, 0.821328639984131,
0.89130437374115, 0.886775732040405, 0.896964132785797, 1.08184945583344,
1.17395043373108, 1.11537420749664, 1.15379846096039, 1.20203125476837,
1.12583827972412, 1.10014677047729, 0.891100168228149, 1.01484513282776,
1.01270127296448, 0.895487844944, 0.877909421920776, 1.11780989170074,
1.18047833442688, 1.14531397819519, 1.28634309768677, 1.23702371120453,
1.01186645030975, 1.15563869476318, 1.0918824672699, 1.2023059129715,
1.11806273460388, 1.11690294742584, 1.13390302658081, 1.10064888000488,
1.1418149471283, 1.06309700012207), holiday = c(-0.323977619409561,
-0.32814821600914, -0.335260361433029, -0.355545252561569, -0.347469061613083,
-0.349574476480484, -0.331554502248764, -0.351303607225418, -0.342486947774887,
-0.34791961312294, -0.342795491218567, -0.344868391752243, -0.344075173139572,
-0.342763870954514, -0.365125387907028, -0.301046937704086, -0.303365021944046,
-0.301353365182877, -0.316689401865005, -0.312132269144058, -0.317412197589874,
-0.331865310668945, -0.306569844484329, -0.306523144245148, -0.328399240970612,
-0.328934848308563, -0.340233236551285, -0.310919582843781, -0.352815061807632,
-0.324335247278214, -0.333071410655975, -0.25605234503746, -0.28141376376152,
-0.267955660820007, -0.261935144662857, -0.23365119099617, -0.204256281256676,
-0.271868377923965, -0.266900211572647, -0.268673747777939, -0.246791422367096,
-0.261210560798645, -0.275096118450165, -0.272033154964447, -0.307673662900925,
-0.316463977098465, -0.315838783979416, -0.278884381055832, -0.29575651884079,
-0.300951153039932, -0.275152236223221, -0.292507320642471, -0.283683449029922,
-0.333169460296631, -0.323910266160965, -0.317758291959763, -0.318425863981247,
-0.318803340196609, -0.316879868507385, -0.321441829204559, -0.342198520898819,
-0.336640536785126, -0.334549427032471, -0.304668426513672, -0.281678169965744,
-0.287939876317978, -0.278673946857452, -0.299236595630646, -0.295697629451752,
-0.297617554664612, -0.300621718168259, -0.294943511486053, -0.291149199008942,
-0.263380706310272, -0.264828890562057, -0.260132044553757, -0.294039487838745,
-0.284087061882019, -0.27564924955368, -0.275646597146988, -0.294898957014084,
-0.290615618228912, -0.264040410518646, -0.267250239849091, -0.27454400062561,
-0.22650308907032, -0.220207497477531, -0.233633011579514, -0.205283910036087,
-0.201104089617729, -0.235431581735611, -0.252072185277939, -0.257701843976974,
-0.233586445450783, -0.239779070019722, -0.233227252960205, -0.26639312505722,
-0.256356805562973, -0.248022571206093, -0.285306513309479),
month_10 = c(0.109010718762875, 0.112969301640987, 0.110368527472019,
0.107052445411682, 0.0947703272104263, 0.109930463135242,
0.106371931731701, 0.114236004650593, 0.102411419153214,
0.0184143912047148, 0.0301767271012068, 0.0376978516578674,
0.0472327470779419, 0.0622735135257244, 0.043902475386858,
0.0611664243042469, 0.0618763938546181, 0.0555795393884182,
0.0623081848025322, 0.0636096075177193, 0.0675770491361618,
0.0629641935229301, 0.0288578178733587, 0.025121470913291,
0.0300221722573042, 0.0611664243042469, 0.0659252777695656,
0.0159557648003101, 0.0376978516578674, 0.032174538820982,
0.0317839048802853, 0.0626140907406807, 0.0577763170003891,
0.0571180321276188, 0.0571180321276188, 0.0590739175677299,
0.0760120898485184, 0.0670360922813416, 0.0699110627174377,
0.0717969089746475, 0.0640148967504501, 0.0670360922813416,
0.164964601397514, 0.152724280953407, 0.115071900188923,
0.129654854536057, 0.122471310198307, 0.0768138542771339,
0.0400041155517101, 0.0573667995631695, 0.0685276389122009,
0.0337212830781937, 0.0616994015872478, 0.0589617975056171,
0.059433214366436, 0.0567467175424099, 0.0661386772990227,
0.0704409778118134, 0.0611028559505939, 0.0579087659716606,
0.0622764676809311, 0.0538184903562069, 0.0553129874169827,
0.058912742882967, 0.059433214366436, 0.0575473643839359,
0.0575473643839359, 0.058104183524847, 0.055678017437458,
0.0575473643839359, 0.059433214366436, 0.0603105537593365,
0.0589617975056171, 0.0601625964045525, 0.0577935017645359,
0.0574705749750137, 0.0550192892551422, 0.0475285314023495,
0.0577935017645359, 0.0543489865958691, 0.0526755712926388,
0.0553129874169827, 0.0589617975056171, 0.0593133755028248,
0.0547068528831005, 0.0676216259598732, 0.0511428378522396,
0.0564789660274982, 0.0641900449991226, 0.0666491389274597,
0.0390096306800842, 0.058005329221487, 0.0449355207383633,
0.062143836170435, 0.0591179206967354, 0.0637796893715858,
0.0633300691843033, 0.0702810436487198, 0.0647093132138252,
0.0427459664642811), month_12 = c(0.0733551606535912, 0.0707022771239281,
0.0707631036639214, 0.0718683376908302, 0.071540854871273,
0.0993443354964256, 0.0973133370280266, 0.0668289735913277,
0.0731536969542503, 0.0717423185706139, 0.0750747397542,
0.0754749700427055, 0.0746353641152382, 0.0912141725420952,
0.100288398563862, 0.07671108096838, 0.0708827450871468,
0.0708827450871468, 0.0741974636912346, 0.0761196836829185,
0.0648202076554298, 0.114817388355732, 0.100519739091396,
0.100288398563862, 0.102664910256863, 0.0566539540886879,
0.0648743882775307, 0.0706770494580269, 0.0746353641152382,
0.0750747397542, 0.0750747397542, 0.113439425826073, 0.127338454127312,
0.114467553794384, 0.112322382628918, 0.136203452944756,
0.131634846329689, 0.144591823220253, 0.139709115028381,
0.140504062175751, 0.145617410540581, 0.166768744587898,
0.151759415864944, 0.151587069034576, 0.156320676207542,
0.131974145770073, 0.135847419500351, 0.159762278199196,
0.163790658116341, 0.158604919910431, 0.127949997782707,
0.109744042158127, 0.101787634193897, 0.0582009926438332,
0.0671374276280403, 0.0669510439038277, 0.074992410838604,
0.074992410838604, 0.0787725821137428, 0.0696783438324928,
0.0965322777628899, 0.0666131302714348, 0.0666131302714348,
0.0925824269652367, 0.0875711515545845, 0.0873847678303719,
0.0873847678303719, 0.0711322501301765, 0.0696783438324928,
0.0667495802044868, 0.0667495802044868, 0.0685289725661278,
0.0582009926438332, 0.0599979534745216, 0.0702869072556496,
0.068938173353672, 0.0610831864178181, 0.074992410838604,
0.0702869072556496, 0.0683296099305153, 0.0671374276280403,
0.0666131302714348, 0.0815844461321831, 0.070037417113781,
0.068938173353672, 0.0862899348139763, 0.0824234709143639,
0.0761196836829185, 0.0663527771830559, 0.0751885995268822,
0.072908379137516, 0.0599117167294025, 0.0743952021002769,
0.0666681602597237, 0.0667495802044868, 0.0748060271143913,
0.0800538137555122, 0.0787725821137428, 0.0741974636912346,
0.0741937384009361), date = structure(c(14610, 14611, 14612,
14613, 14614, 14615, 14616, 14617, 14618, 14619, 14620, 14621,
14622, 14623, 14624, 14625, 14626, 14627, 14628, 14629, 14630,
14631, 14632, 14633, 14634, 14635, 14636, 14637, 14638, 14639,
14640, 14641, 14642, 14643, 14644, 14645, 14646, 14647, 14648,
14649, 14650, 14651, 14652, 14653, 14654, 14655, 14656, 14657,
14658, 14659, 14660, 14661, 14662, 14663, 14664, 14665, 14666,
14667, 14668, 14669, 14670, 14671, 14672, 14673, 14674, 14675,
14676, 14677, 14678, 14679, 14680, 14681, 14682, 14683, 14684,
14685, 14686, 14687, 14688, 14689, 14690, 14691, 14692, 14693,
14694, 14695, 14696, 14697, 14698, 14699, 14700, 14701, 14702,
14703, 14704, 14705, 14706, 14707, 14708, 14709), class = "Date")), row.names = c(NA,
100L), class = "data.frame")
Adding a plane at the value of 0 for z:
library(plotly)
library(dplyr)
# I separated your df wrangling to better understand the data
df <- df %>%
select(date, everything())
# Code you posted
df %>%
plot_ly(
x = colnames(subset(., select = c(2:4))), # probably not the most efficient method
y = ~date,
z = data.matrix(subset(., select = c(2:4))),
type = "surface",
colors = c("darkblue", "yellow", "darkred")
) %>%
# The surface added by using y & x from df
# and making z a matrix of zeros 3 rows by 100 columns.
add_surface(z = matrix(0, ncol = 3, nrow = 100),
y = df$date,
x = colnames(subset(df, select = c(2:4))),
opacity = .8)
Your plot:
With added zero z plane:

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