【问题标题】:R shiny subset based on user-input and then code based on the subset基于用户输入的 R 闪亮子集,然后基于子集的代码
【发布时间】:2018-05-01 15:20:14
【问题描述】:

我尝试使用闪亮的服务器基于一个子集进行计算 - 称为 sub1。 sub1 工作正常,直到我尝试选择某个列或尝试对其进行计算。

sub1 是来自大型数据集的子集。

附上我的代码。让我知道我做错了什么。非常感谢。

server <- function(input,output) {
  data1 <- read.csv("..",fileEncoding="UTF-8-BOM")

  sub1<- reactive(subset(data1,Sex == input$sex & AVS.Impairment == 
  input$impairment & Year == input$yr & Mortality <= max(input$mm) & Mortality 
  >= min(input$mm)))

  output$text<-renderDataTable(sub1())
  # I want to use the sub1 to run more calculation. 
  # But when I try to select the column and create column it won't work

   sub1$table = paste0(sub1$Sex) %>% tolower # this line doesn't work
   # error is object of type 'closure' is not subsettable

}

shinyApp(ui=ui, server=server)

【问题讨论】:

  • 使用 sub1()$Sex 你正在使用一个响应函数,它是一个闭包。
  • 谢谢哈兰。我以前试过。它没有用。相反,它显示了一个不同的错误 --- .getReactiveEnvironment()$currentContext 中的错误:没有活动的反应上下文不允许操作。 (你试图做一些只能从反应式表达式或观察者内部完成的事情。)
  • 通过提供可重现的示例,它会更容易帮助您。
  • @MLavoie 感谢您的回复。所以它从一开始就是一个大数据集。然后我根据条件用户输入得到一个子集。但我想在子集上运行计算并生成一个图表。
  • 如果您查看 R 数据集包,您会发现许多数据集可用于重现您遇到的问题。这将使您生成可重现的示例。

标签: r shiny


【解决方案1】:

我创建了一个可重现的示例,如果我们未注释指示的代码,它会产生您提到的两个错误。您会注意到,当第一次开始使用 Shiny 时,您遇到的错误在人们中很常见,所以这个答案并不是什么新鲜事。

# Show two errors that occur in Shiny.  
# 1. The error that occurs when you try to use and input$var outside of a reactive context
# 2. The error that occurs when you treat a reative function as an dataset instead of a function.

library(shiny)
library(lubridate)
d <- datasets::longley
# Create a date field for dateRangeInput
d$date <- as.Date(as.character(d$Year),"%Y")
# Define UI for application that draws a histogram
ui <- fluidPage(

   # Application title
   titlePanel("Use of reactive in Shiny"),

   sidebarLayout(
      sidebarPanel(
        dateRangeInput("yr", h3("Date range"),
                       start = min(d$date),  
                       end = '1962-05-01'),
        sliderInput("mm","Minimum GNP", 500,min=min(d$GNP),
                     max=max(d$GNP))
     ),

      # Show a plot of the generated distribution
      mainPanel(
         textOutput("text"),
         textOutput("text_err"),
         dataTableOutput("table")

      )
   )
)

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

  sub1 <- reactive({
    subset(d,GNP <=input$mm & Year >= year(input$yr[[1]]) & Year <= year(input$yr[[2]]))
  })

  # Uncomment this will produce   
  # Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
  # sub2 <-   subset(d,GNP <=input$mm & Year >= year(input$yr[[1]]) & Year <= year(input$yr[[2]]))

  output$text <- renderPrint(paste(year(input$yr[[1]]),year(input$yr[[2]])))

  # This will generate the error Warning: Error in $: object of type 'closure' is not subsettable
  # output$text_err <- renderPrint(paste(sub1$Year))

  output$table <- renderDataTable(sub1())

}

# Run the application 
shinyApp(ui = ui, server = server)

【讨论】:

  • 谢谢你这样做哈兰!
猜你喜欢
  • 2014-02-26
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
  • 1970-01-01
  • 2020-07-26
  • 1970-01-01
  • 2021-06-02
相关资源
最近更新 更多