我有一个名为p1的列表。
p1
$ColA
Var1 Freq
1 asgfg 2.1
2 dds 2.1
3 dfg 4.3
4 dfh 2.1
$ColB
Var1 Freq
1 A 44.7
2 B 55.3
但是当我尝试使用下面的代码进行子集设置时,出现错误。我知道我们可以使用p1 [[“ ColB”]] [“ Var1”]]。但是下面的代码对于R Shiny进行过滤很有用。知道为什么下面的代码不合适吗?
p1[[p1[["ColB"]]["Var1"]]]
R Flexdashboard中的示例代码
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: scroll
runtime: shiny
source_code: embed
theme: cosmo
---
```{r setup, include=FALSE}
library(flexdashboard)
library(magrittr)
library(gridExtra)
library(ggplot2)
library(cowplot)
library(tidyverse)
library(dplyr)
library(tidyr)
library(shinythemes)
```
```{r}
df <- structure(list(ColA = c("gf", "dfg", "er", "gfs", "fdg", "sdf",
"er", "dgh", "dfg", "sfdg", "jyfj", "asgfg", "jgh", "ghjhg",
"ghj", "gjgj", "dgrert", "tyew", "ewt", "tyu", "hgj", "hjghj",
"dsgdg", "yt", "ryuy", "tyutyu", "uiuy", "yoiy", "ret", "e",
"dsgdfg", "hgdhg", "gfdg", "dghgd", "hdsger", "gdfgd", "gt",
"fdgd", "sgdf", "gdfsh", "sdfh", "dfh", "dgsh", "fg", "dds",
"gh", "rth"), ColB = c("A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"
), ColC = 1:47, ColD = 2:48), class = "data.frame", row.names = c(NA,
-47L))
df <- as.data.frame(df)
df <- as.data.frame(unclass(df))
# percentage of count for categorical univariate ------------------------
p1 <- list()
p <- list()
bs <- names(Filter(is.factor, df))
for(i in bs)
{
p1[[i]] <- as.data.frame(round(prop.table(table(df[,i]))*100,1))
do.call(rbind,p1) %>% as.data.frame()
}
Summary
=================
Inputs {.sidebar} {data-width=140}
-----------------------------------------------------------------------
```{r}
h6(selectInput("Plot1","First Variable",choices = c("",names(df)),width = 200))
output$filter_2 <- renderUI({
if (input$Plot1 == "") {
return()
} else {
h6(selectInput("b", label = "Levels", choices = unique(df[,input$Plot1]),multiple = TRUE,width = 200))
}
})
uiOutput("filter_2")
h6(selectInput("Plot2","Second Variable",choices = c("",names(df)),width = 200))
height <- reactive({
if (input$Plot1 != "") {
length(unique(df[,input$Plot1]))*60
} else {"NULL"}
})
```
Column {data-width=350}
-----------------------------------------------------------------------
### Univariate analysis
```{r}
plotOutput("Plot2")
s_data <- reactive({
p1[[input$Plot1]][["Var1"]] %in% input$b
})
Plot2 <- reactive(
for(i in bs)
{if (input$Plot1 == i) {
p[[i]] <- print(ggplot(data=s_data()[[i]],aes_string(x=reorder(s_data()[[i]]["Var1"],+s_data()[[i]]["Freq"]),y=s_data()[[i]]["Freq"],fill=s_data()[[i]]["Freq"]))+geom_bar(stat = "identity")+ylab("Percentage")+xlab(i)+theme(axis.text.x=element_text(angle=90, hjust=1,size = 12))+geom_text(aes_string(label = s_data()[[i]]["Freq"]),size=4,vjust = 1,hjust =1, colour="white")+coord_flip())
} else if (input$Plot1 == "NULL") {
""
}})
output$Plot2 <- renderPlot(
{
Plot2()
}, height = function() height()
)
```
希望这更有意义:)我正在尝试所有可能性,但没有得到