首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用shinyTree包查看R中的树视图

使用shinyTree包查看R中的树视图
EN

Stack Overflow用户
提问于 2016-11-03 13:44:15
回答 1查看 3.9K关注 0票数 2

我正在创建树视图在R闪亮使用shinyTree包,能够做同样的。用于服务器部分的代码具有列表创建功能。现在,更多的要求是将数据转换为列表并导入相同的列表,以便使用renderTree实现树结构。

下面是我写的代码:

代码语言:javascript
复制
#ui part
library(shiny)
library(shinyTree) # Using shinyTree Package

# UI for application that demonstrates a Tree View

shinyUI(
  pageWithSidebar(
    # Application title
    headerPanel("Tree View in Shiny"),

    sidebarPanel(
      helpText(HTML("A simple Shiny Tree example.
                  <hr>Created using shinyTree Package."))
    ),
    mainPanel(
      # Show a simple table.
      shinyTree("tree")
    )
  ))
#--------------------------------------------------------------------

#server part

library(shiny)
library(shinyTree)

# server logic required to generate a tree structure

shinyServer(function(input, output, session) {
  output$tree <- renderTree({
    **list(
      Folder1 = list(File1.1 = structure("",sticon="file"), File1.2 = structure("",sticon="file")),
      Folder2 = list(
        Subfolder2.1 = list(File2.1.1 = structure("",sticon="file"), File2.1.2 = structure("",sticon="file")
                            , File2.1.3=structure("",sticon="file")),
        Subfolder2.2 = list(File2.2.1 = structure("",sticon="file"), File2.2.2 = structure("",sticon="file")),
        File2.3 = structure("",sticon="file")
      )**
    )
  })
})

代码中的star部分需要替换为list (已经使用dataframe进行了转换)。我怎样才能实现同样的目标。

EN

回答 1

Stack Overflow用户

发布于 2017-12-09 12:28:47

这里使用递归方法cab从数据中获取树结构。

解决方案:

将数据转换为树的代码(列表)

代码语言:javascript
复制
a<-read.csv("Tree.csv") # Importing the dataframe

# Recursion function to get tree structure
gettree<-function(a)
{

# defining tree as list
tree<-list()  

# Condition to identifly if selected column is not last column
if(class(a)!="factor"&&length(a)>1)
{

# getting uniques list of names from the columns to create folder 
b<-unique(a[,1])  

# runnig file name in loop
for(i in b)
{
# check if the flie name i not blank
if(i!="")
{ 
# subset data for content of folder  
subdata<-a[which(a[,1]==i),] 

# if there is only one element for that item change icon as file 
if(length(subdata[,-1])==1)
{tree[[i]]=structure("",sticon="file")}
else{
# call the function recursively  
tree[[i]]<-gettree(subdata[,-1]) 
  }}

}}

# Change icon of last columns as file 
if(class(a)=="factor"||length(a)==1)
{
for(i in a)
{      
  tree[[i]]=structure("",sticon="file")
}

}
return(tree)
}

ui.R

代码语言:javascript
复制
library(shiny)
library(shinyTree) # Using shinyTree Package

# UI for application that demonstrates a Tree View

shinyUI(
pageWithSidebar(
# Application title
headerPanel("Tree View in Shiny"),

sidebarPanel(
  helpText(HTML("A simple Shiny Tree example.
              <hr>Created using shinyTree Package."))
),
mainPanel(

  shinyTree("tree")
)
))

Server.R

代码语言:javascript
复制
# server logic required to generate a tree structure

shinyServer(function(input, output, session) {
a<-read.csv("Tree.csv")

# call the function get tree and pass the data frame
tree<-gettree(a)

# render the list in tree
output$tree <- renderTree({
tree
  })
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40403093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档