R:修改 R Markdown 教程

4

我正在使用 R 编程语言。

我之前已经制作了以下 8 张图,并将它们保存为 HTML 文件放在我的工作目录中:

library(plotly)

Red_A <- data.frame(var1 = rnorm(100,100,100), var2 = rnorm(100,100,100)) %>%
  plot_ly(x = ~var1, y = ~var2, marker = list(color = "red")) %>% 
  layout(title = 'Red A')

Red_B <- data.frame(var1 = rnorm(100,100,100), var2 = rnorm(100,100,100)) %>%
  plot_ly(x = ~var1, y = ~var2, marker = list(color = "red")) %>% 
  layout(title = 'Red B')

Blue_A <- data.frame(var1 = rnorm(100,100,100), var2 = rnorm(100,100,100)) %>%
  plot_ly(x = ~var1, y = ~var2, marker = list(color = "blue")) %>% 
  layout(title = 'Blue A')

Blue_B <- data.frame(var1 = rnorm(100,100,100), var2 = rnorm(100,100,100)) %>%
  plot_ly(x = ~var1, y = ~var2, marker = list(color = "red")) %>% 
  layout(title = 'Blue B')

htmlwidgets::saveWidget(as_widget(Red_A), "Red_A.html")
htmlwidgets::saveWidget(as_widget(Red_B), "Red_B.html")
htmlwidgets::saveWidget(as_widget(Blue_A), "Blue_A.html")
htmlwidgets::saveWidget(as_widget(Blue_B), "Blue_B.html")

我的问题:使用这个模板(https://testing-apps.shinyapps.io/flexdashboard-shiny-biclust/),我想制作一个flexdashboard,允许用户从两个下拉菜单中选择“颜色”和“字母”,然后呈现相应的图形之一(例如col = Red&letter = B ->“Red B”)。然后,我希望能够将最终产品本身保存为HTML文件。这将看起来像这样:

enter image description here

我尝试通过调整教程来编写适用于此问题的Rmarkdown代码:

---
title: "Plotly Graph Selector"
output: 
  flexdashboard::flex_dashboard:
  orientation: columns
vertical_layout: fill
runtime: shiny
---
  
  
Inputs {.sidebar}

 selectInput("Letter", label = h3("Letter"), 
    choices = list("A" = 1, "B" = 2), 
    selected = 1)

 selectInput("Color", label = h3("Color"), 
    choices = list("Red" = 1, "Blue" = 2), 
    selected = 1)

我该如何继续进行?

注意

我知道可以将预先制作的HTML文件加载到仪表板中,例如:

# https://dev59.com/MMT5oIgBc1ULPQZFw3s6
<object class="one" type="text/html" data="Red_A.html"></object>
<object class="one" type="text/html" data="Red_B.html"></object>
<object class="one" type="text/html" data="Blue_A.html"></object>
<object class="one" type="text/html" data="Blue_B.html"></object>
1个回答

1
你可以使用iframerenderUI来使用addResourcePath本地渲染HTML文件。使用paste0paste动态创建HTML文件以选择它们。以下是一些可重现的代码:
---
title: "Plotly Graph Selector"
output: 
  flexdashboard::flex_dashboard:
  orientation: columns
  vertical_layout: fill
  self_contained: false
runtime: shiny
---
  
```{r global, include=FALSE}
```

Inputs {.sidebar}
-----------------------------------------------------------------------

```{r}
 selectInput("Letter", label = h3("Letter"), 
    choices = c("A", "B"), 
    selected = "A")

 selectInput("Color", label = h3("Color"), 
    choices = c("Red", "Blue"), 
    selected = "Red")
```

Row 
-----------------------------------------------------------------------

```{r}
addResourcePath("Downloads", "~/Downloads")
renderUI({
  color <- input$Color
  letter <- input$Letter
  tags$iframe(
    seamless="seamless",
    src=paste0("Downloads/", paste0(paste(color, letter, sep = "_"), ".html")),
    width = 600,
    height = 400)
})
```

输出:

enter image description here


如果您想要删除图表周围的边框,您可以在iframe调用中添加frameBorder = "0",像这样:
```{r}
addResourcePath("Downloads", "~/Downloads")
renderUI({
  color <- input$Color
  letter <- input$Letter
  tags$iframe(
    seamless="seamless",
    src=paste0("Downloads/", paste0(paste(color, letter, sep = "_"), ".html")),
    width = 600,
    height = 400,
    frameBorder = "0")
})
```

输出:

enter image description here


使用类似于 basenamegetwd()

```{r}
addResourcePath(basename(getwd()), getwd())
renderUI({
  color <- input$Color
  letter <- input$Letter
  tags$iframe(
    seamless="seamless",
    src=paste0(basename(getwd()), "/", paste0(paste(color, letter, sep = "_"), ".html")),
    width = 600,
    height = 400,
    frameBorder = "0")
})
```

1
嗨 @stats_noob,你是指用 getwd 替换 addResourcepath 中的 "~/Downloads" 吗? - Quinten
@ Quinten:举个例子,假设我的图表当前保存在这里:“C:/Users/me/OneDrive/Documents”。 - stats_noob
感谢您的回复!代码运行了 - 但我得到了以下空输出:https://imgur.com/a/j0sB2Ri - stats_noob
1
我觉得我修好了!我改成了:addResourcePath("Documents", getwd()) - stats_noob
啊,我明白了,忘记改变addResourcePath的第一个参数了。请再次检查编辑。 - Quinten
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接