基于R Shining dashboard中的单选按钮进行打印
这是我的代码:基于R Shining dashboard中的单选按钮进行打印,r,shiny,R,Shiny,这是我的代码: ui <- fluidPage( radioButtons("dist", "Distribution type:", c("Sepal.Length" = "Length", "Sepal.Width" = "Width", "Petal.Length" = "Length")), plotOutput("distPlot") ) server <- func
ui <- fluidPage(
radioButtons("dist", "Distribution type:",
c("Sepal.Length" = "Length",
"Sepal.Width" = "Width",
"Petal.Length" = "Length")),
plotOutput("distPlot")
)
server <- function(input, output) {
output$distPlot <- renderPlot({
dist <- switch(input$dist,
"Length" = plot(iris$Sepal.Length),
"Width" = plot(iris$Sepal.Width),
"Length" = plot(iris$Sepal.Length))
plot(dist)
})
}
shinyApp(ui, server)
}
ui您不需要绘制指定的函数,因为switch
it0已经在这样做了
server <- function(input, output) {
output$distPlot <- renderPlot({
switch(input$dist,
"Length" = plot(iris$Sepal.Length),
"Width" = plot(iris$Sepal.Width),
"Length" = plot(iris$Sepal.Length))
})
}
如何使用par(mfrow=c(1,2))
为每个按钮绘制两个图?我不知道您想要实现什么。?每个按钮有两个绘图?但您只在“服务器”上定义了一个绘图。要为选项创建两个绘图,我可以想到两个备选方案:1)在UI中添加另一个“plotOutput”,在服务器中呈现另一个绘图。2) 使用“ggplot”和“facet”根据变量进行复制。谢谢@David Jorquera,我将尝试使用ggplot。
server <- function(input, output) {
output$distPlot <- renderPlot({
par(mfrow=c(1,2))
switch(input$dist,
"Length" = plot(iris$Sepal.Length),
"Width" = plot(iris$Sepal.Width),
"Length" = plot(iris$Sepal.Length))
switch(input$dist,
"Length" = plot(iris$Sepal.Length),
"Width" = plot(iris$Sepal.Width),
"Length" = plot(iris$Sepal.Length))
})