如何使ui.R和server.R从ShinyApp中的其他文件和绘图导入函数和数据
我有一个三部分的文件,我想用它来制作闪亮的应用程序 酷图 这是一个存储打印功能的代码如何使ui.R和server.R从ShinyApp中的其他文件和绘图导入函数和数据,r,shiny,rstudio,R,Shiny,Rstudio,我有一个三部分的文件,我想用它来制作闪亮的应用程序 酷图 这是一个存储打印功能的代码 library(ggplot2) library(tidyverse) library(ggrepel) #------------------------- # Function #------------------------- plotit <- function (dat, x_thres, y_thres) { dat["Significant"] <- ifelse((dat$
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)
用户界面
我的问题是如何从plot.R
和
显示绘图
目前,在RStudio中,它看起来像这样(减去我的评论)
这是你想要的吗?请确保您的滑块具有唯一的名称
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)
库(ggplot2)
图书馆(tidyverse)
图书馆(ggrepel)
图书馆(闪亮)
#-------------------------
#作用
#-------------------------
情节、不签名、签名)
p x|thres | mpg>y|thres),
aes(重量、mpg、标签=型号),
box.padding=单位(0.35,“线”),
点填充=单位(0.3,“线”)
) +
主题(legend.position=“无”)
返回(p)
}
#-------------------------
#开始主代码
#-------------------------
#我确实想使用文件作为输入,而不是
#默认mtcars变量
你想要什么?请确保您的滑块具有唯一的名称
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)
库(ggplot2)
图书馆(tidyverse)
图书馆(ggrepel)
图书馆(闪亮)
#-------------------------
#作用
#-------------------------
情节、不签名、签名)
p x|thres | mpg>y|thres),
aes(重量、mpg、标签=型号),
box.padding=单位(0.35,“线”),
点填充=单位(0.3,“线”)
) +
主题(legend.position=“无”)
返回(p)
}
#-------------------------
#开始主代码
#-------------------------
#我确实想使用文件作为输入,而不是
#默认mtcars变量
谢谢。差不多了。我需要将coolplot.R
与ui.R
和server.R
分开,我该怎么做?你可以通过source
添加这个,比如source('coolplot.R',local=TRUE)
。但是您需要指定源文件中的文件目录callI在服务器.r中添加了一行,我认为应该在那里尝试。我得到了这个错误参数意味着不同的行数:2,0对象'xintercept'未找到1
。如果您能在单独的代码ui.R
、server.R
和coolplot.R
中给出示例,我将不胜感激。否,请仅将其放入服务器。请确保文件已加载…谢谢。差不多了。我需要将coolplot.R
与ui.R
和server.R
分开,我该怎么做?你可以通过source
添加这个,比如source('coolplot.R',local=TRUE)
。但是您需要指定源文件中的文件目录callI在服务器.r中添加了一行,我认为应该在那里尝试。我得到了这个错误参数意味着不同的行数:2,0对象'xintercept'未找到1
。如果您能在单独的代码ui.R
、server.R
和coolplot.R
中给出示例,我将不胜感激。否,请仅将其放入服务器。只要确保为它加载文件。。。
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)
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)
}
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)