如何避免在 Shiny 应用程序中多次重复相同的过程
How to avoid repeating the same procedure multiple times in Shiny app
我写了一个闪亮的应用程序,我从泊松分布中抽样。每次我想输出数据图或摘要时,我都重写了函数。我宁愿只使用该函数一次,然后让所有图表和摘要都引用该函数被调用的那一次,否则输出的结果将不相同。当我尝试调用该函数一次并将结果存储为变量以供 ggplot 使用时,我收到错误消息,即 ggplot 无法使用反应数据,而且我无法将其强制转换为数据框。
我尝试了以下变体:
mydata <- reactive({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
Muts
})
但是它们不起作用,因为 ggplot 无法使用输入。我想我没有正确使用反应函数。非常感谢任何有助于减少代码冗余并确保图表和摘要都使用相同的单一基础数据的帮助。提前谢谢你。
我当前的代码:
server.R
library(shiny)
library(ggplot2)
# Define server logic required to draw a histogram
function(input, output) {
output$distPlot <- renderPlot({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
# draw the density plot
ggplot(Muts, aes(Muts)) + geom_density()
})
output$distPlot2 <-renderPlot({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
Muts <- as.data.frame(Muts)
ggplot(Muts, aes(Muts)) + geom_histogram()
})
output$summary <- renderPrint({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
summary(Muts)
})
}
你的想法是对的,但是 reactive
本质上是一个缓存函数,需要调用它来检索它的 return 值,所以 mydata()
是正确的,mydata
只是 return 函数本身(不是 return 值)。
library(shiny)
library(ggplot2)
# Define UI for application
ui <- fluidPage(
# Application title
titlePanel(""),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("x",
"Number of bins:",
min = 1,
max = 50,
value = 30),
sliderInput("y",
"Number of bins:",
min = 1,
max = 50,
value = 30),
sliderInput("z",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
plotOutput("distPlot2"),
verbatimTextOutput("summary")
)
)
)
# Define server logic
server <- function(input, output) {
mydata <- reactive({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
Muts
})
output$distPlot <- renderPlot({
Muts <- mydata()
ggplot(Muts, aes(Muts)) + geom_density()
})
output$distPlot2 <-renderPlot({
Muts <- mydata()
ggplot(Muts, aes(Muts)) + geom_histogram()
})
output$summary <- renderPrint({
Muts <- mydata()
summary(Muts)
})
}
# Run the application
shinyApp(ui = ui, server = server)
只需确保调用 mydata()
以实际获取反应对象的值。
library(shiny)
library(ggplot2)
ui <- fluidPage(
numericInput("x", "x", 2), numericInput("y", "y", 2), numericInput("z", "z", 2),
plotOutput("distPlot"), plotOutput("distPlot2"))
server <- function(input, output) {
mydata <- reactive({
x <- input$x
y <- input$y
z <- input$z
data.frame(x=rpois(100,(x*y*z)))
})
output$distPlot <- renderPlot({
ggplot(mydata(), aes(x)) + geom_density()
})
output$distPlot2 <-renderPlot({
ggplot(mydata(), aes(x)) + geom_density()
})
}
runApp(list(ui=ui, server=server))
我写了一个闪亮的应用程序,我从泊松分布中抽样。每次我想输出数据图或摘要时,我都重写了函数。我宁愿只使用该函数一次,然后让所有图表和摘要都引用该函数被调用的那一次,否则输出的结果将不相同。当我尝试调用该函数一次并将结果存储为变量以供 ggplot 使用时,我收到错误消息,即 ggplot 无法使用反应数据,而且我无法将其强制转换为数据框。
我尝试了以下变体:
mydata <- reactive({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
Muts
})
但是它们不起作用,因为 ggplot 无法使用输入。我想我没有正确使用反应函数。非常感谢任何有助于减少代码冗余并确保图表和摘要都使用相同的单一基础数据的帮助。提前谢谢你。
我当前的代码:
server.R
library(shiny)
library(ggplot2)
# Define server logic required to draw a histogram
function(input, output) {
output$distPlot <- renderPlot({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
# draw the density plot
ggplot(Muts, aes(Muts)) + geom_density()
})
output$distPlot2 <-renderPlot({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
Muts <- as.data.frame(Muts)
ggplot(Muts, aes(Muts)) + geom_histogram()
})
output$summary <- renderPrint({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
summary(Muts)
})
}
你的想法是对的,但是 reactive
本质上是一个缓存函数,需要调用它来检索它的 return 值,所以 mydata()
是正确的,mydata
只是 return 函数本身(不是 return 值)。
library(shiny)
library(ggplot2)
# Define UI for application
ui <- fluidPage(
# Application title
titlePanel(""),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("x",
"Number of bins:",
min = 1,
max = 50,
value = 30),
sliderInput("y",
"Number of bins:",
min = 1,
max = 50,
value = 30),
sliderInput("z",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
plotOutput("distPlot2"),
verbatimTextOutput("summary")
)
)
)
# Define server logic
server <- function(input, output) {
mydata <- reactive({
x <- input$x
y <- input$y
z <- input$z
Muts <- as.data.frame(rpois(100,(x*y*z)))
Muts
})
output$distPlot <- renderPlot({
Muts <- mydata()
ggplot(Muts, aes(Muts)) + geom_density()
})
output$distPlot2 <-renderPlot({
Muts <- mydata()
ggplot(Muts, aes(Muts)) + geom_histogram()
})
output$summary <- renderPrint({
Muts <- mydata()
summary(Muts)
})
}
# Run the application
shinyApp(ui = ui, server = server)
只需确保调用 mydata()
以实际获取反应对象的值。
library(shiny)
library(ggplot2)
ui <- fluidPage(
numericInput("x", "x", 2), numericInput("y", "y", 2), numericInput("z", "z", 2),
plotOutput("distPlot"), plotOutput("distPlot2"))
server <- function(input, output) {
mydata <- reactive({
x <- input$x
y <- input$y
z <- input$z
data.frame(x=rpois(100,(x*y*z)))
})
output$distPlot <- renderPlot({
ggplot(mydata(), aes(x)) + geom_density()
})
output$distPlot2 <-renderPlot({
ggplot(mydata(), aes(x)) + geom_density()
})
}
runApp(list(ui=ui, server=server))