【0008day】Shiny的介绍

介绍:Shiny 是一个开源 R 包,它提供了一个优雅而强大的 Web 框架,用于使用 R 构建 Web 应用程序。Shiny 可以帮助您将分析转变为交互式 Web 应用程序,而无需 HTML、CSS 或 JavaScript 知识。

# download R package
pkgtest <- function(x){
    if(x %in% rownames(installed.packages()) == FALSE){
        install.packages(x,dependencies = TRUE)
    }
    library(x,character.only = TRUE)
    
}
neededpackages <- c("shiny","dplyr","plotly","readxl","DT","ggExtra")
for(package in neededpackages){pkgtest(package)}



library(shiny)
library(bslib)
library(dplyr)
library(ggplot2)
library(ggExtra)




penguins_csv <- "E:\\BaiduSyncdisk\\A-Area longtime\\R language\\Niche\\temp_data.csv"

df <- readr::read_csv(penguins_csv)
# Find subset of columns that are suitable for scatter plot
df_num <- df |> select(where(is.numeric), -Year)

ui <- page_sidebar(
    sidebar = sidebar(
        varSelectInput("xvar", "X variable", df_num, selected = "Bill Length (mm)"),
        varSelectInput("yvar", "Y variable", df_num, selected = "Bill Depth (mm)"),
        checkboxGroupInput(
            "species", "Filter by species",
            choices = unique(df$Species), 
            selected = unique(df$Species)
        ),
        hr(), # Add a horizontal rule
        checkboxInput("by_species", "Show species", TRUE),
        checkboxInput("show_margins", "Show marginal plots", TRUE),
        checkboxInput("smooth", "Add smoother"),
    ),
    plotOutput("scatter")
)

server <- function(input, output, session) {
    subsetted <- reactive({
        req(input$species)
        df |> filter(Species %in% input$species)
    })
    
    output$scatter <- renderPlot({
        p <- ggplot(subsetted(), aes(!!input$xvar, !!input$yvar)) + list(
            theme(legend.position = "bottom"),
            if (input$by_species) aes(color = Species),
            geom_point(),
            if (input$smooth) geom_smooth()
        )
        
        if (input$show_margins) {
            margin_type <- if (input$by_species) "density" else "histogram"
            p <- ggExtra::ggMarginal(p, type = margin_type, margins = "both",
                                     size = 8, groupColour = input$by_species, groupFill = input$by_species)
        }
        
        p
    }, res = 100)
}

shinyApp(ui, server)

所需要的资料下载:https://download.csdn.net/download/qq_45697428/89440340

运行的结果:

在这里插入图片描述
在这个框架下可以调整XY轴的内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Q一件事

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值