简体   繁体   English

R Shiny访问动态输入

[英]R Shiny accessing a dynamic input

I am new to shiny, I have been stuck on this for quite a while and the things I tried and read so far held no fruits. 我刚接触光泽,已经坚持了很长时间,到目前为止,我尝试和阅读的内容均未见成效。 I have a dropdown, where the user will choose a Question, then depending on the question, a drop down will be populated with the response numbers. 我有一个下拉菜单,用户可以在其中选择一个问题,然后根据该问题在下拉菜单中填充响应号。 After the user chooses the response number a map will be plotted. 用户选择响应编号后,将绘制一个地图。 I have gone through the dynamic drop down list, that is done. 我已经浏览了动态下拉列表,即已完成。 But I cannot get access to the second user input, the response number, so I can select my data for the map accordingly. 但是我无法访问第二个用户输入的响应号,因此我可以相应地为地图选择数据。 I feel like this is really simple I don't know why this does not work. 我觉得这真的很简单,我不知道为什么这行不通。 I am getting this error: 我收到此错误:

Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. .getReactiveEnvironment()$ currentContext()中的错误:如果没有活动的反应性上下文,则不允许进行操作。 (You tried to do something that can only be done from inside a reactive expression or observer.) (您试图做只能从反应式表达式或观察器内部完成的操作。)

My code is as follows, I have tentatively put in special cases and later I will expand to a general case with lists. 我的代码如下,我尝试将特殊情况放在后面,稍后再扩展到带有列表的一般情况。

library(shiny)



runApp(list(
  ui = shinyUI(pageWithSidebar(
    headerPanel('Dialect Comparison'),
    sidebarPanel(
      selectInput('Question', 'Choose The Question', c('100. Do you cut or mow the lawn or grass?'='Q100',
                                                       '101. Do you pass in homework or hand in homework?'='Q101')),
      uiOutput('C')


    ),
    mainPanel(
      plotOutput('plot')
    )
  )
  ),


  server = function(input, output){
    output$C = renderUI({ 
      ques = input$Question
      selectInput('Choice', 'Choose The Response', seq(0,max(ling_data[,ques])))
    })



    ##########
    #The Data#
    ##########

    #text = renderPrint({'uhu'})
    #print(text()) #THIS WORKS

    choice_number = renderPrint({input$Choice}) #http://shiny.rstudio.com/gallery/dynamic-ui.html
    print(choice_number()) #it fails here

    ...
    })    

  }
))

Reactive variables, including inputs, can be accessed only in a reactive scope created using one of the following: 包括输入在内的反应性变量只能在使用以下方式之一创建的反应性范围中访问:

  • shiny::reactive , shiny::eventReactive shiny::reactiveshiny::eventReactive shiny::reactive
  • shiny::observe , shiny::observeEvent shiny::observeshiny::observeEvent shiny::observe shiny::observeEvent
  • shiny::render*

or inside shiny::isolate block. 或内部shiny::isolate块。

For actions like logging you probably want observe block: 对于诸如日志记录之类的操作,您可能希望observe块:

observe({print(choice_number())})

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

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