我对R Markdown和Shiny很新,我正在尝试制作一个简单的交互式html文档。我有两个共享相同行名(公司名称)的数据框,其余的是基于不同度量的百分比(每行加1)。我想要的是当人们选择一家公司时,会出现两个条形图。我可以创建一个下拉列,但不知道如何添加两个图表。以下是我的代码:
---
title: "Test"
author: "Z"
date: "March 30, 2018"
output: html_document
runtime: shiny
---
```{r}
Names = c('A','B','C')
Rev = c(-0.2,0.3,0.4)
Mar = c(0.5,0.2,0.1)
Mul = c(0.5,0.2,0.4)
Lev = c(0.2,0.3,0.1)
df1 = data.frame(Names,Rev,Mar,Mul,Lev)
Sal = c(0.4,0.2,0.4)
Ear = c(0.1,0.2,0.3)
Pri = c(0.3,0.1,0.1)
Dev = c(0.2,0.5,0.2)
df2 = data.frame(Names,Sal,Ear,Pri,Dev)
selectInput(inputId = 'Company', label = 'Choose a company',
choices = Names)
```
答案 0 :(得分:1)
这是一种方法:
renderPlot({ ## wrap the plot prep and plot function in a reactive space
data_to_plot <- unlist(df1[df1$Names == input$Company, 2: ncol(df1)]) # get data
barplot(data_to_plot) # call the plot function
})
这是有效的,因为有光泽只允许输入影响反应空间中的事物(大括号{}
)。此外,可以使用input
对象调用闪亮的输入。