基于blogdown搭建的第一篇文章

概述

基于blogdown搭建的github个人网站,主要是相关内容的测试。

R Code

function

summary(cars)
     speed           dist       
 Min.   : 4.0   Min.   :  2.00  
 1st Qu.:12.0   1st Qu.: 26.00  
 Median :15.0   Median : 36.00  
 Mean   :15.4   Mean   : 42.98  
 3rd Qu.:19.0   3rd Qu.: 56.00  
 Max.   :25.0   Max.   :120.00  

plot

plot(pressure)

ggplot

library(ggplot2)
gg <- ggplot(mtcars, aes(x=mpg)) + geom_histogram(binwidth=1)
gg

htmlwidgets

blogdown/html-widgets

We do not recommend that you use different HTML widgets from many R packages on the same page, because it is likely to cause conflicts in JavaScript. For example, if your theme uses the jQuery library, it may conflict with the jQuery library used by a certain HTML widget. In this case, you can conditionally load the theme’s jQuery library by setting a parameter in the YAML metadata of your post and revising the Hugo template that loads jQuery.

通过widgetframe::frameWidget()来导入htmlwidgets。

highcharter

library(highcharter)
hc <- hchart(mpg, "scatter", hcaes(x = displ, y = hwy, group = class))
widgetframe::frameWidget(hc, height = 350, width = '100%')

echarter

library(echarter)
ec <- echarter(mpg, type = "scatter", mapping = ecaes(x = displ, y = hwy, group = class))
widgetframe::frameWidget(ec, height = 350, width = '100%')

plotly

library(plotly, quietly = TRUE, warn.conflicts = FALSE)
gg_plotly <- ggplotly(gg, session="knitr")
widgetframe::frameWidget(gg_plotly, height = 350, width = '100%')

leaflet

library(htmlwidgets)
library(leaflet)
leaflet_map <- leaflet(width = "100%") %>% addTiles() %>% setView(lng = 108, lat = 36, zoom = 4)
widgetframe::frameWidget(leaflet_map, height = 350, width = '100%')

DT

library(DT)
table_DT <- DT::datatable(
  mtcars, options = list(pageLength = 10), selection = "single",
  rownames=FALSE, colnames = names(mtcars))
widgetframe::frameWidget(table_DT, height = 350, width = '100%')

kable

library(knitr)
#kable(head(iris), format = "html")
kable(head(iris))
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

shiny

library(shiny)

ui <- shinyUI(pageWithSidebar(
  headerPanel('Iris k-means clustering'),
  sidebarPanel(
    selectInput('xcol', 'X Variable', names(iris)),
    selectInput('ycol', 'Y Variable', names(iris),
                selected=names(iris)[[2]]),
    numericInput('clusters', 'Cluster count', 3,
                 min = 1, max = 9)
  ),
  mainPanel(
    plotOutput('plot1')
  )
))

palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
          "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))


server <- shinyServer(function(input, output, session) {
  
  # Combine the selected variables into a new data frame
  selectedData <- reactive({
    iris[, c(input$xcol, input$ycol)]
  })
  
  clusters <- reactive({
    kmeans(selectedData(), input$clusters)
  })
  
  output$plot1 <- renderPlot({
    par(mar = c(5.1, 4.1, 0, 1))
    plot(selectedData(),
         col = clusters()$cluster,
         pch = 20, cex = 3)
    points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
  })
  
})

shinyApp(ui = ui, server = server)
Avatar
Jeevan Yue
Data Analyst