【问题标题】:VerbatimTextOutput() when hidden prevents the rest of the main body objects to be displayed in shiny appVerbatimTextOutput() 隐藏时可防止其余主体对象显示在闪亮的应用程序中
【发布时间】:2021-11-03 09:32:48
【问题描述】:

我在创建函数TLH 下方创建了闪亮的应用程序,并将其用作源文件。在主体中,我有一个 verbatimTextOutput()datatable()。奇怪的是,当我删除创建文本的行 verbatimTextOutput("oResults"), 时,数据表也被隐藏了。我不明白为什么会发生这种情况,但我想使用 TLH.r 文件来创建应用程序,而不是将所有内容都放在 app.r 文件中,因为我的实际应用程序更复杂。

app.r

library(shiny)
library(DT)
source("TLH.R")
ui <- fluidPage(
  # Application title
  titlePanel("Investment Advisor Monitoring - Tax Loss Harvesting"),
  
  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      tabsetPanel(type = "pills",
                  tabPanel("Investment Selected", 
                           textInput("StockTicker3", "Enter Stock Symbol", value = "XOM"),
                           numericInput("StartAmount", "Start Amount", value = "10000")
                  )
                  
      ),
      
      
      
      actionButton("goButton", "Calculate")
      
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      h3('Results'),
      
      tabsetPanel(type = "pills",
                  
                  tabPanel("Tax Loss Harvesting Results",
                           verbatimTextOutput("oResults"),
                           dataTableOutput("oParametersTable")
                  )
                  
      )
    )
  )
)

server <- function(input, output) {
  
  calc <- eventReactive(input$goButton,{
    
    TLH(input, output)
    
  })
  output$oResults <-  renderPrint({calc()})
}

shinyApp(ui = ui, server = server)

TLH.R

TLH <- function(input, output) {
  #Load Libraries
  
  ##Input Data
  
  StockTicker <- input$StockTicker3
  
  StartAmount <- input$StartAmount
  
  output$oParametersTable <- renderDataTable({
    
    ParametersTable <- data.frame(
      c("Stock Ticker", "Amount Invested"
        ),
      c(input$StockTicker, input$StartAmount
        )
    )
    
    colnames(ParametersTable)<- c("Parameters","Values")
    
    datatable(ParametersTable,
              options = list(
                paging =TRUE,
                pageLength =  15 
              ))})
  
  
  return("Tax Loss Harvest - Results")
}

【问题讨论】:

  • 我认为如果您渲染您的数据表,它将显示在我们的仪表板上。您只是在服务器中渲染打印
  • 我不明白你。最初两者都显示。如果我删除 verbatimTextOutput 一个都没有。这对表格有何影响?
  • 数据表的渲染发生在 TLH.R 内部

标签: r shiny


【解决方案1】:

我的建议是从函数TLH 返回一个对象列表。然后,您可以根据需要访问和显示每个对象。试试这个

TLH <- function(input, output) {
  #Load Libraries
  
  ##Input Data
  
  StockTicker <- input$StockTicker3
  
  StartAmount <- input$StartAmount
  
  #output$oParametersTable <- renderDataTable({
  
    ParametersTable <- data.frame(
      c("Stock Ticker", "Amount Invested"
      ),
      c(input$StockTicker, input$StartAmount
      )
    )
    
    colnames(ParametersTable)<- c("Parameters","Values")
    
    # datatable(ParametersTable,
    #           options = list(
    #             paging =TRUE,
    #             pageLength =  15 
    #           ))
    #})
  
  
  #return("Tax Loss Harvest - Results")
    return(list(
      val1 = ParametersTable,
      val2 = "Tax Loss Harvest - Results"
    ))
}

library(shiny)
library(DT)
# source("TLH.R")

ui <- fluidPage(
  # Application title
  titlePanel("Investment Advisor Monitoring - Tax Loss Harvesting"),
  
  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      tabsetPanel(type = "pills",
                  tabPanel("Investment Selected", 
                           textInput("StockTicker3", "Enter Stock Symbol", value = "XOM"),
                           numericInput("StartAmount", "Start Amount", value = 10000)
                  )
      ),
      actionButton("goButton", "Calculate")
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      h3('Results'),
      
      tabsetPanel(type = "pills",
                  tabPanel("Tax Loss Harvesting Results",
                           #verbatimTextOutput("oResults"),
                           DTOutput("t1")
                  )
                  
      )
    )
  )
)

server <- function(input, output) {
  
  calc <- eventReactive(input$goButton, {
    TLH(input, output)
  })
  
  output$t1 <- renderDT({
    datatable( calc()$val1, #ParametersTable,
              options = list(
                paging =TRUE,
                pageLength =  15 
              ))
  })
  
  output$oResults <-  renderPrint({calc()$val2})
}

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2020-12-13
    • 2017-02-21
    • 1970-01-01
    • 2021-05-18
    • 2020-09-21
    • 2019-07-02
    • 1970-01-01
    • 2020-06-02
    • 2021-07-13
    相关资源
    最近更新 更多