R闪亮电抗选择输入

时间:2018-08-20 14:28:45

标签: r shiny

当我尝试使我的选择输入在R闪亮的反应式上作用于另一个选择输入时,我总是遇到错误。我尝试了renderUi和updateSelectizeInput失败。我更喜欢使用updateSelectize,因为这与应用程序的其余部分保持一致。

我希望第二个选择输入为不是NA的列的列名。这是一些简化的代码:

library(dplyr)
library(shiny)

df <- setNames(data.frame(matrix(c(NA, NA, NA, 4, 6, 2, 1, 6, NA, NA), ncol = 5, nrow = 2, byrow = TRUE)), c("t1", "t2", "t3", "t4", "t5"))
df <- cbind(data.frame(ID = c("a", "b"), stringsAsFactors = FALSE), df)

all_drop_options <- df %>% pull(ID)

server <- function(input, output, session){

  updateSelectizeInput(session = session, inputId = "SID",
                       choices = all_drop_options, selected = "a",
                       server = TRUE)

  new_dat <- reactive({
    df %>% filter(ID == input$SID)
  })

  year_opts2 <- reactive({
    new_dat() %>%
      select(-ID) %>%
      select_if(~!is.na(.)) %>% colnames()
  })

  observe({
    updateSelectizeInput(session = session, inputId = "yr",
                         choices = year_opts2()
    )})
}

ui <- fluidPage(

  selectInput(inputId = "SID", label = NULL,
              choices = "a", selected = "a"),

  selectInput(inputId = "yr", label = "",choices = "")
)

shinyApp(ui, server)

2 个答案:

答案 0 :(得分:0)

您的问题是这个

src_q_regex = re.compile('\[sales\]\.\[customer\]', re.IGNORECASE)


for a in range(0, len(ssis_txt_files_2)):

    open_sample_file = open(ssis_txt_files_2[a], 'r')

    whatever = open_sample_file.readlines()
    whatever = ''.join(whatever)

    source_found = src_q_regex.search('hi')
    if source_found:
        thing = str(source_found.group())
        print(str(thing))

尝试评估new_dat <- reactive({ df %>% filter(ID == input$SID) }) 内部的内容将无法正常工作。只需将reactve()替换为:

df %>% filter(ID == input$SID)

如此:

new_dat()

编辑:

正如您所指出的,脚本有时会出错,因为library(dplyr) library(shiny) df <- setNames(data.frame(matrix(c(NA, NA, NA, 4, 6, 2, 1, 6, NA, NA), ncol = 5, nrow = 2, byrow = TRUE)), c("t1", "t2", "t3", "t4", "t5")) df <- cbind(data.frame(ID = c("a", "b"), stringsAsFactors = FALSE), df) all_drop_options <- df %>% pull(ID) ui <- fluidPage( selectInput(inputId = "SID", label = NULL, choices = "a", selected = "a"), selectInput(inputId = "yr", label = "",choices = "") ) server <- function(input, output, session){ updateSelectizeInput(session = session, inputId = "SID", choices = all_drop_options, selected = "a", server = TRUE) year_opts2 <- reactive({ tryCatch({ df %>% filter(ID == input$SID) %>% select(-ID) %>% select_if(~!is.na(.)) %>% colnames()}, error = function(x){ return('') }) }) observe({ updateSelectizeInput(session = session, inputId = "yr", choices = year_opts2() )}) } shinyApp(ui, server) 偶尔会因错误返回空数据帧而使filter停止执行。

我添加了colnames()来缓解这种情况,但不完全确定为什么会发生这种情况!

答案 1 :(得分:0)

背景:

由于某些原因,第二个reactive中的“我不知道它们”,new_dat()在第一个迭代后变为空,因此select_if做应做的事情并生成错误。

#Run this code for better understanding
observe(print(new_dat()))

year_opts2 <- reactive({
   browser()
 new_dat() %>%
   select(-ID) %>%
   select_if(~!is.na(.)) %>%
   colnames()

})

observe(print(year_opts2()))

现在,如果我们对browser()select_if(~!is.na(.))进行注释,则代码将正常工作而不会出现任何错误。像这样

observe(print(new_dat()))

year_opts2 <- reactive({
#browser()
 new_dat() %>%
   select(-ID) %>%
  #select_if(~!is.na(.)) %>%
   colnames()

})

observe(print(year_opts2()))

解决方案:

希望下面可以解决您的问题

year_opts2 <- reactive({
 colnames(new_dat()[,!is.na(new_dat())][-1])
})