【问题标题】:How do I use the NULL Value as a variable call in R Shiny如何在 R Shiny 中使用 NULL 值作为变量调用
【发布时间】:2018-11-12 22:40:26
【问题描述】:

如何在 RSHINY 中将 NULL 作为变量值传递?

在 phyloseq 中,有一个 plot 叫做 plot_net。 最基本的 plot_net 绘图代码如下所示:

data(enterotype)
#Eliminate samples with no entereotype denomination
enterotype = subset_samples(enterotype, !is.na(Enterotype))

plot_net(enterotype, maxdist = 0.1, point_label = NULL)

我正在尝试创建一个允许用户更改此图形的 RShiny 应用程序。

point_label 有几个不同的选项(例如:“SecTech”、“SampleID”、NULL)。

我已经有了这个标签的所有其他值,我只是不确定如何添加 NULL。

这是我所做的:

这可能无法运行,因为它不在闪亮的应用程序中,但我将其作为示例来说明问题。

library(shiny)
library(phyloseq)

# Data: This data contains info about nodes and edges on Phyloseq data.
data(enterotype)
#Eliminate samples with no entereotype denomination. Make it a lesson to 
always catalogue data correctly from the start. 
enterotype = subset_samples(enterotype, !is.na(Enterotype))
       
# a is the collection of variable names for point_label
    
a <- sample_variables(enterotype)
    
theme_set(theme_bw())

# Define UI for application that draws a network plot
shinyUI(fluidPage(
  
  # Application title
  titlePanel("Network Plots"),
  
   
  sidebarLayout(
    sidebarPanel(
      
       
      selectInput("labelBy",
                  "Select the point label category",
                  ***choices = c(a, "NA" = NULL),***
                  selected = "NA")
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
       plotOutput("netPlot")#,
       #plotOutput("networkPlot")
    )
  )
))

shinyServer(function(input, output) {
   
  output$netPlot <- renderPlot({
    
    plot_net(enterotype, maxdist = .1, point_label = input$labelBy)
    
  })
})
shinyApp(ui = ui, server = server)

这一行是我的问题:

选择 = c(a, "NA" = NULL)

如何将 NULL 添加到我的选择列表中。无论我如何尝试,NULL 始终被视为零值,它不会作为选项出现。

如果我将 NULL 写为“NULL”,phyloseq 函数 plot_net 不会接受它。 只取值 point_label = NULL 表示没有值。

我认为可以创建一个 if...else 循环,如果用户在 checkboxInput 上单击 NULL,则绘图将由第二行代码生成,指定 point_label 中的值为 NULL,但是如果有多个变量可能具有 NULL 值,那会非常麻烦。

可能有一些明显的技巧,比如在 NULL 值前面放置一个 $ 或 %,但我找不到它。如果有人可以提供帮助,那就太好了!

【问题讨论】:

    标签: r shiny null phyloseq


    【解决方案1】:

    我认为没有办法在selectInput 中使用NULL。这是您几乎解决的替代方案 - 在selectInput 中使用"None"(或任何其他替换值)并在绘图时使用NULL 切换它。这样你就不用写多个if...else了。

    # update on UI side
    selectInput("labelBy",
                "Select the point label category",
                choices = c("None", a),
                selected = "None")
    
    # update on server side
    output$netPlot <- renderPlot({
      point_labels <- switch(input$labelBy, "None" = NULL, input$labelBy)
      plot_net(enterotype, maxdist = .1, point_label = point_labels)
    })
    

    【讨论】:

    • 这似乎并不能真正解决我的问题。即使没有在服务器中编码,也没有真正的方法可以在用户端更改它(除非有办法使其成为默认选择)。在这种情况下,如果无法在前端更改它,我如何使“无”成为服务器中的标准值?谢谢!
    • 您的意思是这不起作用,因为用户在使用应用程序时会看到None 而不是NULL
    • 没有。当我将它放入服务器文件时,它不会作为用户界面上的选项出现。我还尝试使用命令 c(a, "None" = NULL) 将其放入 UI,但这也无济于事。
    • @ArielA 更新了答案。让我知道这是否是您正在寻找的。​​span>
    • 漂亮!感谢您的所有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 2018-06-30
    相关资源
    最近更新 更多