【问题标题】:using input variables in reactive part Rshiny在反应部分 Rshiny 中使用输入变量
【发布时间】:2018-10-22 04:32:29
【问题描述】:

我对 Rshiny 保持沉默。我想捕获用户输入的信息(变量)并将它们传递给 python 脚本,我将在 R 本身中调用它。但最初我需要服务器代码方面的帮助,我在反应代码部分没有做正确的事情。

到目前为止我的代码是:

library(shiny)
ui <- fluidPage(
  headerPanel( 
  titlePanel("RADP CR Prediction Tool"),
  br(),
  tags$head(tags$script(src = "message-handler.js")),
  textInput('Region', label = 'Enter the region'),
  textInput('Regulatory', label = 'Enter the regulatory status'),
  textInput('Description', label = 'Enter the description for the CR'),
  br(),
  br(),
  actionButton("goButton", "Go!"),
  mainPanel(

      # Output: Formatted text for caption ----
      h3(textOutput("caption", container = span)),

      # Output: Verbatim text for data summary ----
      verbatimTextOutput("summary"),

      # Output: HTML table with requested number of observations ----
      tableOutput("view")

    )
)

server <- function(input, output, session) {
region_input=reactive(input$Region)
regulatory_input <- reactive(input$Regulatory)
description_input <-reactive(input$Description)
observeEvent(input$do, {
    session$sendCustomMessage(type = 'testmessage',
      message = 'Thank you for clicking')
  })
}
shinyApp(ui, server)

当我运行代码时,它给了我错误:意外符号在: ") 服务器”

我需要使用regulatory_input、description_input和region_input作为R变量,这样我可以做进一步的分析。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您缺少括号。您关闭了mainPanelheaderPanel,但没有fluidPage 函数的右括号。

    你可以在 RStudio 中通过将光标放在第 25 行的 ) 之后看到这一点,你会看到 headerPanel( 中打开的 ( 被突出显示。您也可以选择您的代码,然后点击ctrl I 缩进它。您会看到server 函数在fluidPage 函数“内部”。

    这似乎是一件小事,但注意这样的细节对于编程来说至关重要。根据我的经验,10 次中有 9 次出现问题时,我会忘记这样的小事。

    至于标题中的问题,您输入的值已经在一个变量中:input$ID_OF_INPUT。就像使用任何其他变量一样使用它。没有理由使用以下内容复制其中的值:variable &lt;- reactive({input$id})。只需使用input$id 即可使用variable

    【讨论】:

    • 是的,好点。其实我更喜欢python。这个 R 东西是临时的。我在简单的 R 中运行 R 代码。我认为你是正确的。应该使用 Rstudio。让我纠正一下,再运行一下代码。
    • 但我的问题仍然存在。如何将输入值捕获为 R 变量?
    • 是的,我知道。但是我必须将它存储在一个变量中以供以后使用。这就是我使用变量的原因。 input$Region 也给出了未找到的对象输入
    • 这实际上不是您在这里所做的。阅读?reactive 的帮助:reactive({input$Region}) 不返回可以存储在变量中的值,它返回一个 函数,当调用时(使用 region_input(),因为它不是一个函数一个变量),返回input$Region的当前值。如果要将input$Region当前 值保存在一致的变量中,则需要使用isolate()
    猜你喜欢
    • 2019-06-08
    • 2021-01-06
    • 2017-11-14
    • 2017-11-13
    • 2017-10-07
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2015-06-25
    相关资源
    最近更新 更多