我正在创建一个shiny应用程序,用户可以在其中选择要查看的plot。我使用this问题作为一种方法,但我得到了一个错误。我怎么才能解决这个问题?
用户界面
library(shiny)
library(shinydashboard)
library(shinythemes)
library(tidyverse)
ui = navbarPage("Project ", theme = shinytheme("cyborg")
uiOutput("all"),
tabPanel("Plot",
icon = icon("chart-area"),
sidebarLayout(sidebarPanel(
selectInput("Plot", "Please select a plot to view:",
choices = c("Plot-1", "Plot-2")),
submitButton("Submit")),
plotOutput(outputId = "Plots",
width = "1024px",
height = "768px")
)))服务器
server = function(input, output, session) {
data = eventReactive(input$Plot,{
switch(input$Plot,
"Plot-1" = Plot1,
"Plot-1" = Plot2)
})
output$Plots = renderPlot({
data("midwest", package = "ggplot2") # Sample data
Plot1 = ggplot(midwest, aes(x=area, y=poptotal)) +
geom_point()
Plot2 = ggplot(midwest, aes(x=area, y=poptotal)) + geom_point() +
geom_smooth(method="lm")
})
}
# Run the application
shinyApp(ui = ui, server = server)错误

发布于 2021-10-02 06:50:01
有个办法-
library(shiny)
library(shinydashboard)
library(shinythemes)
ui = navbarPage("Project ", theme = shinytheme("cyborg"),
uiOutput("all"),
tabPanel("Plot",
icon = icon("chart-area"),
sidebarLayout(sidebarPanel(
selectInput("Plot", "Please select a plot to view:",
choices = c("Plot-1", "Plot-2")),
actionButton("submit", "Submit")),
plotOutput(outputId = "Plots",
width = "1024px",
height = "768px")
)))
server = function(input, output, session) {
observeEvent(input$submit,{
Plot1 = ggplot(midwest, aes(x=area, y=poptotal)) +
geom_point()
Plot2 = ggplot(midwest, aes(x=area, y=poptotal)) + geom_point() +
geom_smooth(method="lm")
output$Plots = renderPlot({
switch(isolate(input$Plot),
"Plot-1" = Plot1,
"Plot-2" = Plot2)
})
})
}
# Run the application
shinyApp(ui = ui, server = server)https://stackoverflow.com/questions/69414472
复制相似问题