简体   繁体   English

如何在 R Shiny 中使用 NULL 值作为变量调用

[英]How do I use the NULL Value as a variable call in R Shiny

How do I pass NULL as a Variable Value in RSHINY?如何在 RSHINY 中将 NULL 作为变量值传递?

In phyloseq, there is a plot called plot_net.在 phyloseq 中,有一个名为 plot_net 的图。 The most basic plot_net plot code looks like this:最基本的 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)

在此处输入图片说明

I am trying to create an RShiny app which allows a user to alter this graphic.我正在尝试创建一个 RShiny 应用程序,它允许用户更改此图形。

point_label has several different options (ex: "SecTech", "SampleID", NULL). point_label 有几个不同的选项(例如:“SecTech”、“SampleID”、NULL)。

I already have all of the other values for this label, I am just not sure how to add NULL.我已经有了这个标签的所有其他值,我只是不确定如何添加 NULL。

Here is what I did:这是我所做的:

This might not run since it isn't in a shiny app but I included it as an example to illustrate the issue.这可能无法运行,因为它不在闪亮的应用程序中,但我将其作为示例来说明问题。

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)

This line is my question:这一行是我的问题:

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

How do I add NULL to my list of choices.如何将 NULL 添加到我的选择列表中。 No matter how I tried it, NULL was always taken as a zero value and it does not appear as an option.无论我如何尝试,NULL 始终被视为零值,并且它不会作为选项出现。

If I write NULL as "NULL', the phyloseq function plot_net doesn't take it. It only takes the value point_label = NULL for no value.如果我将 NULL 写为“NULL”,则 phyloseq 函数 plot_net 不接受它。它只接受值 point_label = NULL 表示没有值。

I think that it is possible to create an if... else loop where if a user clicks NULL on a checkboxInput then the plot will be generated by a second line of code specifying that the value in point_label is NULL, but that can be really cumbersome if there are several variables with a possible NULL Value.我认为可以创建一个 if...else 循环,如果用户在 checkboxInput 上单击 NULL,则该图将由第二行代码生成,指定 point_label 中的值为 NULL,但这可能是真的如果有多个变量可能具有 NULL 值,则很麻烦。

There probably is some obvious trick like placing a $ or % in front of the NULL value but I couldn't find it.可能有一些明显的技巧,例如在 NULL 值前面放置 $ 或 % 但我找不到它。 If anyone could help it would be great!如果有人可以提供帮助,那就太好了!

I don't think there is a way to use NULL in selectInput .我认为没有办法在selectInput使用NULL Here's an alternative which you almost worked out - Use "None" (or any other replacement value) in selectInput and switch it with NULL while plotting.这是您几乎找到的替代方法 - 在selectInput使用"None" (或任何其他替换值)并在绘图时将其切换为NULL This way you don't have to write multiple if...else .这样你就不必写多个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)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM