garcianacho commited on
Commit
785c839
·
1 Parent(s): c759701

Upload app.R

Browse files
Files changed (1) hide show
  1. app.R +97 -41
app.R CHANGED
@@ -1,51 +1,107 @@
 
 
 
 
 
 
1
  library(shiny)
2
- library(bslib)
3
- library(dplyr)
4
  library(ggplot2)
5
 
6
- df <- readr::read_csv("penguins.csv")
7
- # Find subset of columns that are suitable for scatter plot
8
- df_num <- df |> select(where(is.numeric), -Year)
9
-
10
- ui <- page_fillable(theme = bs_theme(bootswatch = "minty"),
11
- layout_sidebar(fillable = TRUE,
12
- sidebar(
13
- varSelectInput("xvar", "X variable", df_num, selected = "Bill Length (mm)"),
14
- varSelectInput("yvar", "Y variable", df_num, selected = "Bill Depth (mm)"),
15
- checkboxGroupInput("species", "Filter by species",
16
- choices = unique(df$Species), selected = unique(df$Species)
 
 
 
 
 
 
 
17
  ),
18
- hr(), # Add a horizontal rule
19
- checkboxInput("by_species", "Show species", TRUE),
20
- checkboxInput("show_margins", "Show marginal plots", TRUE),
21
- checkboxInput("smooth", "Add smoother"),
22
- ),
23
- plotOutput("scatter")
24
- )
 
 
 
 
 
 
 
 
 
 
25
  )
26
 
27
- server <- function(input, output, session) {
28
- subsetted <- reactive({
29
- req(input$species)
30
- df |> filter(Species %in% input$species)
31
- })
32
-
33
- output$scatter <- renderPlot({
34
- p <- ggplot(subsetted(), aes(!!input$xvar, !!input$yvar)) + list(
35
- theme(legend.position = "bottom"),
36
- if (input$by_species) aes(color=Species),
37
- geom_point(),
38
- if (input$smooth) geom_smooth()
39
- )
40
-
41
- if (input$show_margins) {
42
- margin_type <- if (input$by_species) "density" else "histogram"
43
- p <- p |> ggExtra::ggMarginal(type = margin_type, margins = "both",
44
- size = 8, groupColour = input$by_species, groupFill = input$by_species)
45
  }
46
 
47
- p
48
- }, res = 100)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  }
 
 
50
 
51
- shinyApp(ui, server)
 
1
+ # Numerical simulation for a low-pass filter
2
+ # Nacho garcia 2018
3
4
+ # GPL v3
5
+
6
+ #Library loading
7
  library(shiny)
 
 
8
  library(ggplot2)
9
 
10
+ # Define UI
11
+ ui <- fluidPage(
12
+
13
+ # Application title
14
+ titlePanel("Low-pass Filter Numerical Simulator"),
15
+
16
+ # Sidebar
17
+ sidebarLayout(
18
+ sidebarPanel(
19
+ img(src='https://upload.wikimedia.org/wikipedia/commons/thumb/3/3b/RC_Divider.svg/200px-RC_Divider.svg.png', align = "center"),
20
+ sliderInput("res", "R value (AU):", 1,100,50),
21
+ sliderInput("cap", "C value (AU):", 1,100,50),
22
+ sliderInput("noise", "Noise Level (AU):", 1,100,50),
23
+ actionButton("do", "RUN", align = "center"),
24
+
25
+ h6("G2C", align = "left"),
26
+
27
+
28
  ),
29
+
30
+ # Plot
31
+ mainPanel(
32
+
33
+ tabsetPanel(
34
+ tabPanel("Plot", plotOutput("Plot")),
35
+ tabPanel("Table", tableOutput("table")),
36
+ tabPanel("Summary", verbatimTextOutput("summary")),
37
+ tabPanel("Info", htmlOutput("help"))
38
+ )
39
+
40
+
41
+
42
+
43
+
44
+ )
45
+ )
46
  )
47
 
48
+ # Define server
49
+ server <- function(input, output) {
50
+
51
+ observeEvent(input$do, {
52
+
53
+ Noise <- as.data.frame(1:200)
54
+ stoch<-input$noise * 0.002
55
+ IN <- runif(200, 0, stoch)
56
+ Signal.Noise <- cbind(Noise, IN)
57
+ Signal.Noise$Out<-0.01
58
+ r<- input$res *0.6
59
+ c<-input$cap *0.3
60
+
61
+ for (i in 2:200) {
62
+
63
+ Signal.Noise$Out[i]<-((Signal.Noise$IN[i-1]-Signal.Noise$Out[i-1])/(r*c*(Signal.Noise$Out[i-1])))
64
+ Signal.Noise$Out[i]<-Signal.Noise$Out[i-1]+Signal.Noise$Out[i]
 
65
  }
66
 
67
+ colnames(Signal.Noise)<-c("Time(AU)","IN","Vout")
68
+
69
+ output$Plot <- renderPlot( {
70
+
71
+ p<-ggplot(Signal.Noise)+
72
+ geom_line(aes(x=`Time(AU)`, y=Vout), colour="red")+
73
+ geom_line(aes(x=`Time(AU)`, y=IN), colour="black")+
74
+ annotate("text", x=15, y=0.20, label= " Output", colour="red", size=5)+
75
+ annotate("text", x=15, y=0.19, label= "Input", colour="black", size=5)+
76
+ xlim(2,200)+
77
+ ylim(0,0.2)+
78
+ theme_minimal()
79
+
80
+ print(p)
81
+
82
+ })
83
+
84
+ output$summary <- renderPrint({
85
+ summary(Signal.Noise)
86
+ })
87
+
88
+ output$table <- renderTable({
89
+ Signal.Noise
90
+ })
91
+
92
+
93
+
94
+
95
+ output$help <- renderUI({
96
+ HTML(paste("","","Electrical filters are combinations of electronic components that transform input signals into modified outputs and one of the most simple filters is the low-pass filter.",
97
+ "","Canonical low-pass filters consist of a resistor and a capacitor connected in parallel (see the scheme on the left) and they have been traditionally used to remove high frequencies from sigmoidal inputs. In this very specific situation, the cut-off frequencies can be calculated from the resistance and capacitance values and the behavior of the filter is very well understood; however, the role of low-pass filters in noise suppression has not been fully explored.",
98
+ "",
99
+ "We became interested in the relationship between low-pass filters and noise-suppression because all biological pathways are very reliable systems which need to effectively distinguish between noise and signal to provide fidelity in the response. Unfortunately, we found that the differential equations that describe the behavior of low-pass filters are not able to integrate the noise in the system in a useful way so to circumvent that problem We developed this simulation.", sep = "</br>"))
100
+ })
101
+
102
+
103
+ })
104
  }
105
+ # Run the application
106
+ shinyApp(ui = ui, server = server)
107