在R shiny markdown中渲染3d图

vhipe2zx  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(118)

我有一个闪亮的markdown文件的运行代码。我正在尝试的情节是一个3D情节被渲染为一个闪亮的markdown文档,而不是作为一个应用程序。
代码工作,但三维图弹出一个新的窗口,并没有呈现在html。
这里有一个可重复的例子。

---
title: "test"
date: "April 16, 2019"
output: html_document
runtime: shiny
---
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(fig.pos = 'H')
library(kableExtra)
library(rgl) # plot3d
library(knitr)
library(shiny)
knit_hooks$set(webgl = hook_webgl, rgl = hook_rgl)
inputPanel(selectInput('x', label = 'x axis:', choices = colnames(mtcars)[1:7], selected = 'mpg'), 
           selectInput('y', label = 'y axis:', choices = colnames(mtcars)[1:7], selected = 'cyl'),
           selectInput('z', label = 'z axis:', choices = colnames(mtcars)[1:7], selected = 'hp'))

renderPlot({
  plot3d(mtcars[, input$x], mtcars[,input$y], mtcars[,input$z],
         size = 4, 
         xlab = paste('Feat. ', input$x, sep = ''), 
         ylab = paste('Feat. ', input$y, sep = ''),
         zlab = paste('Feat. ', input$z, sep = ''), 
         type = 'p', col = rainbow(3)
                      )
  rglwidget()

})

输出

有什么线索我在这里错过了什么?
非常感谢。

mhd8tkvw

mhd8tkvw1#

应该可以用rgl实现。或者,您可以使用r3js

---
title: "Untitled"
author: "Stéphane Laurent"
date: "2023-10-09"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(r3js)
inputPanel(
  selectInput(
    'x', label = 'x axis:', choices = colnames(mtcars)[1:7], selected = 'mpg'
  ), 
  selectInput(
    'y', label = 'y axis:', choices = colnames(mtcars)[1:7], selected = 'cyl'
  ),
  selectInput(
    'z', label = 'z axis:', choices = colnames(mtcars)[1:7], selected = 'hp')
)
p <- reactive({
  plot3js(
    x = mtcars[, input$x], y = mtcars[,input$y], z = mtcars[,input$z],
    col = rainbow(3),
    xlab = input$x,
    ylab = input$y,
    zlab = input$z
  )})
renderR3js(r3js(p(), zoom = 3))

![](https://i.stack.imgur.com/QSQKx.gif)

查看文档的选项。

![](https://i.stack.imgur.com/XzX5S.png)

相关问题