您没有提供可重现的示例,因此我使用了 mtcars 和 iris 数据集。您可以在全局环境中读取自己的数据集。为了示例,我使用renderPlot 函数向您展示它的工作原理。这里有两个建议:
选项 1:
library(shiny)
library(tidyr)
ui = pageWithSidebar(
headerPanel('testing'),
sidebarPanel(
selectInput('x', label = 'Choose the book', choices = c('book1_name', 'book2_name'), selected = 'book1_name')
),
mainPanel(
plotOutput('plot1')
)
)
server = function(input, output) {
output$plot1 <- renderPlot({
if (input$x == 'book1_name') {
data1 <- iris
data2 <- mtcars
rownames(data2) <- NULL
data1a <- data1[, 1:2]
data2a <- data2[, 1:2]
par(mfrow = c(1, 2))
plot(data1a) #
plot(data2a) #
}
if (input$x == 'book2_name') {
data1 <- mtcars
data2 <- iris
# rownames(data2) <- NULL
data1a <- data1[, 1:2]
data2a <- data2[, 1:2]
par(mfrow = c(1, 2))
plot(data1a) #
plot(data2a) #
}
})
}
shinyApp(ui = ui, server = server)
使用选项 1,您可以在渲染函数中选择数据集(可能是另一个计算)。
选项 2:
library(shiny)
ui = pageWithSidebar(
headerPanel('testing'),
sidebarPanel(
selectInput('x', label = 'Choose the book', choices = c('book1_name', 'book2_name'), selected = 'book1_name')
),
mainPanel(
plotOutput('plot1')
)
)
server = function(input, output) {
data1 <- reactive({
if (input$x == 'book1_name') {
data1 <- iris
} else {
data1 <- mtcars
}
})
data2 <- reactive({
if (input$x == 'book1_name') {
data2 <- mtcars
} else {
data1 <- iris
}
})
output$plot1 <- renderPlot({
data1 <- data1()
data2 <- data2()
data1a <- data1[, 1:2]
data2a <- data2[, 1:2]
par(mfrow = c(1, 2))
plot(data1a) #
plot(data2a) #
})
}
shinyApp(ui = ui, server = server)
使用选项 2,您可以选择渲染函数之外的数据集。
希望对你有帮助!