@@ -22,15 +22,23 @@ library(tidyverse)
2222# User Interface
2323ui <- fluidPage(
2424 # Upload the file
25- fileInput("input_file", "Upload file"),
25+ fileInput(inputId = "input_file",
26+ label = "Upload file"),
2627 # Select from the dropdown menu the column you want on the x-axis
27- selectInput("x_axis_input", "Select x-axis", choices = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")),
28+ selectInput(inputId = "x_axis_input",
29+ label = "Select x-axis",
30+ choices = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
31+ ),
2832 # Select from the dropdown menu the column you want on the y-axis
29- selectInput("y_axis_input", "Select y-axis", choices = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")),
33+ selectInput(inputId = "y_axis_input",
34+ label = Select y-axis",
35+ choices = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
36+ ),
3037 # The output plot
31- plotOutput("plot"),
38+ plotOutput(outputId = "plot"),
3239 # The download plot button
33- downloadButton("download_button", "Download the data .png")
40+ downloadButton(outputId = "download_button",
41+ label = "Download the data .png")
3442)
3543
3644# Server
@@ -43,7 +51,8 @@ server <- function(input, output) {
4351 # Reactive expression to create a scatterplot from the uploaded data and user selected axes
4452 iris_plot <- reactive ({
4553 ggplot(iris_data()) +
46- geom_point(aes_string(x = input$x_axis_input, y = input$y_axis_input))
54+ geom_point(aes(x = .data[[input$x_axis_input]],
55+ y = .data[[input$y_axis_input]]))
4756 })
4857 # Render the plot from the iris_plot() reactive expression
4958 output$plot <- renderPlot({
@@ -57,9 +66,10 @@ server <- function(input, output) {
5766 },
5867 # The content of the file will be the contents of the iris_plot() reactive expression
5968 content = function(file) {
60- png(file)
61- print(iris_plot())
62- dev.off()
69+ ggsave(
70+ filename = file,
71+ plot = iris_plot()
72+ )
6373 }
6474 )
6575}
@@ -83,23 +93,29 @@ ui <- fluidPage(
8393 sidebarLayout(
8494 sidebarPanel(
8595 # Upload the file
86- fileInput("input_file", "Upload file"),
96+ fileInput(inputId = "input_file",
97+ label = "Upload file"),
8798 # Select from the dropdown menu the column you want on the x-axis
88- selectInput("x_axis_input", "Select x-axis", choices = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")),
99+ selectInput(inputId = "x_axis_input",
100+ label = "Select x-axis",
101+ choices = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")),
89102 # Select from the dropdown menu the column you want on the y-axis
90- selectInput("y_axis_input", "Select y-axis", choices = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"))
103+ selectInput(inputId = "y_axis_input",
104+ label = "Select y-axis",
105+ choices = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"))
91106 ),
92107 mainPanel(
93108 # The output plot
94- plotOutput("plot")
109+ plotOutput(outputId = "plot")
95110 )
96111 ),
97112 hr(),
98113 fluidRow(
99114 column(12,
100115 align = "right",
101116 # The download plot button
102- downloadButton("download_button", "Download the data .png")
117+ downloadButton(outputId = "download_button",
118+ label = "Download the data .png")
103119 )
104120 )
105121)
@@ -114,7 +130,8 @@ server <- function(input, output) {
114130 # Reactive expression to create a scatterplot from the uploaded data and user selected axes
115131 iris_plot <- reactive ({
116132 ggplot(iris_data()) +
117- geom_point(aes_string(x = input$x_axis_input, y = input$y_axis_input))
133+ geom_point(aes(x = .data[[input$x_axis_input]],
134+ y = .data[[input$y_axis_input]]))
118135 })
119136 # Render the plot from the iris_plot() reactive expression
120137 output$plot <- renderPlot({
@@ -128,9 +145,10 @@ server <- function(input, output) {
128145 },
129146 # The content of the file will be the contents of the iris_plot() reactive expression
130147 content = function(file) {
131- png(file)
132- print(iris_plot())
133- dev.off()
148+ ggsave(
149+ filename = file,
150+ plot = iris_plot()
151+ )
134152 }
135153 )
136154}
@@ -149,42 +167,48 @@ library(shiny)
149167library(ggplot2)
150168library(shinythemes)
151169
152- ui <- fluidPage(theme = shinytheme("superhero"),
153- ...
170+ ui <- fluidPage(
171+ theme = shinytheme("superhero"),
172+ ...
154173)
155174```
156175
157176The final app should look like:
158177
159- ``` {r}
160- #| label: complete_answer
161- #| eval: false
178+ ```
162179# Load libraries
163180library(shiny)
164181library(ggplot2)
165182library(shinythemes)
166183
167- ui <- fluidPage(theme = shinytheme("superhero"),
184+ ui <- fluidPage(
185+ theme = shinytheme("superhero"),
168186 sidebarLayout(
169187 sidebarPanel(
170188 # Upload the file
171- fileInput("input_file", "Upload file"),
189+ fileInput(inputId = "input_file",
190+ label = "Upload file"),
172191 # Select from the dropdown menu the column you want on the x-axis
173- selectInput("x_axis_input", "Select x-axis", choices = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")),
192+ selectInput(inputId = "x_axis_input",
193+ label = "Select x-axis",
194+ choices = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")),
174195 # Select from the dropdown menu the column you want on the y-axis
175- selectInput("y_axis_input", "Select y-axis", choices = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"))
196+ selectInput(inputId = "y_axis_input",
197+ label = "Select y-axis",
198+ choices = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"))
176199 ),
177200 mainPanel(
178201 # The output plot
179- plotOutput("plot")
202+ plotOutput(outputId = "plot")
180203 )
181204 ),
182205 hr(),
183206 fluidRow(
184207 column(12,
185208 align = "right",
186209 # The download plot button
187- downloadButton("download_button", "Download the data .png")
210+ downloadButton(outputId = "download_button",
211+ label = "Download the data .png")
188212 )
189213 )
190214)
@@ -199,7 +223,8 @@ server <- function(input, output) {
199223 # Reactive expression to create a scatterplot from the uploaded data and user selected axes
200224 iris_plot <- reactive ({
201225 ggplot(iris_data()) +
202- geom_point(aes_string(x = input$x_axis_input, y = input$y_axis_input))
226+ geom_point(aes(x = .data[[input$x_axis_input]],
227+ y = .data[[input$y_axis_input]]))
203228 })
204229 # Render the plot from the iris_plot() reactive expression
205230 output$plot <- renderPlot({
@@ -213,13 +238,11 @@ server <- function(input, output) {
213238 },
214239 # The content of the file will be the contents of the iris_plot() reactive expression
215240 content = function(file) {
216- png(file)
217- print(iris_plot())
218- dev.off()
241+ ggsave(
242+ filename = file,
243+ plot = iris_plot()
244+ )
219245 }
220246 )
221247}
222-
223- # Run the app
224- shinyApp(ui = ui, server = server)
225248```
0 commit comments