r语言绘制动态交互式表格
时间: 2023-07-18 12:37:03 浏览: 57
要绘制动态交互式表格,可以使用R语言中的一些包,比如shiny、plotly、DT等。下面给出一个简单的例子:
首先需要安装和加载相关的包:
```R
install.packages(c("shiny", "plotly", "DT"))
library(shiny)
library(plotly)
library(DT)
```
然后创建一个shiny应用:
```R
ui <- fluidPage(
titlePanel("动态交互式表格"),
sidebarLayout(
sidebarPanel(
selectInput("dataset", "选择数据集:", choices = c("iris", "mtcars"))
),
mainPanel(
tabsetPanel(
tabPanel("表格", DTOutput("table")),
tabPanel("散点图", plotlyOutput("scatterplot"))
)
)
)
)
server <- function(input, output) {
# 读取数据集
data <- reactive({
if (input$dataset == "iris") {
iris
} else {
mtcars
}
})
# 生成表格
output$table <- renderDT({
datatable(data())
})
# 生成散点图
output$scatterplot <- renderPlotly({
plot_ly(data(), x = ~wt, y = ~mpg, color = ~cyl, size = ~hp)
})
}
shinyApp(ui, server)
```
这个应用包含一个侧边栏,可以选择数据集,然后在主面板中显示表格和散点图。当选择不同的数据集时,表格和散点图会自动更新。
可以将这个应用保存为一个R文件,然后在RStudio中运行,或者使用`runApp()`函数在R控制台中运行。