Reputation: 63984
I have a three parts of file that I want to use to make Shiny app.
This is a code that stores function for plotting.
library(ggplot2)
library(tidyverse)
library(ggrepel)
#-------------------------
# Function
#-------------------------
plotit <- function (dat, x_thres, y_thres) {
dat["Significant"] <- ifelse((dat$wt > x_thres |
dat$mpg > y_thres ), 'NotSignif','Signif')
p <- ggplot(dat, aes(wt, mpg)) +
geom_point(alpha=0.8,size=2.75, aes(color=Significant)) +
scale_color_manual(values=c("#B94024","#7D8D87")) +
geom_vline(xintercept= x_thres, colour = '#B94024') +
geom_hline(yintercept=y_thres, colour = '#B94024') +
geom_text_repel(data=subset(dat, wt > x_thres | mpg > y_thres),
aes(wt,mpg,label=model),
box.padding = unit(0.35, "lines"),
point.padding = unit(0.3, "lines")
) +
theme(legend.position="none")
return(p)
}
#-------------------------
# Begin main code
#-------------------------
# I literally want to use file as input not
# default mtcars variable
infile <- "https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv"
dat <- read_delim(infile,delim=",", col_types = cols())
y_thres <- 25
x_thres <- 3
plotit(dat, x_thres, y_thres)
The function basically takes the x-threshold and y-threshold and make the plot like this:
Then I tried to construct the Shiny app that lets the user
slide through x-threshold and y-threshold based on the same input data
and calling the plotit
function. As they slide the vertical, horizontal red lines, and labeling dots will change accordingly.
My Shiny app files are these:
library(shiny)
# Define server logic for slider examples
function(input, output) {
# Reactive expression to compose a data frame containing all of
# the values
sliderValues <- reactive({
# Compose data frame
data.frame(
Name = c("X-threshold",
"Y-threshold"
),
Value = as.character(c(input$integer,
input$integer
)),
stringsAsFactors=FALSE)
})
# Show the values using an HTML table
output$values <- renderTable({
sliderValues()
})
}
library(shiny)
# Define UI for slider demo application
fluidPage(
# Application title
titlePanel("Sliders"),
# Sidebar with sliders that demonstrate various available
# options
sidebarLayout(
sidebarPanel(
# Simple integer interval
sliderInput("integer", "X-threshold",
min=3, max=10, value=1),
# Simple integer interval
sliderInput("integer", "Y-threshold",
min=10, max=35, value=1)
),
# Show a table summarizing the values entered
mainPanel(
tableOutput("values")
# How can I output the plot from coolplot.R here????
)
)
)
My question is how can I make ui.R
import function from plot.R
and
display the plot?
Currently it the Shiny looks like this (minus my comment), in RStudio.
Upvotes: 0
Views: 2092
Reputation: 29387
Is what you want? make sure your sliders have unique names please
library(ggplot2)
library(tidyverse)
library(ggrepel)
library(shiny)
#-------------------------
# Function
#-------------------------
plotit <- function (dat, x_thres, y_thres) {
dat["Significant"] <- ifelse((dat$wt > x_thres |
dat$mpg > y_thres ), 'NotSignif','Signif')
p <- ggplot(dat, aes(wt, mpg)) +
geom_point(alpha=0.8,size=2.75, aes(color=Significant)) +
scale_color_manual(values=c("#B94024","#7D8D87")) +
geom_vline(xintercept= x_thres, colour = '#B94024') +
geom_hline(yintercept=y_thres, colour = '#B94024') +
geom_text_repel(data=subset(dat, wt > x_thres | mpg > y_thres),
aes(wt,mpg,label=model),
box.padding = unit(0.35, "lines"),
point.padding = unit(0.3, "lines")
) +
theme(legend.position="none")
return(p)
}
#-------------------------
# Begin main code
#-------------------------
# I literally want to use file as input not
# default mtcars variable
infile <- "https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv"
dat <- read_delim(infile,delim=",", col_types = cols())
y_thres <- 25
x_thres <- 3
plotit(dat, x_thres, y_thres)
ui <- shinyUI(
fluidPage(
# Application title
titlePanel("Sliders"),
# Sidebar with sliders that demonstrate various available
# options
sidebarLayout(
sidebarPanel(
# Simple integer interval
sliderInput("integer", "X-threshold",
min=3, max=10, value=1),
# Simple integer interval
sliderInput("integer2", "Y-threshold",
min=10, max=35, value=1)
),
# Show a table summarizing the values entered
mainPanel(
tableOutput("values"),
plotOutput("myplot")
# How can I output the plot from coolplot.R here????
)
)
)
)
server <- shinyServer(function(input, output, session) {
# Reactive expression to compose a data frame containing all of
# the values
sliderValues <- reactive({
# Compose data frame
data.frame(Name = c("X-threshold", "Y-threshold"),
Value = as.character(c(input$integer, input$integer2)),
stringsAsFactors=FALSE)
})
# Show the values using an HTML table
output$values <- renderTable({
sliderValues()
})
output$myplot <- renderPlot({
plotit(dat, input$integer, input$integer2)
})
})
shinyApp(ui = ui, server = server)
Edit: load the plot function from a file
coolplot.R
library(ggplot2)
library(tidyverse)
library(ggrepel)
#-------------------------
# Function
#-------------------------
#-------------------------
# Begin main code
#-------------------------
# I literally want to use file as input not
# default mtcars variable
infile <- "https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv"
dat <- read_delim(infile,delim=",", col_types = cols())
plotit <- function (dat, x_thres, y_thres) {
dat["Significant"] <- ifelse((dat$wt > x_thres |
dat$mpg > y_thres ), 'NotSignif','Signif')
p <- ggplot(dat, aes(wt, mpg)) +
geom_point(alpha=0.8,size=2.75, aes(color=Significant)) +
scale_color_manual(values=c("#B94024","#7D8D87")) +
geom_vline(xintercept= x_thres, colour = '#B94024') +
geom_hline(yintercept=y_thres, colour = '#B94024') +
geom_text_repel(data=subset(dat, wt > x_thres | mpg > y_thres),
aes(wt,mpg,label=model),
box.padding = unit(0.35, "lines"),
point.padding = unit(0.3, "lines")
) +
theme(legend.position="none")
return(p)
}
Shiny Part
library(shiny)
source("coolplot.R",local = TRUE)$value
ui <- shinyUI(
fluidPage(
# Application title
titlePanel("Sliders"),
# Sidebar with sliders that demonstrate various available
# options
sidebarLayout(
sidebarPanel(
# Simple integer interval
sliderInput("integer", "X-threshold",
min=3, max=10, value=1),
# Simple integer interval
sliderInput("integer2", "Y-threshold",
min=10, max=35, value=1)
),
# Show a table summarizing the values entered
mainPanel(
tableOutput("values"),
plotOutput("myplot")
# How can I output the plot from coolplot.R here????
)
)
)
)
server <- shinyServer(function(input, output, session) {
# Reactive expression to compose a data frame containing all of
# the values
# Add the source file of the plot if necessary
sliderValues <- reactive({
# Compose data frame
data.frame(Name = c("X-threshold", "Y-threshold"),
Value = as.character(c(input$integer, input$integer2)),
stringsAsFactors=FALSE)
})
# Show the values using an HTML table
output$values <- renderTable({
sliderValues()
})
output$myplot <- renderPlot({
plotit(dat, input$integer, input$integer2)
})
})
shinyApp(ui = ui, server = server)
Upvotes: 2