小编May*_*ans的帖子

使用map和pluck从嵌套列表中获取值

我有以下令人讨厌的嵌套列表

编辑:更新以包括value_I_dont_want

mylist <- list(
  list(
    nested_1 = list(
      nested_2 = list(
        list( value_I_want = "a", value_I_dont_want = "f"),
        list( value_I_want = "b", value_I_dont_want = "g")
      )
    )
  ),
  list(
    nested_1 = list(
      nested_2 = list(
        list( value_I_want = "c", value_I_dont_want = "h"),
        list( value_I_want = "d", value_I_dont_want = "i"),
        list( value_I_want = "e", value_I_dont_want = "j")
      )
    )
  )
)
Run Code Online (Sandbox Code Playgroud)

我想得到所有value_I_want

我知道我可以在 for 循环中使用以下代码

mylist[[x]]$nested_1$nested_2[[y]]$value_I_want
Run Code Online (Sandbox Code Playgroud)

但我想提高我的地图技能。map_chr我了解当列表是单个级别时如何使用,但我没有找到很多关于从非常嵌套的列表中提取的资源。我也知道我可以使用,[[但还没有找到合适的文档?

任何帮助表示赞赏!

r pluck purrr

6
推荐指数
1
解决办法
1827
查看次数

加入2个嵌套列表

我想合并两个列表

list_1 <- list(LIST1 = list(list("a"), list("b"), list("c")))
list_2 <- list(LIST2 = list(list("1"), list("2"), list("3")))
Run Code Online (Sandbox Code Playgroud)

所需输出:

combined_list <- list()
combined_list[[1]] <- c("a", "1")
combined_list[[2]] <- c("b", "2")
combined_list[[3]] <- c("c", "3")
Run Code Online (Sandbox Code Playgroud)

我有一个讨厌的for循环方式,但是我想使用purrr清理它吗?任何帮助表示赞赏!

r purrr tidyverse

5
推荐指数
1
解决办法
58
查看次数

使用操作按钮的闪亮 renderPlot

我正在尝试创建一个闪亮的应用程序,用户可以选择要绘制的 x 和 y 轴,然后单击“添加图形”以绘制图形 - 但此代码不起作用,我不确定为什么。我需要将input$x_iris和设置input$y_iris为反应式吗?

library(shiny)
library(ggplot2)

ui <- fluidPage(

  titlePanel("4 Plot Generator"),

  sidebarLayout(
    sidebarPanel(
      fluidRow(
        column(6, selectInput("x_iris", "X:", choices= c(unique(colnames(iris))))),
        column(6, selectInput("y_iris", "Y:", choices = c(unique(colnames(iris)))))
      ),

      # button to add graph
      # button to remove graph
      fluidRow(
        column(6, actionButton("add_graph", "Add Graph")),
        column(6, actionButton("reset_graph", "Reset Graphs"))
      )

    ),

    # Show graph
    mainPanel(
      plotOutput("plots")
    )
    )
  )

# Define server logic required to draw a histogram
server <- function(input, output) {

  observeEvent(input$add_graph,{
    # not …
Run Code Online (Sandbox Code Playgroud)

r shiny shiny-reactivity

5
推荐指数
1
解决办法
2429
查看次数

格式化闪亮的 Plotly 子图 - 单独的标题和图形大小

我试图为plotly'ssubplot函数中的每个图表提供单独的标题,我发现了一篇文章,您可以使用它来扩展子图,%>% layout(title = "Main Title)但我想为每个图表提供单独的标题(我正在使用,ggtitle但只绘制最后一个标题提供(图 4)。我发现了类似的帖子为每个子图提供标题 - R Shiny但我不认为我可以facet_wrap在我的场景中。

此外 - 我想知道如何增加子图中图表之间的边距,因为它们似乎真的被压在一起了。

任何帮助表示赞赏!

在此输入图像描述

ui <- fluidPage(
  sidebarPanel("This is a sidebar"),
  mainPanel(plotlyOutput("myplot"))
    )

server <- function(input, output){

  output$myplot <- renderPlotly({

    gg1 <- ggplotly(
      ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) + 
        geom_point() +
        theme_minimal() +
        ggtitle("Plot 1")
    )

    gg2 <- ggplotly(
      ggplot(iris, aes(x=Species, y=Sepal.Length)) +
        geom_boxplot() +
        theme_minimal() +
        ggtitle("Plot 2")
    )

    gg3 <- ggplotly(
      ggplot(iris, aes(x=Petal.Width)) +
        geom_histogram() +
        ggtitle("Plot 3")
    ) …
Run Code Online (Sandbox Code Playgroud)

r shiny plotly ggplotly

5
推荐指数
1
解决办法
3627
查看次数

右对齐 gt 中的 rowname_col

我想右对齐,rowname_col但看起来您不能应用于cols_align行名?

tibble(
  one = c("Long names", "Other Name", "Name | Name"),
  two = 1:3
) %>% gt(rowname_col = "one") %>%
  cols_align(align = "right", columns = vars(one))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

r gt

5
推荐指数
1
解决办法
1360
查看次数

包含特定列名的数据框子集列表

我有一个数据框列表,我想x从每个数据框中获取列作为字符串。

testing <- list(data.frame(A = "Yes", B = "No"),
                data.frame(B = "No", C = "No"),
                data.frame(A = "Yes"))
Run Code Online (Sandbox Code Playgroud)

我可以打印哪些数据帧中有colnameA,但我无法连接到子集原始测试

lapply(testing, function(x) "A" %in% colnames(x))
Run Code Online (Sandbox Code Playgroud)

期望输出

[[1]]
    A  B
1 Yes No

[[2]]
    A
1 Yes
Run Code Online (Sandbox Code Playgroud)

r list subset lapply purrr

4
推荐指数
2
解决办法
255
查看次数

重命名数据框列表中的所有列

我有一个数据框列表

vars <- c("Col_1", "Col_2", "Col_3")

list_df <- list(
  tibble(
    a = 1,
    b = 2,
    c = 3
  ),
  tibble (
    d = 1,
    e = 2,
    f = 3
  )
)
Run Code Online (Sandbox Code Playgroud)

我想使用 重命名列,vars但我想我在这里遗漏了一些东西。

map(list_df, ~rename_all(.x, vars))
Run Code Online (Sandbox Code Playgroud)

结果应该是:

list(
  tibble(
    Col_1 = 1,
    Col_2 = 2,
    Col_3 = 3
  ),
  tibble (
    Col_1 = 1,
    Col_2 = 2,
    Col_3 = 3
  )
)
Run Code Online (Sandbox Code Playgroud)

无论初始列名如何,我都希望它可以工作(并且所有数据框始终只有三列。这可能吗?

r list purrr

4
推荐指数
1
解决办法
64
查看次数

使用 map(v, reduce) 在 R 中创建斐波那契数列?

我想知道我们是否可以使用purrr'smapreduce来创建类似于 JavaScript 中的这个函数的斐波那契函数:

function fib(n){
  return new Array(n).fill(1).reduce((arr, _ ,i) => {
    arr.push((i <= 1) ? i : arr[i-2] + arr[i-1])
    return arr
  },[]) ;
}
console.log(fib(10))
Run Code Online (Sandbox Code Playgroud)

我在这里看到R中使用递归的斐波那契数列的答案,但我想知道我们是否可以专门使用 purrr reduce,如果可以,如何使用?

r purrr

3
推荐指数
1
解决办法
167
查看次数

数据框的嵌套列表 [使用 purrr + map]

我看过很多帖子,所以如果这是多余的,我很抱歉,但希望获得一些帮助来压平嵌套列表:

test <- list()
test <- c(
  list("A" = c(list("1"), list("2"), list("3"))), 
  list("B" = c(list("4"), list("5"), list("6")))
)
Run Code Online (Sandbox Code Playgroud)

所需输出

  name subcat
1    A      1
2    A      2
3    A      3
4    B      4
5    B      5
6    B      6
Run Code Online (Sandbox Code Playgroud)

我正在努力编写一个嵌套的 for 循环,但我真的很想使用 purrr 或更优雅的东西来创建一个包含两列的数据框:子目录列和列表中每个元素的名称的重复列。

感谢您的任何帮助,甚至只是向我指出类似的帖子 - 谢谢!

nested r list purrr

0
推荐指数
1
解决办法
2085
查看次数

标签 统计

r ×9

purrr ×6

list ×3

shiny ×2

ggplotly ×1

gt ×1

lapply ×1

nested ×1

plotly ×1

pluck ×1

shiny-reactivity ×1

subset ×1

tidyverse ×1