我创建了一个闪亮的代码,用动作按钮绘制图形。
由于我同时有R绘图和绘图,我想在同一个面板中绘制它们,并点击它们的按钮。这是一个简单的例子:
UI:
library(shiny)
shinyUI(
fluidPage(
sidebarLayout(
sidebarPanel(
hr(),
actionButton("button1", label = "Button 1",
style='padding:4px; font-size:80%'),
hr(),
actionButton("button2", label = "Button 2",
style='padding:4px; font-size:80%'),
width = 2
),
mainPanel(
wellPanel(
conditionalPanel(
condition = "paneltype == 'A'",
plotOutput("plot",height = "1820px",width = "1250px")
),
conditionalPanel(
condition = "paneltype == 'B'",
plotlyOutput("plot",height = "auto")
)
)
)
)
)
)
服务器:
paneltype = 'A'
shinyServer(function(input, output) {
observeEvent(input$button1, {
output$plot <- renderPlot({
plot(1:10)
},width = 1400)
paneltype <<- 'A'
})
observeEvent(input$button2, {
output$plot <- renderPlotly({
plot_ly(as.data.frame(1:10))
})
paneltype <<- 'B'
})
})
编辑:
library(shiny)
library(plotly)
ui=shinyUI(
fluidPage(
sidebarLayout(
sidebarPanel(
hr(),
actionButton("button1", label = "Button 1",
style='padding:4px; font-size:80%'),
hr(),
actionButton("button2", label = "Button 2",
style='padding:4px; font-size:80%'),
width = 2
),
mainPanel(
wellPanel(
uiOutput("plots")
)
)
)
)
)
server=shinyServer(function(input, output) {
observeEvent(input$button1, {
output$plots=renderUI({plotOutput("plot")})
output$plot <- renderPlot({
layout(matrix(c(1,2,3),3,1,byrow=TRUE))
plot(1:10)
plot(10:1)
plot(20:40)
},width = 1400)
})
observeEvent(input$button2, {
output$plots=renderUI({plotlyOutput("plotly")})
output$plotly <- renderPlotly({
plot_ly(as.data.frame(1:10))
})
})
})
shinyApp(ui,server)
答案 0 :(得分:1)
conditionalPanel在客户端(js)上工作,因此它看不到你的变量。
您可以使用renderUI
library(shiny)
library(plotly)
ui=shinyUI(
fluidPage(
sidebarLayout(
sidebarPanel(
hr(),
actionButton("button1", label = "Button 1",
style='padding:4px; font-size:80%'),
hr(),
actionButton("button2", label = "Button 2",
style='padding:4px; font-size:80%'),
width = 2
),
mainPanel(
wellPanel(
uiOutput("plots")
)
)
)
)
)
server=shinyServer(function(input, output) {
observeEvent(input$button1, {
output$plots=renderUI({plotOutput("plot")})
output$plot <- renderPlot({
plot(1:10)
},width = 1400)
})
observeEvent(input$button2, {
output$plots=renderUI({plotlyOutput("plotly")})
output$plotly <- renderPlotly({
plot_ly(as.data.frame(1:10))
})
})
})
shinyApp(ui,server)
如果你想要一个情节,你可以这样做
1)定义绘图功能
plot_f=function(){
graphics::layout(matrix(c(1,2,3),3,1,byrow=TRUE))
plot(1:10)
plot(10:1)
plot(20:40)
}
2)renderPlot
output$plot <- renderPlot({
plot_f()
},width = 1400)